Error lnk2001:unresolved external symbol _main

Source: Internet
Author: User


Resolve external symbol Error: _main,_winmain@16,__beginthreadex
When you create an MFC project, you do not use the MFC AppWizard Wizard, if you do not set the project parameters, you will generate many connection errors at compile time, such as error LNK2001 errors, typical error prompts are:
LIBCMTD.lib (crt0.obj): Error lnk2001:unresolved external symbol _main
LIBCD.lib (wincrt0.obj): Error lnk2001:unresolved external symbol _winmain@16
Msvcrtd.lib (crtexew.obj): Error lnk2001:unresolved external symbol _winmain@16
Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __beginthreadex
Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __endthreadex

1. Windows subsystem Setup error, prompt:
LIBCMTD.lib (crt0.obj): Error lnk2001:unresolved external symbol _main

You can set Windows projects to use the Windows subsystem instead of the console:

[Project]--> [Settings]--> Select the Link property page,
Change/subsystem:console to/subsystem:windows in Project options

2. Console subsystem Setup error, prompt:
LIBCD.lib (wincrt0.obj): Error lnk2001:unresolved external symbol _winmain@16

To use the console subsystem instead of Windows, the console project sets:

[Project]--> [Settings]--> Select the Link property page,
Change/subsystem:windows to/subsystem:console in Project options

3. Program entry settings error, prompt:
Msvcrtd.lib (crtexew.obj): Error lnk2001:unresolved external symbol _winmain@16

Typically, the program entry function for an MFC project is WinMain, and if the Unicode version of the project is compiled, the program entry must be changed to wWinMainCRTStartup, so the program entry needs to be reset:

[Project]--> [Settings]--> Select the Link property page,
Select output in category,
And then fill in the wWinMainCRTStartup in Entry-point symbol, you can

4. Thread Run-time Library Setup error, prompt:
Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __beginthreadex
Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __endthreadex

This is because MFC needs to change the settings when it wants to use multiple-threading libraries:

[Project]--> [Settings]--> Select the "C + +" property page,
Select the code Generation in category,
Then select Debug multithreaded or multithreaded in the use Run-time Library
Salted Fish Ranger (75374355) 12:11:11
which
single-threaded single-threaded static link library (release version)
multithreaded multithreaded static link library (release version)
Multithreaded DLL multithreaded dynamic link library (release version)
Debug single-threaded single-threaded static link library (Debug version)
Debug multithreaded multithreaded static link library (Debug version)
Debug multithreaded DLL multithreaded dynamic link library (Debug version)

Single-threaded: Multi-Threading is not required, used in DOS environments
Multithreading: can run concurrently
Static Library: Directly link the library with the program, you can run out of the MFC library
Dynamic Library: Requires the corresponding DLL dynamic library, the program can run
Release version: Use when officially released
Debug Version: Debugging phase use

Beginners in the process of learning VC + +, encountered LNK2001 error message mainly:

unresolved external symbol "symbol" (Indeterminate external "sign").

This error message is generated if the connector cannot find the referenced function, variable, or label in all library and destination files. In general, there are two reasons for the error: one is the referenced function, the variable does not exist, the spelling is incorrect, or the use of the error, and then a different version of the connection library may be used.

The following are the reasons why LNK2001 errors may occur:

A LNK2001 caused by coding errors

1. does not match the program code or module definition (. DEF) file can lead to LNK2001. For example, this error occurs if a variable "var1" is declared inside a C + + source file, but an attempt is made to access the variable "VAR1" in another file.

2. If you use an inline function that is in the. CPP is defined within the file, not within the header file, resulting in a LNK2001 error.

3. When a function is invoked, the LNK2001 is generated if the parameter type used does not match the type of the function declaration.

4. Attempting to invoke a virtual function from a constructor or destructor of a base class will result in LNK2001.

5. To be aware of the public nature of functions and variables, only global variables and functions can be public. static functions and static variables have the same use-scope limit. Attempting to access any static variable not declared within the file from outside the file will result in a compilation error or LNK2001.

Variables declared within a function (local variables) can only be used within the scope of the function.

The global constants of C + + have only static connection performance. This is different from C, if you try to use global variables within multiple files in C + +, you can also generate LNK2001 errors. One solution is to include the initialization code for the constant in the header file when needed, and in the. The CPP file contains the header file, and another method is to assign a constant to the variable when used.

Two LNK2001 caused by compilation and link settings

1. If the/nod (/NODEFAULTLIB) option is used at compile time, the runtime and MFC libraries required by the program are written to the target file module when connected, but are not linked to the project file unless they are explicitly included in the file. Using/nod in this case will result in an error LNK2001.

2. If you do not set the program entry for wWinMainCRTStartup, you will get a LNK2001 error message for "unresolved external on _winmain@16" when using Unicode and MFC.

3. When compiling with the/MD option, since all runtime libraries are kept within a dynamic-link library, references to "Func" in the source file are references to "__imp__func" in the target file. If you attempt to connect using a static library LIBC.LIB or LIBCMT.LIB, a LNK2001 occurs on the __imp__func, if you do not use the/MD option
LNK2001 can also occur when using a MSVCxx.LIB connection.

4. When compiling with the/ML option, a LNK2001 will occur on the _errno if the LIBCMT.LIB link is used.

5. When compiling a debug version of an application, connecting with a distribution modal library can also produce LNK2001, as well as using the debug version of the modal library to connect to the release application.

6. Mixed use of different versions of libraries and compilers can also cause problems, since newer libraries may contain symbols and descriptions that are not available in previous versions.

The function inline (/ob1 or/OB2) is turned on while programming, but the error message is obtained when the function inline is closed in the corresponding header file describing the function (no inline keyword). To avoid this problem, you should use the inline keyword flag inline function in the corresponding header file.

8. Incorrect/subsystem or/entry settings can also cause LNK2001.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.