visual-studio Visual C++ 中多个项目之间的链接器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/604484/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linker errors between multiple projects in Visual C++
提问by rlbond
I have a solution with multiple projects. I have a "main" project, which acts as a menu and from there, the user can access any of the other projects. On this main project, I get linker errors for every function called. How do I avoid these linker errors? I set the project dependencies already in the "Project Dependencies..." dialog.
我有一个包含多个项目的解决方案。我有一个“主”项目,它充当菜单,用户可以从那里访问任何其他项目。在这个主项目中,每个调用的函数都会出现链接器错误。如何避免这些链接器错误?我已经在“项目依赖项...”对话框中设置了项目依赖项。
Thanks
谢谢
EDIT -- I did as suggested and added the output folder to the linker's additional directories. Now, however, I get a million errors as follows:
编辑——我按照建议做了并将输出文件夹添加到链接器的附加目录中。但是,现在我收到了一百万个错误,如下所示:
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __thiscall std::basic_ios >::setstate(int,bool)" (?setstate@?$basic_ios@DU?$char_traits@D@std@@@std@@QAEXH_N@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __thiscall std::ios_base::width(int)" (?width@ios_base@std@@QAEHH@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __thiscall std::basic_streambuf >::sputn(char const *,int)" (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEHPBDH@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static bool __cdecl std::char_traits::eq_int_type(int const &,int const &)" (?eq_int_type@?$char_traits@D@std@@SA_NABH0@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static int __cdecl std::char_traits::eof(void)" (?eof@?$char_traits@D@std@@SAHXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __thiscall std::basic_streambuf >::sputc(char)" (?sputc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEHD@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_streambuf > * __thiscall std::basic_ios >::rdbuf(void)const " (?rdbuf@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEPAV?$basic_streambuf@DU?$char_traits@D@std@@@2@XZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: char __thiscall std::basic_ios >::fill(void)const " (?fill@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEDXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __thiscall std::ios_base::flags(void)const " (?flags@ios_base@std@@QBEHXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: int __thiscall std::ios_base::width(void)const " (?width@ios_base@std@@QBEHXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: static unsigned int __cdecl std::char_traits::length(char const *)" (?length@?$char_traits@D@std@@SAIPBD@Z) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_ostream > & __thiscall std::basic_ostream >::flush(void)" (?flush@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV12@XZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::basic_ostream > * __thiscall std::basic_ios >::tie(void)const " (?tie@?$basic_ios@DU?$char_traits@D@std@@@std@@QBEPAV?$basic_ostream@DU?$char_traits@D@std@@@2@XZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: bool __thiscall std::ios_base::good(void)const " (?good@ios_base@std@@QBE_NXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __thiscall std::basic_ostream >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEXXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __thiscall std::basic_streambuf >::_Lock(void)" (?_Lock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEXXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __thiscall std::basic_streambuf >::_Unlock(void)" (?_Unlock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEXXZ) already defined in panels.lib(panel_main.obj)
3>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref@facet@locale@std@@QAEPAV123@XZ) already defined in panels.lib(panel_main.obj)
3>libcpmtd.lib(ios.obj) : error LNK2005: "private: static void __cdecl std::ios_base::_Ios_base_dtor(class std::ios_base *)" (?_Ios_base_dtor@ios_base@std@@CAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(ios.obj) : error LNK2005: "public: static void __cdecl std::ios_base::_Addstd(class std::ios_base *)" (?_Addstd@ios_base@std@@SAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "void __cdecl _AtModuleExit(void (__cdecl*)(void))" (?_AtModuleExit@@YAXP6AXXZ@Z) already defined in msvcprtd.lib(locale0_implib.obj)
3>libcpmtd.lib(locale0.obj) : error LNK2005: __Fac_tidy already defined in msvcprtd.lib(locale0_implib.obj)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static void __cdecl std::locale::facet::facet_Register(class std::locale::facet *)" (?facet_Register@facet@locale@std@@CAXPAV123@@Z) already defined in msvcprtd.lib(locale0_implib.obj)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@CAPAV_Locimp@12@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Init(void)" (?_Init@locale@std@@CAPAV_Locimp@12@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,class std::basic_string,class std::allocator > const &)" (?_Locinfo_ctor@_Locinfo@std@@SAXPAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" (?_Locinfo_dtor@_Locinfo@std@@SAXPAV12@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
3>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
采纳答案by EFraim
Without knowing any other detail about your solution it is hard to tell, however Rebuild All, might be helpful. This situation can sometimes occur when there are mixed object files for different architectures.
在不知道您的解决方案的任何其他细节的情况下,很难判断,但是全部重建可能会有所帮助。当不同体系结构有混合的目标文件时,有时会发生这种情况。
You might also want to consider using "References" instead of "Dependencies"
您可能还想考虑使用“参考”而不是“依赖项”
EDIT:
编辑:
After what you have posted it seems that your linkage to standard libraries is inconsistent. Could it be that one of the project links standard libraries statically while others dynamically? (See project properties->linker) Or one to the release runtime, while others to debug? (though the last one should be possible, with caveats)
在您发布的内容之后,您与标准库的链接似乎不一致。可能是其中一个项目静态链接标准库,而其他项目动态链接?(见项目属性->链接器)或者一个到发布运行时,而其他的去调试?(虽然最后一个应该是可能的,但需要注意)
回答by Dani van der Meer
It is difficult to answer without knowing all the details about your solution, but I will assume your "other" project are set up to produce a .lib file, and the main project then links all these lib files. If that is the case then a possible cause for the errors you are getting is that your projects link to different versions of the runtime library.
如果不了解有关您的解决方案的所有详细信息,很难回答,但我假设您的“其他”项目已设置为生成 .lib 文件,然后主项目链接所有这些 lib 文件。如果是这种情况,那么出现错误的可能原因是您的项目链接到不同版本的运行时库。
From Microsoft's documentation, "All modules passed to a given invocation of the linker must have been compiled with the same run-time library compiler option (/MD, /MT, /LD)."
从Microsoft 的文档中可以看出,“传递给给定链接器调用的所有模块都必须使用相同的运行时库编译器选项(/MD、/MT、/LD)进行编译。”
Try to change all the projects to use the same version of the runtime library. In Visual Studio 2010, this is in the Project's Properties | Configuration Properties | C/C++ | Code Generation | Runtime Library. Choose either Multi-threaded Debug DLLor Multi-threaded Debug(for your debug configuration).
尝试将所有项目更改为使用相同版本的运行时库。在 Visual Studio 2010 中,这是在项目的属性 | 配置属性 | C/C++ | 代码生成 | 运行时库。选择多线程调试 DLL或多线程调试(用于您的调试配置)。
回答by Andy
Are the functions in your other projects exported? If they aren't exported, then there is nothing to link to from the main exe, so that would cause the errors. See Exporting from a DLL Using __declspec(export)for more information.
您其他项目中的功能是否已导出?如果它们没有被导出,那么主 exe 就没有任何可链接的内容,因此会导致错误。有关详细信息,请参阅使用 __declspec(export) 从 DLL 导出。
回答by probably at the beach
The errors you are getting are 'error LNK2005' where a symbol in the library you are linking (msvcprtd.lib) has already been defined in another library (for example panels.lib). If you add panels.lib to the 'ignore specific library' field (in VS2008 Configuration Properties--> Linker--> Input -->Ignore specific libraries), the errors will stop.
您收到的错误是“错误 LNK2005”,其中您链接的库 (msvcprtd.lib) 中的符号已在另一个库(例如 panel.lib)中定义。如果将 panel.lib 添加到“忽略特定库”字段(在 VS2008 配置属性--> 链接器--> 输入 --> 忽略特定库中),错误将停止。
回答by the_mandrill
I had a very similar issue to this which turned out to be because one of my libraries had the option 'Treat wchar_t as built-in type' set to a different value to the others. This setting is false by default on new projects. Changing them all to true fixed the problem.
我有一个与此非常相似的问题,结果证明是因为我的一个库将选项“将 wchar_t 视为内置类型”设置为与其他库不同的值。默认情况下,此设置在新项目中为 false。将它们全部更改为 true 解决了问题。
回答by Dirk C.
Exactly, at least one of your projects is compiled with a different "Runtime Library" then the others. Maybe you added a new one using the default which is different. Check properties of your project(s) for equal value of "Configuration Properties" -> "C/C++" -> "Code Generation" -> "Runtime Library".
确切地说,您的项目中至少有一个是使用与其他项目不同的“运行时库”编译的。也许您使用不同的默认值添加了一个新的。检查项目的属性是否具有相同的“配置属性”->“C/C++”->“代码生成”->“运行时库”。
回答by Nav
In my case, the problem was because I had kept the function prototypes, the class definitions and the function implementation and the class implementation in a single cpp file. Only when I shifted the declarations and prototypes to a separate header file did these errors disappear. Really weird!
就我而言,问题是因为我将函数原型、类定义和函数实现以及类实现保存在单个 cpp 文件中。只有当我将声明和原型转移到单独的头文件时,这些错误才会消失。真的很奇怪!
回答by VictorV
In my case, I have two methods:
就我而言,我有两种方法:
- Project Properties -> C/C++ -> Code Generation -> Runtime Library.change it to MT. 
- Add your lack library to: Linker-> Input -> Additional Dependencies.If it doesn't work, try to add a full path instead of a library name. 
- 项目属性 -> C/C++ -> 代码生成 -> 运行时库。将其更改为 MT。 
- 将您的缺失库添加到:链接器-> 输入-> 附加依赖项。如果它不起作用,请尝试添加完整路径而不是库名称。 
For example: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64\libvcruntime.lib;
例如:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64\libvcruntime.lib;
That works for me.Answer this question for anyone needs help.
这对我有用。为任何需要帮助的人回答这个问题。
回答by LeopardSkinPillBoxHat
I presume the linker errors you are getting are for "unresolved symbols"?
我认为您收到的链接器错误是“未解析的符号”?
If using static libraries (i.e. .lib file), you will need to add the library to the linker input, so that at linkage time the symbols can be linked against. If you don't do this, you'll get an unresolved symbol:
如果使用静态库(即 .lib 文件),您需要将库添加到链接器输入,以便在链接时可以链接符号。如果你不这样做,你会得到一个未解析的符号:
- Right-click on the project, and select Properties.
- Select Configuration Properties->Linker->Input
- Enter the library name (e.g. filename.lib) under Additional Dependencies.
- 右键单击该项目,然后选择“属性”。
- 选择配置属性->链接器->输入
- 在附加依赖项下输入库名称(例如文件名.lib)。

