Solutions to Common Errors Error LNK2001: unresolved external symbol _main Error C2440: cannot conv

本文针对LNK2001、C2440、RC1015等常见编程错误进行了详细解析,并提供了相应的解决方案。文章还介绍了如何正确配置项目类型、解决类型转换问题以及处理资源文件依赖等。

Solutions to Common Errors

  • Error LNK2001: unresolved external symbol _main
  • Error C2440: cannot convert from 'void*' to 'HICON__ *' (or similar)
  • Fatal error RC1015: cannot open include file 'afxres.h'
  • Error LNK2001: unresolved external symbol InitCommonControls
  • Dialog does not display when certain controls are added

    Error LNK2001: unresolved external symbol _main

    An unresolved external occurs when some code has a call to a function in another module and the linker can't find that function in any of the modules or libraries that you are currently linking to.

    In this specific case, it means one of two things. Either you are trying to write a Win32 GUI application (or non-console application) and accidently compiled it as a Console application... or you really are trying to compile a console application and didn't write or properly compile in a main() function.

    Generally the first is the most common, if you specify Win32 Console as the project type in VC++ when you create your project you will get this error. You will also likely get it if you try to compile from the command line using BC++ but you neglect to specify the correct parameters to tell it to make a Win32 GUI application instead of a console app which is the default.

    Fixing

    If you're using VC++ re-create your project and select the Win32 Application project type (NOT "Console").

    If you're using BC++ command line compiler, use -tW to specify a windows application.

    Error C2440: cannot convert from 'void*' to 'HICON__ *' (or similar)

    If you're compiling the code from this tutorial, it means that you are trying to compile it as C++ code. The code is written for the bcc32 and VC++ C compilers, and as such may not compile exactly the same under C++ since C++ has much stricter rules about converting types. C will just let it happen, C++ wants to you to make it explicit.

    VC++ (and most compilers) will automatically compile a file with a .cpp extension as C++ code, and a file with a .c extension as C code. If you have added the tutorial code to a .cpp file, this is the most likely reason of getting this error.

    If you're compiling code not from this tutorial, I can't guarantee that it's correct and therefor it may actually be an error that needs resolving. You'll have to use your own judgement to determine if it's safe to cast the value and remove the error, or if you are actually trying to make a variable be something it's not.

    Fixing

    If you want to use C, simply rename your file from .cpp to .c. Otherwise, simply add a cast, all of the code in the tutorial will work without any other changes when compiled as C++.

    For example, in C this will work:

    	HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
    

    But in C++ requires a cast:

    	HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
    

    Fatal error RC1015: cannot open include file 'afxres.h'.

    Oddly enough, VC++ adds afxres.h to resource files even when you aren't using an MFC project, and yet the file may only be installed if you install MFC. This perticular file isn't actually required, so to fix the error you can edit the .rc file in notepad and replace both occurances of "afxres.h" with "winres.h" (note that there should be two of them, and you need to change both).

    Error LNK2001: unresolved external symbol InitCommonControls

    You aren't linking to comctl32.lib which this API is defined in. This library is not included by default so you will either need to add it to the libraries on your command line, or add it in your VC++ project settings on the Link tab.

    Dialog does not display when certain controls are added

    Controls such as the ListView, TreeView, Hotkey, Progress Bar, and others are classified as Common Controls, as they were added to windows in comctl32.dll and were not available prior to Windows 95. Controls such as BUTTON, EDIT, LISTBOX, etc... while no doubt being common, are not "Common Controls" and I generally refer to them as "Standard Controls".

    If you add a Common Control to a dialog and it fails to display, you most likely failed to callInitCommonControls() before running your dialog, or perhaps at all. The best place to call it is first thing inWinMain(). Calling it in WM_INITDIALOG is too late, since the dialog will fail before it reaches this point and it will never get called.

    Some people and documentation may tell you that InitCommonControls() is deprecated and you should useInitCommonControlsEx(). Feel free to do this if you want, InitCommonControls() is just simpler and there's nothing wrong with using it.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值