本篇文章介绍的是c运行时库以及与它们向关联的编译器选项和预处理指令。另外还有标准c++库的介绍:
(该文章采用的信息是来源于MSDN,Visual Studio 2010版本,信息是较新的,为的是不让大家在旧版本的信息上浪费时间,原文链接:http://msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.100%29.aspx )
This topic discusses the various .lib files that comprise the C run-time libraries as well as their associated compiler options and preprocessor directives.
C Run-Time Libraries (CRT)
The following libraries contain the C run-time library functions.
C run-time library (without iostream or standard C++ library) | Associated DLL | Characteristics | Option | Perprocessor directives |
libcmt.lib | none,static link | Multithreaded, static link | /MT | _MT |
msvcrt.lib | msvcr100.dll | Multithreaded, dynamic link (import library for MSVCR100.DLL). Be aware that if you use the Standard C++ Library, your program will need MSVCP100.DLL to run. | /MD | _MT,_DLL |
libcmtd.lib | none,static link | Multithreaded, static link (debug) | /MTd | _DEBUG,_MT |
msvcrtd.lib | msvcr100d.dll | Multithreaded, dynamic link (import library for MSVCR100D.DLL) (debug). | /MDd | _DEBUG,_MT,_DLL |
msvcmrt.lib | none,static link | C Runtime static library. Used for mixed managed/native code. | /clr /clr:oldSyntax | |
msvcutr.lib | None, static link | C Runtime static library compiled as 100% pure MSIL code. All code complies with the ECMA URT spec for MSIL. | /clr:pure |
Note
The single-threaded CRT (libc.lib, libcd.lib) (formerly the /ML or /MLd options) is no longer available. Instead, use the multithreaded CRT. See Multithreaded Libraries Performance .
If you link your program from the command line without a compiler option that specifies a C run-time library, the linker will use LIBCMT.LIB. This is different from previous versions of Visual C++ which used LIBC.LIB, the single-threaded library, instead.
如果你用命令行编译自己的程序并且没有指明CRT的库文件,连接器会默认采用LIBCMT.LIB。要注意这和以前vc++版本默认采用LIBC.LIB单线程链接库不同。
Using the statically linked CRT implies that any state information saved by the C runtime library will be local to that instance of the CRT. For example, if you use strtok, _strtok_l, wcstok, _wcstok_l, _mbstok, _mbstok_l when using a statically linked CRT, the position of the strtok parser is unrelated to the strtok state used in code in the same process (but in a different DLL or EXE) that is linked to another instance of the static CRT. In contrast, the dynamically linked CRT shares state for all code within a process that is dynamically linked to the CRT. This concern does not apply if you use the new more secure versions of these functions; for example, strtok_s does not have this problem.
采用静态链接意味着任何由C运行库产生的状态信息都会本地化的保存到该CRT运行实例里面。例如,你采用静态链接来使用这些函数strtok, _strtok_l, wcstok, _wcstok_l, _mbstok, mbstok_l ,那么,strtok函数在语法分析表中的位置与strtok函数在程序中声明的位置无关,与此相对比的是动态链接库的使用,所有的函数调用都会连接到动态的CRT中。如果采用安全性的函数就可以避免此类的问题。
Because a DLL built by linking to a static CRT will have its own CRT state, it is not recommended to link statically to the CRT in a DLL unless the consequences of this are specifically desired and understood. For example, if you call _set_se_translator in an executable that loads the DLL linked to its own static CRT, any hardware exceptions generated by the code in the DLL will not be caught by the translator, but hardware exceptions generated by code in the main executable will be caught.
If you are using the /clr compiler switch, your code will be linked with a static library, msvcmrt.lib. The static library provides a proxy between your managed code and the native CRT. You cannot use the statically linked CRT ( /MT or /MTd options) with /clr . Use the dynamically-linked libraries (/MD or /MDd ) instead.
If you are using the /clr:pure compiler switch, your code will be linked with the static library msvcurt.lib. As with /clr , you cannot link with the statically linked library.
For more information on using the CRT with /clr , see Mixed (Native and Managed) Assemblies ; for /clr:pure , see Pure and Verifiable Code .
To build a debug version of your application, the _DEBUG flag must be defined and the application must be linked with a debug version of one of these libraries. For more information about using the debug versions of the library files, see CRT Debugging Techniques .
This version of Visual C++ is not conformant with the C99 standard.
Standard C++ Library
Standard C++ Library | Characteristics | Option | Preprocess directives |
LIBCPMT.LIB | multithreaded,static link | /MT | _MT |
MSVCPRT.LIB | multithearded,dynamic link(import library from MSVCP100.DLL) | /MD | _MT,_DLL |
LIBCPMTD.LIB | multithreaded,static link | /MTd | _DEBUG,_MT |
MSVCPRTD.LIB | Multithreaded, dynamic link (import library for MSVCP100D.DLL) | /MDd | _DEBUG,_MT,_DLL |
Note LIBCP.LIB and LIBCPD.LIB (via the old /ML and /MLd options) have been removed. Use LIBCPMT.LIB and LIBCPMTD.LIB instead via the /MT and /MTd options.
When you build a release version of your project, one of the basic C run-time libraries (LIBCMT.LIB, MSVCMRT.LIB, MSVCRT.LIB) is linked by default, depending on the compiler option you choose (multithreaded, DLL, /clr). If you include one of the Header Files in your code, a Standard C++ Library will be linked in automatically by Visual C++ at compile time. For example:
#include <ios>
What is the difference between msvcrt.dll and msvcr100.dll?
The msvcrt.dll is now a "known DLL," meaning that it is a system component owned and built by Windows. It is intended for future use only by system-level components.
What problems exist if an application uses both msvcrt.dll and msvcr100.dll?
If you have a .lib or .obj file that needs to link to msvcrt.lib, then you should not have to recompile it to work with the new msvcrt.lib in Visual C++ 2010. The .lib or .obj file may rely on the sizes, field offsets, or member function names of various CRT classes or variables, and those should all still exist in a compatible way. When you relink against msvcrt.lib, your final EXE and DLL image will now have a dependency on msvcr100.dll instead of msvcrt.dll.
If you have more than one DLL or EXE, then you may have more than one CRT, whether or not you are using different versions of Visual C++. For example, statically linking the CRT into multiple DLLs can present the same problem. Developers encountering this problem with static CRTs have been instructed to compile with /MD to use the CRT DLL. Now that the CRT DLL has been renamed to msvcr100.dll, applications may have some components linked to msvcrt.dll and others to msvcr100.dll. If your DLLs pass CRT resources across the msvcrt.dll and msvcr100.dll boundary, you will encounter issues with mismatched CRTs and need to recompile your project with Visual C++ 2010.
If your program is using more than one version of the CRT, some care is needed when passing certain CRT objects (such as file handles, locales and environment variables) across DLL boundaries. For more information on the issues involved and how to resolve them, see Potential Errors Passing CRT Objects Across DLL Boundaries .