为什么运行时库是一个编译器选项而不是一个链接器选项?[英] Why is runtime library a compiler option rather than a linker option?

本文是小编为大家收集整理的关于为什么运行时库是一个编译器选项而不是一个链接器选项?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用Visual Studio 2005构建C ++静态库.由于选择运行时库是一个编译选项,因此我被迫构建库的四个变体,一个用于运行时的每个变体库:

  • /mt-静态运行时库
  • /md -dll运行时库
  • /mtd-调试静态运行时库
  • /mdd- debug dll运行时库

这些是编译器选项,而不是链接器选项.来自Linux背景,这似乎很奇怪.不同的运行时库是否有不同的呼叫约定?为什么不能在链接时间(即链接使用静态库的应用程序)时解决不同的运行时库?

推荐答案

C预处理器定义的一个副作用,例如_DLL和_DEBUG ZDAN提到的:

某些数据结构(例如STL容器和迭代器)在调试运行时的尺寸可能有所不同,这可能是由于_HAS_ITERATOR_DEBUGGING和_SECURE_SCL等功能所致.您必须使用结构定义与图书馆相兼容, '重新链接到.

如果将与不同的运行时库编译的对象文件混合和匹配,则会收到链接器警告,例如以下内容:

warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs

其他推荐答案

这些选项可能会添加定义(例如,__dll和__debug)在运行时库标头文件中使用.一件常见的事情是在动态链接时添加__declspec(dllimport)以函数声明.

编译器似乎还使用它们来帮助链接器链接到正确的库.这在 msdn .

其他推荐答案

编译器需要知道您是生成单线螺纹还是多线程代码.默认情况下,编译器生成线程安全代码(多线程).您必须告诉它是否需要单线程代码.如果更改默认值,则编译器更改默认运行时库(您可以始终在linker命令选项中覆盖此库,只需确保您选择的库具有相同的代码作为对象文件的结构:单线程静态,多线程静态或多线程DLL).请注意,没有单线程DLL选项(根据定义,运行时库DLL将作为线程安全构建,因为多个应用程序共享).

本文地址:https://www.itbaoku.cn/post/356223.html

问题描述

I'm trying to build a C/C++ static library using visual studio 2005. Since the selection of the runtime library is a compile option, I am forced to build four variations of my library, one for each variation of the runtime library:

  • /MT - static runtime library
  • /MD - DLL runtime library
  • /MTd - debug static runtime library
  • /MDd - debug DLL runtime library

These are compiler options, not linker options. Coming from a Linux background, this seems strange. Do the different runtime libraries have different calling conventions or something? Why can't the different runtime libraries be resolved at link time, i.e. when I link the application which uses my static library?

推荐答案

One side effect of the C preprocessor definitions like _DLL and _DEBUG that zdan mentioned:

Some data structures (such as STL containers and iterators) may be sized differently in the debug runtime, possibly due to features such as _HAS_ITERATOR_DEBUGGING and _SECURE_SCL. You must compile your code with structure definitions that are binary-compatible with the library you're linking to.

If you mix and match object files that were compiled against different runtime libraries, you will get linker warnings such as the following:

warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs

其他推荐答案

These options may add defines (__DLL and __DEBUG for example) that are used in the runtime library header files. One common thing to do is to add __declspec(dllimport) to function declarations when linked dynamically.

The compiler also seems to use these to assist the linker in linking to the correct libraries. This is explained in the MSDN.

其他推荐答案

The compiler needs to know if you are generating single threaded or multi-threaded code. By default the compiler generates thread safe code (multi-threaded). You have to tell it if you want single thread code.If you change the default the compiler changes the default run-time library (you can always override this in the linker command options, just be sure that the library you pick has the same code structure as your object files: single-threaded static, multi-threaded static or multi-threaded DLL). Note that there is no single-threaded DLL option (by definition the run-time library DLL will have been built as thread safe as it is shared by multiple apps).