我有一个包含多个项目的解决方案。我有一个“主要”项目,作为一个菜单,从那里,用户可以访问任何其他项目。在这个主项目中,我得到了每个被调用函数的链接器错误。如何避免这些链接器错误?我已经在“Project Dependencies ...”中设置了项目依赖项。对话框。

由于

编辑 - 我按照建议做了,并将输出文件夹添加到链接器的其他目录中。但是,现在我得到了一百万个错误,如下所示:

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)
有帮助吗?

解决方案

在不知道有关您的解决方案的任何其他细节的情况下,很难说,但是Rebuild All可能会有所帮助。当存在针对不同体系结构的混合对象文件时,有时会发生这种情况。

您可能还想考虑使用“参考”和“参考”。而不是“依赖性”

修改

在您发布的内容之后,您与标准库的链接似乎不一致。可能是项目之一静态链接标准库而其他项目动态链接? (请参阅项目属性 - >链接器)或者一个到发布运行时,而其他人要调试? (虽然最后一个应该是可能的,但需要注意)

其他提示

如果不知道有关您的解决方案的所有细节,很难回答,但我会假设您的“其他”解决方案。设置项目以生成.lib文件,然后主项目链接所有这些lib文件。如果是这种情况,那么您获得的错误的可能原因是您的项目链接到运行时库的不同版本。

Microsoft的文档, “传递给链接器的给定调用的所有模块必须使用相同的运行时库编译器选项(/ MD,/ MT,/ LD)进行编译。”

尝试更改所有项目以使用相同版本的运行时库。在Visual Studio 2010中,这是在项目的属性中配置属性| C / C ++ |代码生成|运行时库。选择多线程调试DLL 多线程调试(用于调试配置)。

您的其他项目中的功能是否已导出?如果它们没有导出,则没有任何东西可以链接到主exe,因此会导致错误。请参阅使用__declspec导出DLL(导出)了解更多信息。

您获得的错误是“错误LNK2005”,其中您链接的库中的符号(msvcprtd.lib)已在另一个库中定义(例如panels.lib)。如果将panels.lib添加到“忽略特定库”字段(在VS2008配置属性 - >链接器 - >输入 - >忽略特定库)中,错误将停止。

我认为你得到的链接器错误是针对“未解析的符号”?

如果使用静态库(即.lib文件),则需要将库添加到链接器输入,以便在链接时可以链接符号。如果你不这样做,你将得到一个未解决的符号:

  1. 右键单击项目,然后选择“属性”。
  2. 选择配置属性 - >链接器 - >输入
  3. 在Additional Dependencies下输入库名称(例如filename.lib。

我遇到了一个非常类似的问题,结果是因为我的一个库有“将wchar_t作为内置类型”选项设置为与其他库不同的值。默认情况下,此项设置在新项目中为false。将它们全部更改为true可修复问题。

确切地说,至少有一个项目使用不同的“运行时库”进行编译。然后其他人。也许您使用默认值添加了一个新的。检查项目的属性是否具有相同的“配置属性”值。 - > " C / C ++" - > “代码生成” - > “运行时库”。

在我的情况下,问题是因为我在单个cpp文件中保留了函数原型,类定义和函数实现以及类实现。只有当我将声明和原型转移到单独的头文件时,这些错误才会消失。真奇怪!

就我而言,我有两种方法:

  1. 项目属性 - > C / C ++ - >代码生成 - >运行时库。将其更改为MT。

  2. 将您的缺席库添加到:链接器 - >输入 - >其他依赖项。如果它不起作用,请尝试添加完整路径而不是库名。

  3. 例如:C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ lib \ amd64 \ libvcruntime.lib;

    这对我有用。回答这个问题,任何人都需要帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top