版 本 | 类 型 | 使用的library | 被忽略的library |
Release | 单线程 | libc.lib | libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib |
多线程 | libcmt.lib | libc.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib | |
使用DLL的多线程 | msvcrt.lib | libc.lib, libcmt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib | |
Debug | 单线程 | libcd.lib | libc.lib, libcmt.lib, msvcrt.lib, libcmtd.lib, msvcrtd.lib |
多线程 | libcmtd.lib | libc.lib, libcmt.lib, msvcrt.lib, libcmtd.lib, msvcrtd.lib | |
使用DLL的多线程 | msvcrtd.lib | libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib |
I find the following resource on the internet:
You are trying to link with incompatible libraries.
Important The run-time libraries now contain directives to prevent mixing different types. You’ll receive this warning if you try to use different types or debug and non-debug versions of the run-time library in the same program. For example, if you compiled one file to use one kind of run-time library and another file to use another kind (for example, single-threaded versus multithreaded) and tried to link them, you’ll get this warning. You should compile all source files to use the same run-time library. See the Use Run-Time Library (MD, /ML, /MT, /LD) compiler options for more information.
You can use the linker’s /VERBOSE:LIB switch to determine which libraries the linker is searching. If you receive LNK4098 and want to create an executable file that uses, for example, the single-threaded, non-debug run-time libraries(/ML或者是你指定的:Single-Threaded ), use the /VERBOSE:LIB option to find out which libraries the linker is searching. The linker should print LIBC.LIB and not LIBCMT.LIB, MSVCRT.LIB, LIBCD.LIB, LIBCMTD.LIB, or MSVCRTD.LIB as the libraries searched. You can tell the linker to ignore the the incorrect run-time libraries by typing the incorrect libraries in the Ignore Libraries text box on the Link tab of the Settings dialog box in Developer’s Studio or by using the /NODEFAULTLIB:library option with LINK for each library you want to ignore. See the Ignore Libraries (/NODEFAULTLIB) linker option for more information.
The table below shows which libraries should be ignored depending on which run-time library you want to use.
//下面的表中列出了:使用什么库的时候,你需要忽略掉什么库,否则就是LNK4098的错误
To use this run-time library | Ignore these libraries |
Single-threaded (libc.lib) | libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib |
Multithreaded (libcmt.lib) | libc.lib, msvcrt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib |
Multithreaded using DLL (msvcrt.lib) | libc.lib, libcmt.lib, libcd.lib, libcmtd.lib, msvcrtd.lib |
Debug Single-threaded (libcd.lib) | libc.lib, libcmt.lib, msvcrt.lib, libcmtd.lib, msvcrtd.lib |
Debug Multithreaded (libcmtd.lib) | libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, msvcrtd.lib |
Debug Multithreaded using DLL (msvcrtd.lib) | libc.lib, libcmt.lib, msvcrt.lib, libcd.lib, libcmtd.lib |
For example, if you received this warning and you want to create an executable file that uses the non-debug, single-threaded version of the run-time libraries, you could use the following options with the linker:
/NODEFAULTLIB:libcmt.lib
/NODEFAULTLIB:msvcrt.lib
/NODEFAULTLIB:libcd.lib
/NODEFAULTLIB:libcmtd.lib
/NODEFAULTLIB:msvcrtd.lib
==========================================================================================
/MD, /ML, /MT, /LD (Use Run-Time Library)
说明:上面各种指定给LINKER看的“符号”中的“M”表示“Module(模块)”之意
With these options, you can select either single-threaded or multithreaded run-time routines, indicate
that a multithreaded module is a dynamic-link library (DLL), and select the retail or debug version of
the library.
Note Having more than one copy of the run-time libraries in a process can cause problems, because
static data in one copy is not shared with the other copy. To ensure that your process contains only
one copy, avoid mixing static and dynamic versions of the run-time libraries. The linker will prevent
you from linking with both static and dynamic versions within one .EXE file, but you can still end up
with two (or more) copies of the run-time libraries. For example, a dynamic-link library linked with
the static (non-DLL) versions of the run-time libraries can cause problems when used with an .EXE file
that was linked with the dynamic (DLL) version of the run-time libraries. (You should also avoid mixing
the debug and non-debug versions of the libraries in one process.)
Command Line Project Settings Description /MD Multithreaded DLL Defines _MT and _DLL so that both multithread- and DLL-specific versions of the run-time routines are selected from the standard .H files. This option also causes the compiler to place the library name MSVCRT.LIB into the .OBJ file.
Applications compiled with this option are statically linked to MSVCRT.LIB. This library provides a layer of code that allows the linker to resolve external references. The actual working code is contained in MSVCRT.DLL, which must be available at run time to applications linked with MSVCRT.LIB. /MDd Debug Multithreaded DLL Defines _DEBUG, _MT, and _DLL so that debug multithread- and DLL-specific versions of the run-time routines are selected from the standard .H files. It also causes the compiler to place the library name MSVCRTD.LIB into the .OBJ file. /ML Single-Threaded Causes the compiler to place the library name LIBC.LIB into the .OBJ file so that the linker will use LIBC.LIB to resolve external symbols. This is the compiler’s default action. LIBC.LIB does not provide multithread support. /MLd Debug Single-Threaded Defines _DEBUG and causes the compiler to place the library name LIBCD.LIB into the .OBJ file so that the linker will use LIBCD.LIB to resolve external symbols. LIBCD.LIB does not provide multithread support. /MT Multithreaded Defines _MT so that multithread-specific versions of the run-time routines are selected from the standard header (.H) files. This option also causes the compiler to place the library name LIBCMT.LIB into the .OBJ file so that the linker will use LIBCMT.LIB to resolve external symbols. Either /MT or /MD (or their debug equivalents /MTd or /MDd) is required to create multithreaded programs. /MTd Debug Multithreaded Defines _DEBUG and _MT. Defining _MT causes multithread-specific versions of the run-time routines to be selected from the standard .H files. This option also causes the compiler to place the library name LIBCMTD.LIB into the .OBJ file so that the linker will use LIBCMTD.LIB to resolve external symbols. Either /MTd or /MDd (or their non-debug equivalents /MT or MD) is required to create multithreaded programs. /LD Not applicable Creates a DLL.
Passes the /DLL option to the linker. The linker looks for, but does not require, a DllMain function. If you do not write a DllMain function, the linker inserts a DllMain function that returns TRUE.
Links the DLL startup code.
Creates an import library (.LIB), if an export (.EXP) file is not specified on the command line; you link the import library to applications that call your DLL.
Interprets /Fe as naming a DLL rather than an .EXE file; the default program name becomes basename.DLL instead of basename.EXE.
Changes default run-time library support to /MT if you have not explicitly specified one of the /M options /LDd Not applicable Creates a debug DLL. Defines _DEBUG.
To find these options in the development environment, click Settings on the Project menu.
Then click the C/C++ tab, and click Code Generation in the Category box. See the
Use Run-Time Library drop-down box.
The debug options select the debug versions of the library or DLL and define _DEBUG.
For more information on using the debug versions, see C Run-Time Debug Libraries.
For further discussion of DLLs, see DLL Topics.
=====================================================================================
总结下:
(1)Single-Threaded----------------(/ML)
(2)Multithreaded------------------(/MT)
(3)Multithreaded------------------(/MD)
(4)Debug Single-Threaded----------(/MLd)
(5)Debug Multithreaded------------(/MTd)
(6)Debug Multithreaded DLL--------(/MDd)
======================================================================================
C Run-Time Debug Libraries
The following table lists the debug versions of the C run-time library files, along with their
associated compiler options and environment variables. For a list of the release versions of
these libraries, see C Run-Time Libraries.
Prior to Visual C++ 4.2, the C run-time libraries contained the iostream library functions.
In Visual C++ 4.2, the old iostream library functions were removed from LIBCD.LIB,
LIBCMTD.LIB, and MSVCRTD.LIB. This change was made because the Standard C++ library was added
to Visual C++, and it contains a new set of iostream libraries.
Two sets of iostream functions are now included in Visual C++. The old iostream functions now
exist in their own libraries: LIBCID.LIB, LIBCIMTD.LIB, and MSVCIRTD.LIB. The new iostream
functions, as well as many other new functions, exist in the Standard C++ libraries:
LIBCPD.LIB, LIBCPMTD.LIB, and MSVCPRTD.LIB.
The Standard C++ library and the old iostream library are incompatible and only one of them
can be linked with your project. See theStandard C++ Library Overview and
Make the Old iostream Library the Default for details.
When you build a debug version of your project, one of the basic C run-time debug libraries
(LIBCD.LIB, LIBCMTD.LIB, and MSVCRTD.LIB) is linked by default, depending on the compiler
option you choose (single-threaded, multithreaded, or DLL). Depending on the headers you use
in your code, a debug library from the Standard C++ libraries or one from the old iostream
libraries may also be linked.
- If you include aStandard C++ library header in your code, a Standard C++ library will be
- linked in automatically by Visual C++ at compile time. For example:
#include <ios>
- If you include an old iostream library header, an old iostream library will be linked in
- automatically by Visual C++ at compile time. For example:
#include <ios.h>
Note that headers from the Standard C++ library and the old iostream library cannot be mixed.
Headers determine whether a Standard C++ library, an old iostream library, or neither will be
linked. Compiler options determine which of the libraries to be linked is the default
(single-threaded, multithreaded, or DLL). When a specific library compiler option is defined,
that library is considered to be the default and its environment variables are automatically
defined.
C Run-Time Debug Library (without iostream) Characteristics Option Defined LIBCD.LIB Single-threaded, static link /MLd _DEBUG LIBCMTD.LIB Multithreaded, static link /MTd _DEBUG, _MT MSVCRTD.LIB Multithreaded, dynamic link
(import library for MSVCRTD.DLL)1 /MDd _DEBUG, _MT, _DLL 1 In place of the “x” in the DLL name, substitute the major version numeral of Visual C++ that you are using. For example, if you are using Visual C++ version 4, then the library name would be MSVCR40D.DLL.
Standard C++ Debug Library Characteristics Option Defined LIBCPD.LIB Single-threaded, static link /MLd _DEBUG LIBCPMTD.LIB Multithreaded, static link /MTd _DEBUG, _MT MSVCPRTD.LIB Multithreaded, dynamic link (import library for MSVCRTD.DLL) /MDd _DEBUG, _MT, _DLL
iostream Debug Library Characteristics Option Defined LIBCID.LIB Single threaded, static link /MLd _DEBUG LIBCIMTD.LIB Multithreaded, static link /MTd _DEBUG, _MT MSVCIRTD.LIB Multithreaded, dynamic link (import library for MSVCIRTD.DLL) /MDd _DEBUG, _MT, _DLL
The debug versions of the library functions differ from the release versions mainly in that
debug information was included when they were compiled (using the /Z7 or /Zi compiler option),
optimization was turned off, and source code is available. A few of the debug library functions
also contain asserts that verify parameter validity.
Using one of these debug libraries is as simple as linking it to your application with the
/DEBUG:FULL linker option set. You can then step directly into almost any run-time function
call.