c – 链接错误:已经在*****中定义了xxx.LIB ::究竟是什么问题?

问题:

我正在尝试使用名为DCMTK的库,该库使用了一些其他外部库(zlib,libtiff,libpng,libxml2,libiconv).我从同一个网站下载了这些外部库(* .LIB& * .h文件).现在,当我编译DCMTK库时,我收到链接错误(793错误),如下所示:

Error   2   error LNK2005: __encode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll)    LIBCMTD.lib dcmmkdir 
Error   3   error LNK2005: __decode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll)    LIBCMTD.lib dcmmkdir 
Error   4   error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D.dll)  LIBCMTD.lib dcmmkdir 
Error   5   error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib dcmmkdir 
Error   6   error LNK2005: __errno already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib dcmmkdir 
Error   7   error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR90D.dll)    LIBCMTD.lib dcmmkdir 
Error   8   error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR90D.dll)   LIBCMTD.lib dcmmkdir 

文档:

这似乎是这个库的一个流行错误,因此,他们确实有一个FAQ条目来解决这个问题,(http://forum.dcmtk.org/viewtopic.php?t=35)说:

  • The problem is that the linker tries to combine different,
    incompatible versions
    of the Visual
    C++ runtime library into a single
    binary.
  • This happens when not all parts of your project and the libraries you
    link against are generated with the
    same code generation options in Visual
    C++.
  • Do not use the /NODEFAULTLIB workaround, because strange software
    crashes may follow. Fix the problem!

  • DCMTK is by default compiled with the “Multithreaded” or “Multithreaded
    Debug” code generation option (the
    latter for Debug mode).

  • Either change the project settings of all of your code to use these code
    generation options,
  • or change the code generation for all DCMTK modules and re-compile.
  • MFC users beware: DCMTK should be compiled with “Multithreaded DLL” or
    “Multithreaded DLL Debug” settings if
    you want to link the libraries into an
    MFC application.

为他人解决同样的问题:

Huge Amount of Linker Issues with Release Build Only说:

It seems that your release build is
trying to link to something that was
built debug. You probably have a
broken dependency in your build, (or
you missed rebuilding something to
release by hand if your project is
normally built in pieces).

More technically, you seem to be
linking projects built with different
C Run Time library settings, one
with “Multi-Threaded”, another one
with “Multi-Threaded Debug”. Adjust
the settings for all the projects to
use the very same flavour of the
library and the issue should go away

问题:

到目前为止,我曾经认为Name mangling是唯一可能导致链接失败的问题,如果它没有被标准化的话.刚才我知道还有其他事情可能导致同样的效果.

>怎么了“调试模式”(多线程调试)和“释放模式”(多线程)?引擎盖下究竟发生了什么?为什么这件事导致链接错误?
>我想知道是否有一种叫做“单线程调试”和“单线程调试”的东西再次导致同样的事情.
>文档介绍了“代码生成选项”.什么代码生成选项?他们是吗?
>文档特别警告我们不要使用/ NODEFAULTLIB解决方法. (示例/ NODEFAULTLIB:msvcrt).为什么?我怎么会引起麻烦?到底是什么?
>请解释MFC用户文档中的最后一点.因为我将在本项目的后期使用MFC.解释我们为什么要这样做?如果我不这样做会导致什么麻烦.
>还有什么你想提的吗?我的意思是类似的错误.我对Linker&amp ;;非常感兴趣它的问题.所以,如果有类似的东西,你可以提及它们或至少一些关键词.

最佳答案

Whats up with the “Debug Mode”
(Multi-Threaded Debug) and “Release
Mode” (Multi-Threaded)? What exactly
is happening under the hood? Why
exactly this thing is causing linking
error?

由于几个不同的原因,链接器拖入库中.最简单的是,链接器命令行或链接器命令行上的链接器应答文件中列出了库.但是,无论是在项目中编译还是打包到库中,任何目标文件也可以包含linker options,包括请求链接请求特定库.实际上,Visual C编译器会自动嵌入与编译时使用的项目选项相匹配的链接器选项.

在链接时,所有目标文件和静态库文件中的对象的所有链接器选项都会合并在一起.如果请求了多个CRT库文件名,则链接器会读入所有这些文件名,并且它们会出现命名冲突,其中链接器不知道要使用哪个.

I wonder if there is something called
“Single-Threaded Debug” and
“Single-Threaded” which again causes
the same thing.

曾经有过,但Visual C的最后几个版本只提供了多线程兼容的库.

Documentation talks something about
“Code Generation Options”. What Code
Generation Options? WTH are they?

inside your project options.

Documentation specifically warns us
not to use /NODEFAULTLIB workaround.
(example /NODEFAULTLIB :msvcrt ). Why?
How would I cause troubles? what
exactly is it?

如果使用/ NODEFAULTLIB,则会忽略存储在目标文件和库中对象中的所有链接器设置.您最终将没有运行时库,也许会遗漏其他库.您可以手动添加它们,但它仍然是一个很大的混乱.

Please explain the last point in the
documentation for MFC users. Because
I’m going to use MFC later in this
project. Explain Why should we do it?
What troubles would it cause if I
don’t. Anything more you’d like to
mention? I mean regarding similar
errors. I’m very interested in Linker
& its problems. So, if there are any
similar things you can mentions them
or some keywords atleast.

MFC应用程序和MFC库必须使用相同的内存管理功能,以便MFC分配的内存可以被应用程序释放,反之亦然. FILE句柄和其他资源也是共享的. MFC DLL已经编译为在DLL中使用CRT,并且为了能够共享资源,您需要使用相同的CRT,这也意味着使用DLL.

转载注明原文:c – 链接错误:已经在*****中定义了xxx.LIB ::究竟是什么问题? - 代码日志