解决LNK4098:defaultlib“MSVCRT”与冲突
此警告:
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts
with use of other libs; use /NODEFAULTLIB:library
是 Visual Studio 中相当常见的警告。我想了解其确切原因以及处理它的正确方法(如果有的话)。
这出现在使用 /MDd
编译的调试版本中。该项目链接到 windows Version.dll
和 pdh.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
vc\lib 中存在 4 个版本的 CRT 链接库:
查看链接器选项、项目 + 属性、链接器、命令行。请注意,这里没有提及这些库。链接器会自动找出编译器使用的 /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:
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).
这意味着依赖的 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
IMO 此链接来自 Yochai Timmer 非常好且相关,但读起来很痛苦。我写了一个总结。
Yochai,如果您读过本文,请参阅末尾的注释。
对于原始帖子,请阅读:警告 LNK4098:defaultlib“LIBCD”与其他库的使用冲突
错误
含义
如何解决
在后者中,正如原始帖子中提到的那样,可能会出现两个常见问题:
对于这些情况,请确保您了解问题并在解决方案中做出决定。
注意:我想将 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
Meaning
How to resolve
In the latter, as it in mentioned in the original post, two common problems can arise :
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
每次我想用 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.
右键单击该项目,选择“属性”,然后选择“配置属性|”链接器|输入 |忽略特定库并写入msvcrtd.lib
Right-click the project, select Properties then under 'Configuration properties | Linker | Input | Ignore specific Library and write msvcrtd.lib
当项目中没有库被包含两次时,此警告已为我修复。就我而言,它位于带有
#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