解决LNK4098:defaultlib“MSVCRT”与冲突

发布于 2024-09-05 11:55:54 字数 457 浏览 4 评论 0原文

此警告:

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts
  with use of other libs; use /NODEFAULTLIB:library

是 Visual Studio 中相当常见的警告。我想了解其确切原因以及处理它的正确方法(如果有的话)。

这出现在使用 /MDd 编译的调试版本中。该项目链接到 windows Version.dllpdh.dll 等内容,它们本身又链接 MSVCRT.dll。显然,我没有这些的调试版本,无法编译它们。

因此,我将 /NODEFAULTLIB:MSVCRT 添加到链接器命令行,它实际上确实删除了警告。但这实际上有什么作用呢?为什么有必要?

This warning:

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts
  with use of other libs; use /NODEFAULTLIB:library

is a fairly common warning in Visual Studio. I'd like to understand the exact reason for it and the right way (if at all) to handle it.

This comes up in a debug build, compiled with /MDd. The project is linked to things like windows Version.dll and pdh.dll which themselves link with MSVCRT.dll. Obviously, I don't have the debug versions of these and can't compile them.

So I added /NODEFAULTLIB:MSVCRT to the linker command line and it actually did remove the warning. But what does this actually do? And why is it necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小情绪 2024-09-12 11:55:54

vc\lib 中存在 4 个版本的 CRT 链接库:

  • libcmt.lib:用于发布版本 (/MT) 的静态 CRT 链接库
  • libcmtd.lib:用于调试版本 (/MTd) 的静态 CRT 链接库
  • msvcrt.lib :CRT 的发布 DLL 版本的导入库 (/MD)
  • msvcrtd.lib:CRT 的调试 DLL 版本的导入库 (/MDd)

查看链接器选项、项目 + 属性、链接器、命令行。请注意,这里没有提及这些库。链接器会自动找出编译器使用的 /M 开关以及应通过 #pragma comment 指令链接哪个 .lib。有点重要的是,如果 /M 选项和链接的 .lib 之间不匹配,您会遇到可怕的链接错误,并且很难诊断运行时错误。

当链接器被告知链接到 msvcrt.lib libcmt.lib 时,您将看到引用的错误消息。如果将使用 /MT 编译的代码与使用 /MD 编译的代码链接起来,就会发生这种情况。无效,CRT 只能有一种版本。

/NODEFAULTLIB 告诉链接器忽略从 /MT 编译代码生成的 #pragma comment 指令。尽管大量其他链接器错误并不罕见,但这可能会起作用。像 errno 这样的东西,在静态 CRT 版本中是 extern int,但在 DLL 版本中宏化为函数。许多其他人也喜欢这样。

好吧,用正确的方法解决这个问题,找到您正在链接的 .obj 或 .lib 文件,该文件是使用错误的 /M 选项编译的。如果您不知道,那么您可以通过 grep .obj/.lib 文件中的“/MT”来找到它

顺便说一句:Windows 可执行文件(如 version.dll)有自己的 CRT 版本来完成其工作。它位于 c:\windows\system32 中,您不能可靠地将它用于您自己的程序,它的 CRT 标头在任何地方都不可用。您的程序使用的 CRT DLL 有不同的名称(如 msvcrt90.dll)。

There are 4 versions of the CRT link libraries present in vc\lib:

  • libcmt.lib: static CRT link library for a release build (/MT)
  • libcmtd.lib: static CRT link library for a debug build (/MTd)
  • msvcrt.lib: import library for the release DLL version of the CRT (/MD)
  • msvcrtd.lib: import library for the debug DLL version of the CRT (/MDd)

Look at the linker options, Project + Properties, Linker, Command Line. Note how these libraries are not mentioned here. The linker automatically figures out what /M switch was used by the compiler and which .lib should be linked through a #pragma comment directive. Kinda important, you'd get horrible link errors and hard to diagnose runtime errors if there was a mismatch between the /M option and the .lib you link with.

You'll see the error message you quoted when the linker is told both to link to msvcrt.lib and libcmt.lib. Which will happen if you link code that was compiled with /MT with code that was compiled with /MD. Not valid, there can be only one version of the CRT.

/NODEFAULTLIB tells the linker to ignore the #pragma comment directive that was generated from the /MT compiled code. This might work, although a slew of other linker errors is not uncommon. Things like errno, which is a extern int in the static CRT version but macro-ed to a function in the DLL version. Many others like that.

Well, fix this problem the Right Way, find the .obj or .lib file that you are linking that was compiled with the wrong /M option. If you have no clue then you could find it by grepping the .obj/.lib files for "/MT"

Btw: the Windows executables (like version.dll) have their own CRT version to get their job done. It is located in c:\windows\system32, you cannot reliably use it for your own programs, its CRT headers are not available anywhere. The CRT DLL used by your program has a different name (like msvcrt90.dll).

日暮斜阳 2024-09-12 11:55:54

这意味着依赖的 dll 之一是使用不同的 运行时编译的库

项目->属性-> C/C++->代码生成->运行时库

查看所有库,发现它们的编译方式相同。

有关此错误的更多信息,请参阅此链接:

警告 LNK4098:defaultlib“LIBCD”与使用冲突其他库的

It means that one of the dependent dlls is compiled with a different run-time library.

Project -> Properties -> C/C++ -> Code Generation -> Runtime Library

Go over all the libraries and see that they are compiled in the same way.

More about this error in this link:

warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs

倾城°AllureLove 2024-09-12 11:55:54

IMO 此链接来自 Yochai Timmer 非常好且相关,但读起来很痛苦。我写了一个总结。

Yochai,如果您读过本文,请参阅末尾的注释。


对于原始帖子,请阅读:警告 LNK4098:defaultlib“LIBCD”与其他库的使用冲突

错误

LINK:警告 LNK4098:defaultlib“LIBCD”与其他库的使用冲突;使用 /NODEFAULTLIB:库

含义

系统的一部分被编译为使用带有静态链接的调试信息 (libcd) 的单线程标准 (libc) 库

而系统的另一部分被编译为使用没有调试信息的多线程标准库,该库驻留在 DLL 中并使用动态链接

如何解决

  • 忽略该警告,毕竟它只是一个警告。但是,您的程序现在包含相同函数的多个实例。

  • 使用链接器选项 /NODEFAULTLIB:lib。这不是一个完整的解决方案,即使您可以让您的程序以这种方式链接,您也会忽略一个警告信号:代码已针对不同的环境进行编译,您的某些代码可能会针对单线程模型进行编译,而其他代码可能会针对单线程模型进行编译。多线程。

  • [...] 搜索所有库并确保它们具有正确的链接设置

在后者中,正如原始帖子中提到的那样,可能会出现两个常见问题:

  • 您有一个第三方库,该库与您的应用程序的链接方式不同。

  • 您的代码中还嵌入了其他指令:通常这是 MFC。如果系统中的任何模块链接到 MFC,则所有模块名义上都必须链接到相同版本的 MFC。

对于这些情况,请确保您了解问题并在解决方案中做出决定。


注意:我想将 Yochai Timmer 链接的摘要包含到他自己的答案中,但由于有些人无法正确审查编辑,因此我不得不将其写在单独的答案中。抱歉

IMO this link from Yochai Timmer was very good and relevant but painful to read. I wrote a summary.

Yochai, if you ever read this, please see the note at the end.


For the original post read : warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs

Error

LINK : warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs; use /NODEFAULTLIB:library

Meaning

one part of the system was compiled to use a single threaded standard (libc) library with debug information (libcd) which is statically linked

while another part of the system was compiled to use a multi-threaded standard library without debug information which resides in a DLL and uses dynamic linking

How to resolve

  • Ignore the warning, after all it is only a warning. However, your program now contains multiple instances of the same functions.

  • Use the linker option /NODEFAULTLIB:lib. This is not a complete solution, even if you can get your program to link this way you are ignoring a warning sign: the code has been compiled for different environments, some of your code may be compiled for a single threaded model while other code is multi-threaded.

  • [...] trawl through all your libraries and ensure they have the correct link settings

In the latter, as it in mentioned in the original post, two common problems can arise :

  • You have a third party library which is linked differently to your application.

  • You have other directives embedded in your code: normally this is the MFC. If any modules in your system link against MFC all your modules must nominally link against the same version of MFC.

For those cases, ensure you understand the problem and decide among the solutions.


Note : I wanted to include that summary of Yochai Timmer's link into his own answer but since some people have trouble to review edits properly I had to write it in a separate answer. Sorry

北方的韩爷 2024-09-12 11:55:54

每次我想用 VC++ 创建应用程序时都会得到这个。

右键单击该项目,选择“属性”,然后选择“配置属性|” C/C++ |代码生成',选择“多线程调试(/MTd)”进行调试配置。

请注意,这不会更改您的发布配置的设置 - 您需要转到同一位置并为发布选择“多线程 (/MT)”。

I get this every time I want to create an application in VC++.

Right-click the project, select Properties then under 'Configuration properties | C/C++ | Code Generation', select "Multi-threaded Debug (/MTd)" for Debug configuration.

Note that this does not change the setting for your Release configuration - you'll need to go to the same location and select "Multi-threaded (/MT)" for Release.

时光暖心i 2024-09-12 11:55:54

右键单击该项目,选择“属性”,然后选择“配置属性|”链接器|输入 |忽略特定库并写入msvcrtd.lib

Right-click the project, select Properties then under 'Configuration properties | Linker | Input | Ignore specific Library and write msvcrtd.lib

去了角落 2024-09-12 11:55:54

当项目中没有库被包含两次时,此警告已为我修复。就我而言,它位于带有 #pragma comment 的源文件中,并且在项目属性页中:链接器 ->输入->附加依赖项

This warning was fixed for me when no library in the project was included twice. In my case it was in the source file with #pragma comment, and in project property page: Linker -> Input -> Additional Dependencies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文