Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Win32 MFC
I have a linker problem when compiling my Win32 project written in C.
 
My Linker Errors
1.)Error 1 error LNK2001: unresolved external symbol "void __cdecl InitButtonControl(void)" (?InitButtonControl@@YAXXZ)
 
2.)Error 2 error LNK1120: 1 unresolved externals

 
My Little Project is to create a Win32 Custom Control using Child Windows. Really a button. There're 2 source files in my project:
1. button.c - contains window procedure and some other stuff about the button.
2. main.c - contains the WinMain function and a Dialog Procedure to test the control.
 
Code of BUTTON.C
#include "button.h"

LRESULT CALLBACK ButtonProc(HWND hwnd,UINT msg,WPARAM w,LPARAM ww)
{
	return DefWindowProc(hwnd,msg,w,ww);
}
 
void InitButtonControl()
{
	WNDCLASSEX wc;
    
	wc.cbSize         = sizeof(wc);
	wc.lpszClassName  = szButtonClass;
	wc.hInstance      = GetModuleHandle(0);
	wc.lpfnWndProc    = ButtonProc;
	wc.hCursor        = LoadCursor (NULL, IDC_ARROW);
	wc.hIcon          = 0;
	wc.lpszMenuName   = 0;
	wc.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.style          = 0;
	wc.cbClsExtra     = 0;
	wc.cbWndExtra     = sizeof( ButtonInfo * );
	wc.hIconSm        = 0;
 
	RegisterClassEx(&wc);
}
 
Code of MAIN.C
#include <Windows.h>
#include "resource.h"
#include "button.h"

BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	InitButtonControl();
	DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgProc);
	return 0;
}
 
BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
		return TRUE;
 
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDCANCEL:
		case IDOK:
			PostMessage(hwnd,WM_CLOSE,0,0);
			break;
		}
		return TRUE;
 
	case WM_CLOSE:
		EndDialog(hwnd,0);
		return TRUE;
	}
	return FALSE;
}
 

The Place of the problem in Code
I'm sure that the problem is in the InitButtonControl() function in BUTTON.C source file. I figured it out by simply commenting the InitButtonControl() function call in WinMain [see MAIN.c].
 
Please Help me to solve the problem ! I've been trying for days.
 

############QUESTION IMPROVEMENT#########
Hey I found the Big Problem. I copies all of my source files and started a new project. Then Compiled it. Then my program worked nicely. So there is no problem with my source code, i'm pretty sure.
 
So, definitely the case is with the compiler. Maybe with the compiler settings. Please Need Help !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
########################################
 

Further Information:
OS : Windows 7
Compiler : MS Visual C++ 2010
Architecture : 64-bit
Posted 31-Jul-12 6:24am
Edited 31-Jul-12 6:44am
v2
Comments
Wes Aday at 31-Jul-12 12:29pm
   
Have you tried making InitButtonControl public?
Richard MacCutchan at 31-Jul-12 12:30pm
   
In C?
Wes Aday at 31-Jul-12 12:39pm
   
My bad thanks
lewax00 at 31-Jul-12 12:30pm
   
I'm pretty sure there is no public or private in C...
Wes Aday at 31-Jul-12 12:40pm
   
There is not. My bad.
Wes Aday at 31-Jul-12 12:32pm
   
My bad
lewax00 at 31-Jul-12 12:29pm
   
Is there a prototype for InitButtonControl in button.h?
Pravinda Amarathunge at 31-Jul-12 12:41pm
   
yes there is.. please see my comment below !
Richard MacCutchan at 31-Jul-12 12:29pm
   
That looks like a C++ decorated name. Are you sure this is pure C?
Pravinda Amarathunge at 31-Jul-12 12:38pm
   
yes
Pravinda Amarathunge at 31-Jul-12 12:40pm
   
Hey I found the Big Problem. I copies all of my source files and started a new project. Then Compiled it. Then my program worked nicely. So there is no problem with my source code, i'm pretty sure.
 
So, definitely the case is with the compiler. Maybe with the compiler settings. Please Need Help !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Richard MacCutchan at 31-Jul-12 13:00pm
   
Check this link for the compiler options and make sure you are using a consistent set.
Pravinda Amarathunge at 1-Aug-12 0:49am
   
thanks
Wes Aday at 31-Jul-12 12:47pm
   
Ah... Configuration Properties->C/C++->Advanced->Compile As
pasztorpisti at 31-Jul-12 12:52pm
   
I don't know the exact reason why, but the main.c file (in which your code calls your InitButtonControl() function) was compiled as a C++ source, while the file containing the function was compiled as C. I have a VS2008 on my machine here so can't find out if your VS2010 has such an option or not. This is usually an evil stuff becuase 2 seaparate compilation units can interpret the same header file (button.h in this case) differently. This is why some header files contain the extern "C" blocks with __cplusplus ifdefs - to force the interpretation of the header file as a C header file in both C and C++ compilation units.
nv3 at 31-Jul-12 13:26pm
   
The behavior you described could be the result of pre-compiled header mechanics. Could it be that you changed the signature of InitButtonControl and did not do a complete Rebuild?
Pravinda Amarathunge at 1-Aug-12 0:58am
   
What the heck ? Now I can compile my project fine without any errors. I didn't do anything. So is that happens ? I've got this error many times.
nv3 at 1-Aug-12 8:50am
   
Sounds indeed like a pre-compiled header problem. Have you checked the pre-compiled header settings of your project?
Pravinda Amarathunge at 1-Aug-12 13:34pm
   
Well.... I don't have a one ! if then ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

As others have suggested in the comments, almost certainly a "compile as C++" versus "compile as C" issue. The big clue is the decorator in the name of the unresolved symbol.
 
Quote:
1.)Error 1 error LNK2001: unresolved external symbol "void __cdecl InitButtonControl(void)" (?InitButtonControl@@YAXXZ)

 
Although other provided the guidance above, thought I'd post this as a solution since it will remain in the "unanswered questions" category otherwise.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In Simple Words the solution is Something(function/class/datatype/user-defined typse..) is declared, but not defined, Or if defined, the LINKER can't find it.
 
Read the following article and Forum answers for more details ;
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/2a7b43de-60ed-4274-8a67-349a2a34da7a[^]
 
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=298[^]
  Permalink  
Comments
Richard MacCutchan at 9-Aug-12 13:12pm
   
This is not the problem, please read all the existing comments.
Pravinda Amarathunge at 10-Aug-12 13:55pm
   
in my case, it is.
Richard MacCutchan at 11-Aug-12 11:10am
   
Well, we all assumed you had investigated and eliminated the obvious answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100