MFC - Several situations and solutions of LNK2001 "unresolved external symbol"

MFC: Several situations and solutions of LNK2001 "unresolved external symbol"

"Error LNK2001 Unresolvable external symbol", the content of this type of error is relatively complete, so I took it down for reference.

VC++ often encounters a link error LNK2001, which is very annoying, because for programmers, the best error to correct is a compilation error, and generally speaking, when a connection error occurs, the compilation has passed. There are many reasons for connection errors, especially the LNK2001 error, which often makes people confused. If you don't study and understand VC++ in depth, it is very difficult to correct the connection error LNK2001. In the process of learning VC++, beginners encounter LNK2001 error error messages mainly: unresolved external symbol "symbol" (unresolved external "symbol"). This error message is generated if the linker cannot find the referenced function, variable, or label in all library and object files .
In general, errors occur for two reasons:

  • One is that the referenced function or variable does not exist, is spelled incorrectly, or is used incorrectly;
  • Second, a different version of the connection library may be used.

The following are possible causes of the LNK2001 error:

one. LNK2001 due to encoding error.

  1. Mismatched program code or module definition (.DEF) files can cause LNK2001. For example, if a variable "var1" is declared in a C++ source file and an attempt is made to access the variable as variable "VAR1" in another file, this error will occur.
  2. If you use an inline function that is defined in a .CPP file instead of a header file , it will cause a LNK2001 error.
  3. When calling a function , if the parameter type used does not match the type when the function is declared, LNK2001 will be generated.
  4. Attempting to call a virtual function from a base class constructor or destructor will result in LNK2001.
  5. Pay attention to the publicity of functions and variables, only global variables and functions are public . Static functions and static variables have the same usage scope restrictions. Attempting to access any static variable not declared within the file will result in a compilation error or LNK2001 from outside the file. Variables declared within a function (local variables) can only be used within the scope of that function. C++ global constants only have static linking capabilities . This is different from C, which also produces a LNK2001 error if you try to use a global variable in multiple files in C++. One solution is to add the initialization code of the constant in the header file when needed, and include the header file in the .CPP file; another method is to assign a constant to the variable when used.

two. LNK2001 due to compile and link settings

1. If the /NOD (/NODEFAULTLIB) option is used when compiling, the runtime library and MFC library required by the program will be written into the object file module by the compiler when linking, but unless these library names are explicitly included in the file, these libraries will not will be linked into the project file. Using /NOD in this case will result in error LNK2001.

2. If the program entry is not set for wWinMainCRTStartup, the LNK2001 error message "unresolved external on _WinMain@16" will be obtained when using Unicode and MFC.

3. When compiling with the /MD option, since all runtime libraries are kept in the dynamic link library, the reference to "func" in the source file is the reference to "__imp__func" in the object file. LNK2001 will occur on __imp__func if you try to link using static LIBC.LIB or LIBCMT.LIB; LNK2001 will also occur when linking with MSVCxx.LIB if you compile without the /MD option.

4. When compiling with the /ML option, linking with LIBCMT.LIB will cause LNK2001 on _errno.

5. When compiling the debug version of the application, if the release version of the modal library is used for linking, LNK2001 will also be generated; similarly, the same problem will also occur when using the debug version of the modal library to link the release version of the application.

6. Mixing different versions of libraries and compilers can also cause problems, because newer versions of libraries may contain symbols and declarations that earlier versions did not.

7. Using inline and non-inline compile options in different modules can cause LNK2001. If the function inline (/Ob1 or /Ob2) is turned on when the C++ library is created, but the function inline is turned off (without the inline keyword) in the corresponding header file describing the function, then this error message will be obtained. In order to avoid this problem, the inline keyword should be used to mark the inline function in the corresponding header file.

8. Incorrect /SUBSYSTEM or /ENTRY settings can also cause LNK2001.

In fact, there are many reasons for LNK2001, and the above reasons are only part of them. For beginners, these are enough to understand for a while. However, the purpose of analyzing the cause of the error is to avoid the occurrence of the error. Although the LNK2001 error is difficult, as long as the above problems are noticed, it can still be avoided and solved.

Guess you like

Origin blog.csdn.net/wu_zhiyuan/article/details/130251144