VC6.0 error lnk2001:unresolved external symbol _main solution

Source: Internet
Author: User

Learning VC + + often encounter link error LNK2001, this error is very annoying, because for programmers, it is better to change the error than the compilation error, and in general, when a connection error occurs, the compilation has passed. There are many reasons for connection errors, especially LNK2001 errors, which are often unclear. If you do not learn and understand VC + +, to correct the connection error LNK2001 very difficult.

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

unresolved external symbol "symbol" (an indeterminate external "symbol").

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

The following are the possible causes of LNK2001 errors:

A LNK2001 caused by coding errors

1. does not match the program code or module definition (. DEF) file can cause LNK2001. For example, if you declare a variable "var1" within a C + + source file and try to access the variable as a variable "VAR1" within another file, the error occurs.

2. If the inline function used is in the. The definition within the CPP file, rather than within the header file, will result in a LNK2001 error.

3. LNK2001 is generated when the function is called when 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 cause LNK2001.

5. Be aware of the common nature of functions and variables, only global variables, functions can be common. static functions and static variables have the same usage scope restrictions. Attempting to access any static variable that is not declared within the file from outside the file causes 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, which can also produce LNK2001 errors if you try to use global variables in multiple files in C + +. A workaround is to include the initialization code for the constant in the header file when needed, and in the. CPP file, and another method is to assign a constant to the variable when it is used.

Two LNK2001 caused by compilation and linked 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 by the compiler when connected, but these libraries are not linked to the project file unless they are explicitly included in the file. Using/nod in this case will cause error LNK2001.

2. If you do not set a program entry for wWinMainCRTStartup, you will get the LNK2001 error message "unresolved external on [email protected]" When using Unicode and MFC.

3. When compiling with the/MD option, since all runtimes are kept within the 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, LNK2001 will occur on the __imp__func if you do not use the/MD option to edit the

LNK2001 can also occur when using a MSVCxx.LIB connection.

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

5. When compiling a debug version of an application, it also generates LNK2001 if the connection is made with a release modal library, and the same problem arises when connecting to the release application using the debug version of the modal library.

6. Mixed use of different versions of libraries and compilers can also cause problems because the new version of the library may contain symbols and descriptions that were not previously available.

The function inline (/ob1 or/OB2) is opened while programming, but the function inline (without the inline keyword) is closed in the corresponding header file describing the function, and the error message is obtained. To avoid this problem, you should flag inline functions with the inline keyword in the corresponding header file.

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

Workaround:

Resolve external symbol error: _main,[email protected],__beginthreadex

When you create an MFC project without using the MFC AppWizard Wizard, you generate many connection errors at compile time, such as Error LNK2001 errors, and typical error hints are:

LIBCMTD.lib (crt0.obj): Error lnk2001:unresolved external symbol _main

LIBCD.lib (wincrt0.obj): Error lnk2001:unresolved external symbol [email protected]

Msvcrtd.lib (crtexew.obj): Error lnk2001:unresolved external symbol [email protected]

Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __beginthreadex

Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __endthreadex

1. Windows subsystem settings error, prompt:

LIBCMTD.lib (crt0.obj): Error lnk2001:unresolved external symbol _main

For Windows projects to use the Windows subsystem instead of the console, you can set:

[Project]--[Settings]--Select the "Link" property page and change the/subsystem:console to/subsystem:windows in Project options

2. Console subsystem Setup error, prompt:

LIBCD.lib (wincrt0.obj): Error lnk2001:unresolved external symbol [email protected] Console project to use the console subsystem instead of Windows, set:

[Project]--[Settings]--Select the "Link" property page

3. Program entry settings error, prompt:

Msvcrtd.lib (crtexew.obj): Error lnk2001:unresolved external symbol [email protected] Typically, the program entry function for an MFC project is WinMain, If you compile the Unicode version of the project, 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 wWinMainCRTStartup in Entry-point symbol to

4. Thread run-time library setup error, hint: Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __beginthreadex

Nafxcwd.lib (thrdcore.obj): Error lnk2001:unresolved external symbol __endthreadex This is because MFC wants to use a multi-threaded time library and needs to change the settings:

[Project]--[Settings]-Select the ' C + + ' property page, select Code Generation in the category, and select Debug in the Use Run-time Library Multithreaded or multithreaded

Where, single-threaded single-threaded static link library (release version)

multithreaded multithreaded static link library (release version)

Multithreaded DLL multi-threaded dynamic link library (release version)

Debug single-threaded single thread static link library (Debug version)

Debug multithreaded multithreaded static link library (Debug version))

Debug multithreaded DLL multithreaded dynamic link library (Debug version)

Single Thread: Multi-Threading is not required when multi-threaded calls are used in a DOS environment

Multithreading: can run concurrently

Static Library: Link the library and the program directly, you can run away from the MFC library

Dynamic Library: Requires the corresponding DLL dynamic library for the program to run

Release version: used at official release

Debug Version: Debug phase Use

VC6.0 error lnk2001:unresolved external symbol _main solution

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.