编译用C编写的Win32项目时遇到链接器问题. [英] I have a linker problem when compiling my Win32 project written in C.

查看:63
本文介绍了编译用C编写的Win32项目时遇到链接器问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译用C语言编写的Win32项目时遇到链接器问题.

My Linker Errors
1.)错误1错误LNK2001:无法解析的外部符号"void __cdecl InitButtonControl(void)"(?InitButtonControl @@ YAXXZ)

2.)错误2错误LNK1120:1个未解决的外部设备


我的小项目是创建一个Win32 Custom Control using Child Windows.真的是button.我的项目中有2 个源文件:
1. button.c-包含窗口过程和有关按钮的其他内容.
2. main.c-包含WinMain 功能和用于测试控件的对话框过程.

BUTTON.C的代码

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);
}



MAIN.C的代码



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;
}




问题在代码中的位置
我确定问题出在BUTTON.C源文件的InitButtonControl()函数中.我只是通过注释WinMain中的InitButtonControl()函数调用来解决这个问题的[请参见MAIN.c].

请帮我解决问题!我已经尝试了好几天了.


#############问题改进#########
嘿,我发现了大问题.我复制了所有源文件并开始了一个新项目.然后编译.然后我的程序运行良好.所以我的源代码没有问题,我很确定.

因此,编译器肯定是这种情况.也许与编译器设置有关.请需要帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#####################################


其他信息:
操作系统:Windows 7
编译器:MS Visual C ++ 2010
体系结构:64位




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

推荐答案

正如其他人在评论中所建议的,几乎可以肯定是编译为C ++"与编译为C"的问题.最重要的线索是未解析符号名称中的装饰器.

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.

报价:

1.)错误1错误LNK2001:未解决的外部符号"void __cdecl InitButtonControl(void)"(?InitButtonControl @@ YAXXZ)

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.


在简单单词中,解决方案是声明了某种东西(函数/类/数据类型/用户定义的类型..),但未定义.或者,如果已定义,则LINKER找不到它.

阅读以下文章和论坛答案以获取更多详细信息;
http://social.msdn.microsoft.com/Forums/zh/vcgeneral/thread/2a7b43de-60ed-4274-8a67-349a2a34da7a [ http://www.informit.com/guides/content.aspx?g=cplusplus& seqNum = 298 [ ^ ]
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[^]


这篇关于编译用C编写的Win32项目时遇到链接器问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆