VC + + often encounter a link error LNK2001, this error is very annoying, because for programmers, the best error to change is compiled errors, and generally occurs when the connection error,
Compilation has passed. There are many reasons for connection errors, especially LNK2001 errors, which are often unknown. If you do not deeply learn and understand VC + +, to correct the connection error LNK2001 very difficult.
Beginners in the process of learning VC + +, encountered LNK2001 error message mainly:
unresolved external symbol "symbol" (Indeterminate external "sign").
If the connector cannot find the referenced function, variable, or in all library and destination files
Label, this error message is generated. In general, there are two reasons for the error: one is the referenced
The function, the variable does not exist, the spelling is incorrect, or the use of the error, and then a different version may be used
's Connection library.
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
If you declare a variable "var1" within a C + + source file, you try to use a variable in another file
"VAR1" accesses the variable, this error occurs.
2. If you use an inline function that is in the. CPP file defined within, not in header file
Righteousness will cause LNK2001 errors.
3. When calling a function, if the type of parameter that is used does not match the type of the function declaration, it will produce a
LNK2001.
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. When attempting to access from outside the file
Any static variables that are not declared within the file will cause compilation errors 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 differs from C if an attempt is made in C + +
Using global variables within multiple files can also produce LNK2001 errors. One way to solve this problem is when you need to
Header file to include the initialization code for the constant in the. The CPP file contains the header file;
method is used to assign constants to the variable.
Two LNK2001 caused by compilation and link settings
1. If the/nod (/NODEFAULTLIB) option is used at compile time, the program needs to run
Libraries and MFC libraries 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. In this case the use of/nod will guide
Cause the error LNK2001.
2. If the program entry is not set for wWinMainCRTStartup, Unicode and MFC are used
You will get the LNK2001 error message for "unresolved external on _winmain@16".
3. When compiling with the/MD option, now that all the Run-time libraries are kept within the dynamic link library,
A reference to "Func" in the source file, which is a reference to "__imp__func" in the destination file.
If you attempt to connect using a static library LIBC.LIB or LIBCMT.LIB, you will be sent on the __imp__func
Birth LNK2001; If you do not compile with the/MD option, LNK2001 occurs when you use 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 release mode library will also produce
Raw LNK2001; Similarly, using the debug version of the modal library to connect to a release application also produces the same
Problem.
6. Mixed use of different versions of libraries and compilers can also cause problems, because the new version of the library can be
can contain symbols and instructions that were not in the previous version.
7. The use of inline and non inline compilation options in different modules can lead to LNK2001. If
A function inline (/ob1 or/OB2) was opened when the C + + library was created, but the corresponding header describing the function
In the file, the function inline (without the inline keyword) is closed, and the error message is obtained.
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.
In fact, there are many reasons for the LNK2001, the above reason is only part of the first
Scholars have understood this for a while. However, the purpose of analyzing the cause of the error is to avoid mistakes
Mistake occurred. Although it is difficult to LNK2001 errors, as long as the attention to the above problems, still can
enough to avoid and to be resolved.
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.