2

I tried compiling/building the google test samples but I decided to try something simplifier instead when I couldn't get the samples to work. I found this video that details how to set up the unit tests and run them using google test (but from the command line instead): http://vimeo.com/53690756

However, assuming the source code is correctly implemented and I went to my project's properties and set the C/C++'s Additional Include Directories to the g-test-1.6.0 include directory and the Linker's Additional library directories to the same g-test-1.6.0 include directory. However, Linker > Input > Additional Dependencies, I wrote gtestd.lib.

The only error I get is 101 error LNK1103: cannot open file 'gtestd.lib', I guess what I didn't get from the video is how they generated that static library file? When I ran the build using debug previously on another project, I was able to get a gtestd.lib file under Debug and I tried to include that file under the g-test-1.6.0 include directory but then I got 65 linker errors instead.

Essentially, what I'm asking is how to set up google test in Visual Studio 2010 without getting a library or linker error? Where do I get the proper version of gtest.lib or gtestd.lib and where should I place that library file so it's found?

Here are a few of the linker errors I'm receiving:

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

I started over following Chemistpp's steps and When I added "_VARIADIC_MAX=10" to Preprocessor, I got these warnings (but no errors) when building the solutions.

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
4

2 回答 2

3

错误告诉你问题所在。它找不到 gtest.lib。我刚刚提供了一个SSCCE来证明这确实有效。首先阅读gtest 文档并注意您需要从源代码构建库。这样做时,在使用 MSVC 时,您会遇到错误,因为 MSVC 不支持与可变参数有关的东西?我使用了这个解决方案,并将在此处概述它,而不是通过链接提供答案。

第 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 指向库的正确目录。

于 2013-08-27T14:49:11.587 回答
3

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

我希望我能更好地跟踪我为修复它所做的工作,但我很确定它只是将 C/C++ -> 代码生成 -> 运行时库设置为 /MT 用于 gtest 项目(gtest 和 gtest_main)和我自己的测试项目。请参阅此链接

我没有改变任何与可变参数有关的东西。

于 2013-08-27T20:24:03.853 回答