5 years ago (2015-03-21)  Algorithm language |   First to comment  9 
post score 0 times, average 0.0

Solution: Move the function from the .cpp file to the .h file, the root cause of the function is not successful. Learning VC + + often encounter link error LNK2001, this error is very annoying, because for programmers, the best mistake is to compile errors, and generally speaking, when a connection error occurs, the compiler has passed.There are many reasons for connection errors, especially LNK2001 errors, which often make people unknown why.If you do not deeply study and understand VC++, it is very difficult to correct the connection error LNK2001.In the process of learning VC++ for beginners, the error message of the LNK2001 error encountered is mainly: unresolved external symbol “symbol” (undefined external “symbol”). This error message is generated if the linker cannot find the referenced function, variable, or tag in all libraries and object files.In general, there are two reasons for the error: First, the referenced function, variable does not exist, incorrect spelling or use error; secondly, different versions of the connection library may be used. The following are the possible causes of the LNK2001 error: 1.LNK2001 due to a coding error. 1.Incompatible program code or module definition (.DEF) files can cause LNK2001.For example, if you declare a variable "var1" in a C++ source file and you try to access the variable in another file with the variable "VAR1", this error will occur. 2.If you use an inline function that is defined in a .CPP file, rather than being defined in the header file, it will cause the LNK2001 error. 3.If the type of the parameter used when the function is called does not match the type declared by the function, LNK2001 will be generated.limit.When trying to access any static variable that is not declared inside this file from outside the file it will cause compilation errors or LN 4.Attempting to call a virtual function from a base class constructor or destructor will result in LNK2001. 5.Pay attention to the commonality of functions and variables. Only global variables and functions are public. Static functions and static variables have the same range of use K2001. Variables declared within functions (local variables) can only be used within the scope of the function. Using global variables also produces LNK2001 errors.A solution is to include 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 in use. two.LNK2001 due to compilation and link settingsIf you are compiling with /NC++ global constants only static connection performance.This is different from C. If you try to make the OD (/NODEFAULTLIB) option in multiple files in C++, the runtime libraries and MFC libraries needed by the program are written to the target file module by the compiler when connected, but unless explicitly included in the file These library names, otherwise these libraries will not be linked into the project file.Using /NOD in this case will result in error LNK2001.When compiling with the /MD option, since all the runtimes are kept in the dynamic link library, the reference to "func" in the source file is a reference to "__imp__func" in the target file.If you try to connect using the static library LIBC.LIB or LIBCMT.LIB, 2.If you do not set the program entry for wWinMainCRTStartup, you will get LNK2001 error message "unresolved external on _WinMain@16" when using Unicode and MFC. 3.Causes LNK2001 to occur on __imp__func; if not compiled using the /MD option, LNK2001 also occurs when connecting using MSVCxx.LIB. 4.When compiling with the /ML option, LNK2001 occurs on _errno if linked with LIBCMT.LIB. 5.When compiling a debug version of the application, LNK2001 is also generated if you use the distribution modal library for the connection; similarly, the same problem occurs when using a debug version of the modal library to connect to a distribution application.Word flags inline functions. 6.Mixing different versions of libraries and compilers can also cause problems because the new library may contain symbols and descriptions that were not available in earlier versions. 7.Using inline and non-inline compile options on different modules can result in LNK2001.If you create a C++ library with function inlining (/Ob1 or /Ob2), but close the function inlining (without the inline keyword) in the corresponding header file describing the function, you will get this error message.To avoid this problem, the inline key should be used in the corresponding header file.In fact, there are many reasons for LNK2001. The above reasons are only part of it. For beginners, these are enough to understand for a while.However, the purpose of analyzing the cause of the error is to avoid errors.Although the LNK2001 error is more difficult, as long as the above issues are noticed, it can be avoided and solved. 8.Incorrect /SUBSYSTEM or /ENTRY settings can also cause LNK2001. E.g:

This error does not have any hint at compile time, when you Buid you can't pass.

  The reason for this error is that no class Shape gives the body of the constructor and the destructor. The correct wording is as follows:



Unless otherwise specified, all articles on this site are original Baiyuan's Blog, in order to respect the author’s Labor achievements, please indicate the source https://wangbaiyuan.cn/en/error-lnk2001-unresolved-external-symbol-quot-public-thiscall-2.html if You think this article is useful to you, you can click on the "Sponsored Author" below the article to reward the author!


Post comment


No Comment


Forget password?