Windows下用cmake和gtest问题总结

-- TOC --

cmake是个跨平台的工具,但各个平台有其特殊性。在Linux下,cmake生成Makefile,用后make。在Windows下,cmake生成sln和vcxproj文件,然后打开VS或使用devenv。

cmake基础

命令行编译

在Windows下,我们完全可以不用打开VS图形界面,即可完成目标的编译链接生成,因为有个devenv工具,可以在命令行编译sln工程。devenv工具在VS安装之后,就自动有了,请自行搜索一下,建议将devenv工具路径,加入系统Path环境变量。

比如:

> mkdir build
> cd build
> cmake ..
> devenv <xxxx.sln> /build "Debug|x64" [/project rbt]
> devenv <xxxx.sln> /build "Release|x64" [/project rbt]
> devenv <xxxx.sln> /rebuild "Release|x64" [/project rbt]
> devenv <xxxx.sln> /clean
> devenv /?

/build "Debug|x64"就是编译x64的Debug版本。

/project rbt,只编译sln中的rbt这个target。

在VS中调试

用VS打开sln文件,然后打开解决方案的属性页面,设置启动项目

运行库的链接方式

跟Linux下一样,MSVC也提供运行库的lib和DLL两个版本,而且也分Release和Debug版本,Debug版本文件的末尾,有个小d字母表示。

在VS界面,工程属性,C/C++下的代码生成,gtest项目默认使用MTdMT链接运行库。这是链接静态库,可能是因为gtest生成的也是静态库吧。

我遇到的问题是,使用gtest的代码,也用cmake来管理,生成的项目配置中,默认使用MDdMD链接运行库。这是链接DLL动态库。

这两者的不匹配,会导致链接出现问题:

2>------ 已启动全部重新生成: 项目: rbt, 配置: Release x64 ------
2>Building Custom Rule D:/temp/cmake_test/ring_buffer_template/CMakeLists.txt
2>utest.cpp
2>gtest.lib(gtest-all.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>gtest.lib(gtest-all.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: struct _Cvtvec __cdecl std::_Locinfo::_Getcvt(void)const " (?_Getcvt@_Locinfo@std@@QEBA?AU_Cvtvec@@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "protected: __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::basic_streambuf<char,struct std::char_traits<char> >(void)" (??0?$basic_streambuf@DU?$char_traits@D@std@@@std@@IEAA@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: virtual __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::~basic_streambuf<char,struct std::char_traits<char> >(void)" (??1?$basic_streambuf@DU?$char_traits@D@std@@@std@@UEAA@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char)" (?sputc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAAHD@Z) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: __int64 __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const *,__int64)" (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QEAA_JPEBD_J@Z) 已经 在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "protected: char * __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::_Pninc(void)" (?_Pninc@?$basic_streambuf@DU?$char_traits@D@std@@@std@@IEAAPEADXZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: virtual __cdecl std::basic_ios<char,struct std::char_traits<char> >::~basic_ios<char,struct std::char_traits<char> >(void)" (??1?$basic_ios@DU?$char_traits@D@std@@@std@@UEAA@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool)" (?setstate@?$basic_ios@DU?$char_traits@D@std@@@std@@QEAAXH_N@Z) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "protected: __cdecl std::basic_ios<char,struct std::char_traits<char> >::basic_ios<char,struct std::char_traits<char> >(void)" (??0?$basic_ios@DU?$char_traits@D@std@@@std@@IEAA@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: void __cdecl std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void)" (?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAXXZ) 已经在 gtest.lib(gtest-all.obj) 中 定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::basic_ostream<char,struct std::char_traits<char> >::operator<<(int)" (??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@H@Z) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::basic_ostream<char,struct std::char_traits<char> >::operator<<(unsigned __int64)" (??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@_K@Z) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::basic_ostream<char,struct std::char_traits<char> >::flush(void)" (?flush@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV12@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: __cdecl 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@@QEAA@PEAV?$basic_streambuf@DU?$char_traits@D@std@@@1@@Z) 已经在 gtest.lib(gtest-all.obj) 中定义
2>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: virtual __cdecl std::basic_iostream<char,struct std::char_traits<char> >::~basic_iostream<char,struct std::char_traits<char> >(void)" (??1?$basic_iostream@DU?$char_traits@D@std@@@std@@UEAA@XZ) 已经在 gtest.lib(gtest-all.obj) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(locale0.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(locale0.obj) : error LNK2005: "void __cdecl std::_Facet_Register(class std::_Facet_base *)" (?_Facet_Register@std@@YAXPEAV_Facet_base@1@@Z) 已经在 msvcprt.lib(locale0_implib.obj) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@CAPEAV_Locimp@12@XZ) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Init(bool)" (?_Init@locale@std@@CAPEAV_Locimp@12@_N@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" (?_Locinfo_ctor@_Locinfo@std@@SAXPEAV12@PEBD@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" (?_Locinfo_dtor@_Locinfo@std@@SAXPEAV12@@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Locimp::_New_Locimp(class std::locale::_Locimp const &)" (?_New_Locimp@_Locimp@locale@std@@CAPEAV123@AEBV123@@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(cout.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj  中)
2>libcpmtd.lib(cout.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(cerr.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj  中)
2>libcpmtd.lib(cerr.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(locale.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(locale.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(locale.obj) : error LNK2005: "private: static void __cdecl std::locale::_Locimp::_Locimp_Addfac(class std::locale::_Locimp *,class std::locale::facet *,unsigned __int64)" (?_Locimp_Addfac@_Locimp@locale@std@@CAXPEAV123@PEAVfacet@23@_K@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(iosptrs.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(iosptrs.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xlock.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xlock.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __cdecl std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QEAA@H@Z) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(xlock.obj) : error LNK2005: "public: __cdecl std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QEAA@XZ)  已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(wlocale.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(wlocale.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(wlocale.obj) : error LNK2005: "public: unsigned short const * __cdecl std::_Locinfo::_W_Getdays(void)const " (?_W_Getdays@_Locinfo@std@@QEBAPEBGXZ) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(wlocale.obj) : error LNK2005: "public: unsigned short const * __cdecl std::_Locinfo::_W_Getmonths(void)const " (?_W_Getmonths@_Locinfo@std@@QEBAPEBGXZ) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(xlocale.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xlocale.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xlocale.obj) : error LNK2005: "public: unsigned short const * __cdecl std::_Locinfo::_W_Getdays(void)const " (?_W_Getdays@_Locinfo@std@@QEBAPEBGXZ) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(xlocale.obj) : error LNK2005: "public: unsigned short const * __cdecl std::_Locinfo::_W_Getmonths(void)const " (?_W_Getmonths@_Locinfo@std@@QEBAPEBGXZ) 已经在 msvcprt.lib(MSVCP140.dll) 中定义
2>libcpmtd.lib(xstol.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstol.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xstoul.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstoul.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xstoll.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstoll.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xstoull.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstoull.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xmtx.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj  中)
2>libcpmtd.lib(xmtx.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xstrcoll.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstrcoll.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xdateord.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xdateord.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xwcscoll.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xwcscoll.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xwcsxfrm.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xwcsxfrm.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xgetwctype.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xgetwctype.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xtowlower.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xtowlower.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xtowupper.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xtowupper.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(xstrxfrm.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(xstrxfrm.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(StlCompareStringA.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0 ”(utest.obj 中)
2>libcpmtd.lib(StlCompareStringA.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹 配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(StlCompareStringW.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0 ”(utest.obj 中)
2>libcpmtd.lib(StlCompareStringW.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹 配值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(StlLCMapStringW.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(StlLCMapStringW.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配 值“MD_DynamicRelease”(utest.obj 中)
2>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“2”不匹配值“0”(utest.obj 中)
2>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配 值“MD_DynamicRelease”(utest.obj 中)
2>LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
2>libcpmtd.lib(wlocale.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>libcpmtd.lib(xlocale.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>gtest.lib(gtest-all.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>libcpmtd.lib(cout.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>libcpmtd.lib(cerr.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>libcpmtd.lib(locale.obj) : error LNK2001: 无法解析的外部符号 _invalid_parameter
2>gtest.lib(gtest-all.obj) : error LNK2019: 无法解析的外部符号 _CrtSetDbgFlag,函数 "public: __cdecl testing::internal::`anonymous namespace'::MemoryIsNotDeallocated::MemoryIsNotDeallocated(void)" (??0MemoryIsNotDeallocated@?A0x471195df@internal@testing@@QEAA@XZ) 中引用了该符号
2>gtest.lib(gtest-all.obj) : error LNK2019: 无法解析的外部符号 _calloc_dbg,函数 "char * __cdecl std::_Maklocstr<char>(char const *,char *,struct _Cvtvec const &)" (??$_Maklocstr@D@std@@YAPEADPEBDPEADAEBU_Cvtvec@@@Z) 中引用了该符号
2>libcpmtd.lib(locale.obj) : error LNK2001: 无法解析的外部符号 _calloc_dbg
2>libcpmtd.lib(wlocale.obj) : error LNK2001: 无法解析的外部符号 _calloc_dbg
2>libcpmtd.lib(xlocale.obj) : error LNK2001: 无法解析的外部符号 _calloc_dbg
2>libcpmtd.lib(StlCompareStringA.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(locale.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(wlocale.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(xlocale.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(xwcsxfrm.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>gtest.lib(gtest-all.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(locale0.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(cout.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(cerr.obj) : error LNK2001: 无法解析的外部符号 _free_dbg
2>libcpmtd.lib(StlCompareStringA.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(locale.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(wlocale.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(xlocale.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(xwcsxfrm.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>gtest.lib(gtest-all.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(locale0.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(cout.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(cerr.obj) : error LNK2001: 无法解析的外部符号 _malloc_dbg
2>libcpmtd.lib(wlocale.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>libcpmtd.lib(xlocale.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>gtest.lib(gtest-all.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>libcpmtd.lib(cout.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>libcpmtd.lib(cerr.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>libcpmtd.lib(locale.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReport
2>gtest.lib(gtest-all.obj) : error LNK2019: 无法解析的外部符号 _CrtSetReportFile,函数 "public: int __cdecl testing::UnitTest::Run(void)" (?Run@UnitTest@testing@@QEAAHXZ) 中引用了该符号
2>gtest.lib(gtest-all.obj) : error LNK2019: 无法解析的外部符号 _CrtSetReportMode,函数 "public: int __cdecl testing::UnitTest::Run(void)" (?Run@UnitTest@testing@@QEAAHXZ) 中引用了该符号
2>libcpmtd.lib(locale.obj) : error LNK2019: 无法解析的外部符号 _realloc_dbg,函数 "private: static void __cdecl std::locale::_Locimp::_Locimp_Addfac(class std::locale::_Locimp *,class std::locale::facet *,unsigned __int64)" (?_Locimp_Addfac@_Locimp@locale@std@@CAXPEAV123@PEAVfacet@23@_K@Z) 中引用了该符号
2>libcpmtd.lib(xstrcoll.obj) : error LNK2019: 无法解析的外部符号 _wcsdup_dbg,函数 _Getcoll 中引用了该符号
2>libcpmtd.lib(StlCompareStringA.obj) : error LNK2019: 无法解析的外部符号 _CrtDbgReportW,函数 __crtCompareStringA 中引用了该符号
2>libcpmtd.lib(StlLCMapStringA.obj) : error LNK2001: 无法解析的外部符号 _CrtDbgReportW
2>D:\temp\cmake_test\ring_buffer_template\build\Release\rbt.exe : fatal error LNK1120: 11 个无法解析的外部命令
2>已完成生成项目“rbt.vcxproj”的操作 - 失败。

看看这些头大的链接错误.....注意到检测到“RuntimeLibrary”的不匹配项,大约就应该猜到问题在哪里了!

而似乎,修改运行库链接方式的手段,貌似只能在VS界面中进行。特别注意,Release和Debug版本的链接方式,要分别配置!

Debug or Release

> devenv <xxxx.sln> /build "Debug|x64" [/project rbt]
> devenv <xxxx.sln> /build "Release|x64" [/project rbt]

而:

>cmake --fresh -DCMAKE_BUILD_TYPE=RELEASE ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.32.31332.0
-- The CXX compiler identification is MSVC 19.32.31332.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
## OS SYSTEM: Windows
## COMPILER ID: MSVC
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_BUILD_TYPE


-- Build files have been written to: D:/temp/cmake_test/ring_buffer_template/build

可以考虑自己写cmake脚本,使用CMAKE_BUILD_TYPE这个量,来对应不同的link路径。

设置路径

Windows下没有默认的include和link路径,因此,cmake脚本需要妥善处理这些路径问题。

我自己是把gtest先编译好,放到D盘根目录\gtest下,就不动了!

IF(${CMAKE_BUILD_TYPE} STREQUAL "RELEASE")
    LINK_DIRECTORIES(D:/gtest/lib/Release)
ELSE()
    LINK_DIRECTORIES(D:/gtest/lib/Debug)
ENDIF()

本文链接:https://cs.pynote.net/sf/win/202301091/

-- EOF --

-- MORE --