Often encountered such a problem, here is some summary:
It's been a long time since I've written about the basics of programming, and this article is about the library link and the MSVC and the CRT.
If you're using a Linux, MAC, or other non-Windows platform, you can ignore this article, if you're using a Windows platform that is not written with Microsoft Visual Studio C + + (hereinafter referred to as MSVC) software c+ + program, this article may be of limited help to you, but if your operating system is Windows and you are using a program-integrated development environment that MSVC software to write C + + programs, this article should help you to clarify some important basic concepts.
As a programmer, in the process of learning programming, have you ever encountered certain error messages that seem to be unintelligible, but do not know how to solve them. For example, when you are happy to write the program, and verify that all the code can be successfully compiled, and then execute the "build program" (Build Solution) steps, the result ran a bunch of inexplicable errors:
LIBCMTD.lib (mlock.obj): Error LNK2005: __lock has been defined in MSVCRTD.lib (MSVCR80D.dll)
LIBCMTD.lib (mlock.obj): Error LNK2005: __unlock has been defined in MSVCRTD.lib (MSVCR80D.dll)
LIBCMTD.lib (crt0.obj): Error LNK2005: _maincrtstartup has been defined in MSVCRTD.lib (crtexe.obj)
............
Link:warning LNK4098: Default library ' MSVCRTD ' conflicts with other libraries used, use/nodefaultlib:library
Link:warning LNK4098: Default library ' LIBCMTD ' conflicts with other libraries used, use/nodefaultlib:library
D:\workspace\crtlibtest\debug\crtlibtest.exe:fatal error LNK1169: found one or more defined symbols
In general, if you have a library of third-party or open-source projects that are written in your program project, this error situation is more likely to occur. From these seemingly bizarre and confusing error messages, we can probably guess that the problem lies in the two libraries of LIBCMTD.lib and MSVCRTD.lib. But what exactly is LIBCMTD.lib and MSVCRTD.lib. Are these libraries used in our code?
The answer is yes.
Programmers familiar with C know that if you want to use the basic I/O operation functions of the C language, such as printf (), scanf (), or fopen (), you must first include the stdio.h header file in our code with #include syntax. The compiler (Compiler) is able to confirm that printf, scanf, fopen, and so on are legitimately available functions with function declaration in stdio.h for these I/O operation functions.
And when we write the code through the compiler output in the form of OBJ file, it is necessary to pass the linker (Linker) handler, the code in all of the functions defined (function definition) link is built to create a final program execution file. The problem is, we know that printf, scanf, and fopen's function declarations exist in the stdio.h, but the definition of these guys ' functions, that is, the real code of practice, is exactly where they are stored.
in the standard library of the C language.
The standard library developed by the C language is called the "execution Phase Library", which is the C run-timeLibrary, which is usually referred to as CRT. In the standard library of C, which contains a set of commonly used basic functions, such as I/O processing and string manipulation procedures, so long as we write code in C language, we must link the compiled code OBJ file to the C language Execution stage library, to produce a legitimate C language program execution file 。
The CRT is not only a single one by one version of the existence. In fact, apart from the use of "de-Error" and "release" for the purpose of two versions, the two can be derived from the "Static link" and "Dynamic Link" form:
static link : LIBCMTD.lib (Debug version) LIBCMT.lib
dynamic Link : MSVCRTD.lib (Debug version) MSVCRT.lib
Although these four CRT versions are used differently from each other, they all have a common feature, which is that they are all version of the library that can be safely used in multi-thread code, to meet the security requirements of a thread . In fact, in the previous version of MSVC 6, there were two other LIBCD.lib (except the wrong version) and the LIBC.lib Library, which was specifically intended for use by the single thread, but the two options were removed from the SET option since MSVC 2005, so now most The program designer uses a CRT version of the multiple threads.
In the behavior of library linking, the difference between static and dynamic connections is that when static links are used, the function definition of the library is embedded directly into the execution file, while the function definition of the library exists in another independent file, usually a DLL, when using a dynamic link. Format file, which is then posted to the user with the program execution file. Therefore, in the size of the file, the use of dynamically linked file files, usually more than the use of static link file files are smaller.
The advantage of using the dynamically linked CRT version is the ability to separate the standard libraries that are used frequently, and put them in the Windows system folder to reduce the size of the file files we've built. In turn, however, the disadvantage of using a dynamically linked CRT version lies in the DLL files that are attached to the execution file . For example, if the program constructs a Debug configuration execution file with MSVC 2005, the execution file needs to have a msvcr80d.dll presence in order to execute smoothly; If the Release is configured, it is dependent on msvcr80.dll. But if you build the same code on MSVC 2008, the resulting execution file is dependent on the Msvcr90d.dll and msvcr90.dll two different DLL files. different versions of the MSVC will have their own different dependent DLL files.
In the MSVC program project, how to specify whether the code should use a static link or a dynamically linked CRT version. In fact, it is very easy, just select the Code Generation sub-page in the "c/c++" page of the project properties, where there is a project for the runtime library, which is where the CRT Nexus version is set up in an ad hoc program. There are a total of four options, which correspond to the four different versions of the above static link and dynamic link. Multi-threaded debug (/MTD): Corresponds to LIBCMTD.lib multiple thread (/MT): LIBCMT.lib multiple thread debug DLL (/MDD): corresponding MSVCRTD.lib Multiple thread DLLs (/MD): corresponding MSVCRT.lib
If you start building a program without making any settings, the MSVC preset option will use the dynamically linked version.
The following are the main causes of LNK2005 errors:
1. The global variables are defined repeatedly. There are two possible scenarios:
A, for some novice programmers, sometimes think of the need to use global variables where you can use the definition of the declaration. In fact, this is wrong, the global variable is for the entire project. The correct should be defined in a CPP file as follows: int g_test; Then use the CPP file in use: extern int g_test, if you still use int g_test, then there will be LNK2005 error, general error message similar: A Aa.obj error LNK2005 int book C. already defined in Bbb.obj. Remember that you cannot assign a value to a variable or there will be a LNK2005 error.
What is needed here is "declaration", not "definition". According to the C + + standard, a variable is declared and must satisfy two conditions at the same time, otherwise it is defined as:
(1) The declaration must use the extern keyword; (2) cannot assign an initial value to a variable
So, here's the statement:
extern int A;
The following is a definition
int A; int a = 0; extern int a = 0;
B, for programmers who are not so rigorous in programming, always define a global variable in a file that needs to use a variable, and do not take into account the variable name, which is often prone to duplicate variable names and cause LNK2005 errors.
2. The header file contains duplicates. Often need to include the header file contains variables, functions, class definitions, in other areas of use have to be included more than once, if the header file does not have the relevant macros and other measures to prevent repeated links, then there will be a LNK2005 error. The workaround is to do similar processing in the header file that you need to include: #ifndef my_h_file//If this macro is not defined
#define MY_H_FILE//define this macro
....//header file main content
.......
#endif
The above is done using a macro, you can also use precompilation to do, in the header file to add:
#pragma once
Header file Body
3. caused by using a third-party library. This situation is mainly caused by the C run-time function library and the library Conflict of MFC. The way to do this is to put the wrong library in front of another library. This error can be caused by choosing a different C function library. Microsoft and C have two C run-time libraries, one is a common library of functions: LIBC.LIB, multithreading is not supported. Another type of support is Multithreading: MSVCRT.lib. If a project is mixed with these two libraries, it may cause this error, in general it requires MFC libraries to be linked before the C run-time library, so it is recommended to use msvcrt.lib that support multithreading. So before you use a third-party library, you first need to know what library it is linked to, or it may cause LNK2005 errors. If you have to use a third-party library, you can try to modify the method as described below, but there is no guarantee that the problem will be solved, the first two methods are provided by Microsoft:
A, select the VC menu Project->settings->link->catagory Select Input, and then in the Ignore Libraries edit column to fill in the library you need to ignore, such as: Nafxcwd.lib; Libcmtd.lib. Then in the Object/library Modules edit column fill in the correct library order, here need you can determine what is the correct order, oh, God bless you.
B, select the VC menu Project->settings->link page, and then enter/verbose:lib in the Edit tab of project options so that you can see the link order in the Output window during the compilation of the linker.
C, select the VC menu project->settings->c/c++ page, catagory Select code generation after the user runtime Libraray select multithread DLL and other libraries, each Try.
For a compiler related process, refer to: