继续生成链接器和库错误

时间:2013-08-27 14:16:30

标签: c++ visual-studio-2010 visual-c++ googletest

我尝试编译/构建谷歌测试样本,但是当我无法使样本工作时,我决定尝试使用简化器。我发现这个视频详细介绍了如何设置单元测试并使用google测试运行它们(但是从命令行开始):http://vimeo.com/53690756

但是,假设源代码正确实现并且我转到我的项目属性并将C / C ++的附加包含目录设置为g-test-1.6.0包含目录,并将链接器的附加库目录设置为相同的g- test-1.6.0包含目录。但是,Linker>输入>额外的依赖关系,我写了gtestd.lib。

我得到的唯一错误是101错误LNK1103:无法打开文件'gtestd.lib',我想我从视频中得到的是他们如何生成那个静态库文件?当我在之前的另一个项目中使用调试运行构建时,我能够在Debug下获得一个gtestd.lib文件,并且我试图将该文件包含在g-test-1.6.0 include目录下,但后来我得到了65个链接器错误。

基本上,我要问的是如何在Visual Studio 2010中设置谷歌测试而不会出现库或链接器错误?我在哪里可以获得正确版本的gtest.lib或gtestd.lib,我应该在哪里放置该库文件以便找到它?

以下是我收到的一些链接器错误:

Warning 65  warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other  libs; use /NODEFAULTLIB:library  C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LINK  Testproject99
Warning 66  warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LINK  Testproject99
Error   64  error LNK2005: _vprintf already defined in MSVCRTD.lib(MSVCR100D.dll)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(vprintf.obj)  Testproject99
Error   59  error LNK2005: _strtol already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(strtol.obj)   Testproject99
Error   58  error LNK2005: _sprintf_s already defined in MSVCRTD.lib(MSVCR100D.dll) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(sprintf.obj)  Testproject99
Error   63  error LNK2005: _printf already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(printf.obj)   Testproject99
Error   49  error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(crt0.obj) Testproject99
Error   45  error LNK2005: _localeconv already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(lconv.obj)    Testproject99
Error   44  error LNK2005: _fflush already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(fflush.obj)   Testproject99
Error   39  error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR100D.dll)  C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(crt0dat.obj)  Testproject99
Error   50  error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR100D.dll)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(winxfltr.obj) Testproject99
Error   62  error LNK2005: __vsnprintf_s already defined in MSVCRTD.lib(MSVCR100D.dll)  C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(vsnprnc.obj)  Testproject99
Error   48  error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR100D.dll)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(mlock.obj)    Testproject99
Error   60  error LNK2005: __strtoui64 already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(strtoq.obj)   Testproject99
Error   37  error LNK2005: __open_osfhandle already defined in MSVCRTD.lib(MSVCR100D.dll)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(osfinfo.obj)  Testproject99
Error   47  error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR100D.dll) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(mlock.obj)    Testproject99
Error   57  error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(invarg.obj)   Testproject99
Error   56  error LNK2005: __invalid_parameter already defined in MSVCRTD.lib(MSVCR100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\LIBCMTD.lib(invarg.obj)   Testproject99

Error   29  error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" (?_Locinfo_ctor@_Locinfo@std@@SAXPAV12@PBD@Z) already defined in msvcprtd.lib(MSVCP100D.dll)  C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\libcpmtd.lib(locale0.obj) Testproject99
Error   23  error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref@facet@locale@std@@QAEPAV123@XZ) already defined in gtestd.lib(gtest-all.obj) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   22  error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::operator<<(int)" (??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z) already defined in gtestd.lib(gtest-all.obj)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   3   error LNK2005: "public: __thiscall std::basic_iostream<char,struct std::char_traits<char> >::basic_iostream<char,struct std::char_traits<char> >(class std::basic_streambuf<char,struct std::char_traits<char> > *)" (??0?$basic_iostream@DU?$char_traits@D@std@@@std@@QAE@PAV?$basic_streambuf@DU?$char_traits@D@std@@@1@@Z) already defined in gtestd.lib(gtest-all.obj)  C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   32  error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP100D.dll)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\libcpmtd.lib(xlock.obj)   Testproject99
Error   31  error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) already defined in msvcprtd.lib(MSVCP100D.dll) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\libcpmtd.lib(xlock.obj)   Testproject99
Error   18  error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12@std@@QAE@XZ) already defined in gtestd.lib(gtest-all.obj)    C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   21  error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12@std@@QAE@XZ) already defined in gtestd.lib(gtest-all.obj) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   11  error LNK2005: "protected: void __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::setp(char *,char *,char *)" (?setp@?$basic_streambuf@DU?$char_traits@D@std@@@std@@IAEXPAD00@Z) already defined in gtestd.lib(gtest-all.obj)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   8   error LNK2005: "protected: void __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::setp(char *,char *)" (?setp@?$basic_streambuf@DU?$char_traits@D@std@@@std@@IAEXPAD0@Z) already defined in gtestd.lib(gtest-all.obj)   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\msvcprtd.lib(MSVCP100D.dll)   Testproject99
Error   27  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(MSVCP100D.dll) C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Testproject99\libcpmtd.lib(locale0.obj) Testproject99
Error   67  error LNK1169: one or more multiply defined symbols found   C:\Users\smorsi\Documents\Visual Studio 2010\Projects\Testproject99\Debug\Testproject99.exe Testproject99

我开始关注Chemistpp的步骤,当我向预处理器添加“_VARIADIC_MAX = 10”时,我在构建解决方案时收到了这些警告(但没有错误)。

Warning 108 warning MSB8012: TargetPath(C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md/Debug\gtest_unittest-md.exe) does not match the Linker's OutputFile property value (C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md\Debug\gtest_unittest.exe). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).  C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  990 6   gtest_unittest-md
Warning 106 warning MSB8012: TargetPath(C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md/Debug\gtest_prod_test-md.exe) does not match the Linker's OutputFile property value (C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md\Debug\gtest_prod_test.exe). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).    C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  990 6   gtest_prod_test-md
Warning 104 warning MSB8012: TargetPath(C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md/Debug\gtest_main-md.lib) does not match the Library's OutputFile property value (C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md\Debug\gtest_main-mdd.lib). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Lib.OutputFile).  C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  1151    6   gtest_main-md
Warning 102 warning MSB8012: TargetPath(C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md/Debug\gtest-md.lib) does not match the Library's OutputFile property value (C:\Users\smorsi\Desktop\gtest-1.6.0\gtest-1.6.0\msvc\gtest-md\Debug\gtestd.lib). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Lib.OutputFile).   C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  1151    6   gtest-md
Warning 109 warning MSB8012: TargetName(gtest_unittest-md) does not match the Linker's OutputFile property value (gtest_unittest). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).  C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  992 6   gtest_unittest-md
Warning 107 warning MSB8012: TargetName(gtest_prod_test-md) does not match the Linker's OutputFile property value (gtest_prod_test). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).    C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  992 6   gtest_prod_test-md
Warning 105 warning MSB8012: TargetName(gtest_main-md) does not match the Library's OutputFile property value (gtest_main-mdd). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Lib.OutputFile).  C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  1153    6   gtest_main-md
Warning 103 warning MSB8012: TargetName(gtest-md) does not match the Library's OutputFile property value (gtestd). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Lib.OutputFile).   C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets  1153    6   gtest-md

2 个答案:

答案 0 :(得分:3)

错误告诉您问题所在。它找不到gtest.lib。我刚刚提供了SSCCE来证明这确实有效。首先阅读gtest documentation并注意您需要从源代码构建库。这样做时,在使用MSVC时,您会遇到错误,因为MSVC不支持与variadics有关的事情?我使用了this solution,并在此处概述了不通过链接提供答案的精神。

第1步:下载gtest 1.6

第2步:提取到目录,DIR。

第3步:导航至DIR / msvc

步骤4:双击gtest-md.sln并根据需要进行迁移。你会得到一些警告。

步骤5:对于解决方案中的每个项目,右键单击并将其添加到C / C ++中的预处理器定义。 _VARIADIC_MAX=10

第6步:构建解决方案。

步骤7:导航到DIR/msvc/gtest-md/Debug/此文件夹包含gtestd.lib以及gtest-main-mdd.lib文件。我将Debug的全部内容粘贴到新文件夹DIR/lib中。

步骤8:创建一个新的空白溶胶和一个新的空白项目。添加一个新的空白.cpp。

第9步:转到Project->Properties->vc++ directories在其他包含库下添加DIR/Include,在其他库依赖项下添加DIR/lib

步骤10:转到Linker->Input并在其他依赖项下添加gtestd.lib

步骤11:转到C/C++->Preprocessor并在预处理器定义下添加_VARIADIC_MAX=10

第12步:在源添加

#include <gtest/gtest.h> //Or wherever your header file seems to be

int main() {
    return 0;
}

这就是我所做的,我为这个库创建了一个SSCCE。如果不是,那么您需要考虑是否错误地创建了.lib,或者没有将msvc指向库的正确目录。

答案 1 :(得分:3)

我最近为Visual Studio 2010构建了gtest1.6,并在遇到类似的错误后让它工作。

我希望我能更好地跟踪我的修复方法,但我很确定它只是设置C / C ++ - &gt;代码生成 - &gt;运行时库到/ MT用于gtest项目(gtest和gtest_main)和我自己的测试项目。请参阅此link

我没有改变任何与变量的关系。