1 Tag: Link error
Link error Summary 1
When learning VC ++, we often encounter a link error lnk2001, Which is annoying because
For programmers, the best mistake to change is a compilation error. In general, when a connection error occurs,
Compilation is successful. There are many causes of connection errors, especially lnk2001 errors.
To understand why. If you do not thoroughly learn and understand VC ++, You need to correct the connection error lnk2001 non
It is often difficult.
When a beginner is learning VC ++, The lnk2001 error message is as follows:
Unresolved external symbol "symbol ".
If the connection program cannot find the referenced function, variable, or
Label to generate this error message. In general, there are two reasons for the error: one is the reference
The function or variable does not exist, the spelling is incorrect, or the usage is incorrect. Different versions may be used.
.
The following are possible causes of lnk2001 errors:
1. lnk2001 due to an Encoding Error.
1. mismatched program code or module definition files can cause lnk2001. For example,
If a variable "var1" is declared in the C ++ source file, it tries to use the variable
This error occurs when "var1" accesses this variable.
2. If the inline function is defined in the. cpp file, rather than in the header file
Meaning will cause lnk2001 error.
3. When a function is called, if the parameter types used do not match the type declared by the function
Lnk2001.
4. lnk2001 will occur when you try to call a virtual function from the constructor or destructor of the base class.
5. Pay attention to the availability of functions and variables. Only global variables and functions are public.
Static functions and static variables have the same scope of use. When attempting to access from outside the file
Any static variables not declared in this file will cause compilation errors or lnk2001.
The variables declared in the function can only be used within the range of the function.
The global constants of c ++ only have static connection performance. This is different from C. If you try
When you use global variables in multiple files, the lnk2001 error is also generated. One solution is
Add the initialization code of the constant to the header file and include the header file in the. cpp file.
The method is to assign a constant to the variable during use.
Ii. lnk2001 caused by compilation and link settings
1. If the/nod option is used during compilation, the program needs to run
The compiler writes the library and the MFC Library to the target file module during connection, unless explicitly included in the file
These database names. Otherwise, these databases will not be linked to the project file. In this case, use/nod to import
Error lnk2001.
2. If no program entry is set for wwinmaincrtstartup
The "unresolved external on _ winmain @ 16" lnk2001 error message is returned.
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 target file.
If you try to use the static library libc. lib or libcmt. Lib for connection, it will be sent to _ imp _ FUNC.
Lnk2001 is generated. If you do not use the/MD option for compilation, lnk2001 will also occur when you use msvcxx. Lib for connection.
4. When compiling with the/ml option, lnk2001 occurs on _ errno if libcmt. Lib is used.
5. When compiling and debugging applications, if you use the release modal library for connection
Generate lnk2001; similarly, when using the debug modal library to connect to the release application, the same
Problem.
6. Mixed use of libraries and compilers of different versions can also cause problems, because the library of the new version can
Can contain symbols and descriptions not available in earlier versions.
7. using inline and non-inline compilation options in different modules can lead to lnk2001. If
Function inline is enabled when the C ++ library is created, but the corresponding header of the function is described.
Function inline is disabled in the file, and the error message is displayed.
To avoid this problem, use the inline keyword to mark the inline function in the corresponding header file.
8. Incorrect/subsystem or/entry settings can also lead to lnk2001.
In fact, there are many reasons for lnk2001. The above reasons are only part of the problem.
Scholars can understand this for a while. However, the purpose of error analysis is to avoid errors.
Error occurs. Although the lnk2001 error is difficult, as long as you have noticed the above problems, you can still
Which can be avoided and solved.