解决LNK4098:defaultlib'MSVCRT'与...冲突 [英] Resolving LNK4098: defaultlib 'MSVCRT' conflicts with

查看:154
本文介绍了解决LNK4098:defaultlib'MSVCRT'与...冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此警告:

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

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

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.

这是一个调试版本,用 / MDd 。该项目与Windows Version.dll pdh.dll 之间的链接,它们链接到 MSVCRT.dll的。显然,我没有这些调试版本,无法编译它们。

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.

所以我添加了 / NODEFAULTLIB:MSVCRT 到链接器命令行,它实际上删除了警告。但这实际上是做什么的?为什么有必要?

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?

推荐答案

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

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


  • libcmt.lib:用于发布版本(/ MT)的静态CRT链接库

  • libcmtd.lib:用于调试构建的静态CRT链接库(/ MTd)

  • msvcrt.lib:CRT(/ MD)的版本DLL版本的导入库

  • msvcrtd.lib:用于调试DLL版本的CRT(/ MDd)的导入库

  • 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)

查看链接器选项项目+属性,链接器,命令行。请注意这些库中没有提到这些库。链接器自动找出编译器使用的/ M开关,以及哪个.lib应通过#pragma comment指令链接。 Kinda很重要,如果/ M选项与您链接的.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.

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

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 linked with /MD. There can be only one version of the CRT.

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

/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.

嗯,解决这个问题的正确方法,找到你正在链接的.obj或.lib文件,它是用错误的/ M选项编译的。如果你没有任何线索,那么你可以通过点击.obj / .lib文件/ MT找到它。

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:Windows可执行文件(如version.dll)有自己的CRT版本来完成他们的工作。它位于c:\windows\system32中,您无法将其可靠地用于您自己的程序,其CRT标题在任何地方都不可用。您的程序使用的CRT DLL具有不同的名称(如msvcrt90.dll)。

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).

这篇关于解决LNK4098:defaultlib'MSVCRT'与...冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆