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

查看:16
本文介绍了解决 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.dllpdh.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?

推荐答案

vclib 中有 4 个版本的 CRT 链接库:

There are 4 versions of the CRT link libraries present in vclib:

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

查看链接器选项、项目 + 属性、链接器、命令行.请注意这里没有提到这些库.链接器会自动确定编译器使用了什么/M 开关,以及应该通过 #pragma 注释指令链接哪个 .lib.有点重要,如果/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 注释指令.这可能会奏效,尽管许多其他链接器错误并不少见.像 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 选项编译的.如果您不知道,那么您可以通过搜索/MT"的 .obj/.lib 文件来找到它

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"

顺便说一句:Windows 可执行文件(如 version.dll)有自己的 CRT 版本来完成他们的工作.它位于 c:windowssystem32,你不能可靠地将它用于你自己的程序,它的 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:windowssystem32, 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天全站免登陆