Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
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
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
Updated 31-Jul-12 6:44am
v2
Comments
[no name] 31-Jul-12 12:29pm    
Have you tried making InitButtonControl public?
Richard MacCutchan 31-Jul-12 12:30pm    
In C?
[no name] 31-Jul-12 12:39pm    
My bad thanks
lewax00 31-Jul-12 12:30pm    
I'm pretty sure there is no public or private in C...
[no name] 31-Jul-12 12:40pm    
There is not. My bad.

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.
 
Share this answer
 
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[^]
 
Share this answer
 
Comments
Richard MacCutchan 9-Aug-12 13:12pm    
This is not the problem, please read all the existing comments.
Captain Price 10-Aug-12 13:55pm    
in my case, it is.
Richard MacCutchan 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900