vcpkg不适用于谷歌测试

Mik*_*ail 5 c++ googletest visual-studio vcpkg

我安装并集成了最新版本的vcpkg:

e:\work\vcpkg>vcpkg version
Vcpkg package management program version 0.0.65-692a363701156f1bc319306fbde93fb6748325f6

See LICENSE.txt for license information.

e:\work\vcpkg>vcpkg integrate install
Applied user-wide integration for this vcpkg root.

All C++ projects can now #include any installed libraries.
Linking will be handled automatically.
Installing new libraries will make them instantly available.
Run Code Online (Sandbox Code Playgroud)

我安装了谷歌测试:

e:\work\vcpkg>vcpkg list
gtest:x64-windows           1.8              GoogleTest and GoogleMock testing frameworks.
gtest:x86-windows           1.8              GoogleTest and GoogleMock testing frameworks.
Run Code Online (Sandbox Code Playgroud)

gtest.h在Visual Studio 2015 Update 3中包含在我的项目中:

#include <gtest/gtest.h>
Run Code Online (Sandbox Code Playgroud)

编译很好,但我有链接器错误:

1>main.obj : error LNK2001: unresolved external symbol "void __cdecl testing::InitGoogleTest(int *,char * *)" (?InitGoogleTest@testing@@YAXPEAHPEAPEAD@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: int __cdecl testing::UnitTest::Run(void)" (?Run@UnitTest@testing@@QEAAHXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: static class testing::UnitTest * __cdecl testing::UnitTest::GetInstance(void)" (?GetInstance@UnitTest@testing@@SAPEAV12@XZ)
Run Code Online (Sandbox Code Playgroud)

显然,Visual Studio不知道它应该链接gtest.lib.我无法理解为什么.Vcpkg只说"链接将自动处理".不知道怎么做.

在我的项目的"附加库依赖项"中,我可以看到这些继承的值:

$(VcpkgRoot)lib
$(VcpkgRoot)lib\manual-link
Run Code Online (Sandbox Code Playgroud)

$(VcpkgRoot)决心e:\work\vcpkg\installed\x64-windows\.所以看起来集成是成功的.但Visual Studio如何知道它应该链接gtest.lib

请注意,如果我gtest.lib手动添加"附加依赖项",一切正常,并gtest.dll自动复制到输出目录.

小智 7

我认为已故意禁用自动链接行为gtest,请参阅vcpkg问题#306.关于这个问题的原始评论:这里.

vcpkg实现需要手动链接,因为Google Test可以重新定义main(),并且gtest功能在所有四个单独的库文件中都是重复的.
官方文件.

每个项目配置所需要的:
在:Configuration Properties> Linker> Input> Additional Dependencies
在释放建立:

$(VcpkgRoot)lib\manual-link\gtest_main.lib
Run Code Online (Sandbox Code Playgroud)

对于调试版本:

$(VcpkgRoot)debug\lib\manual-link\gtest_main.lib
Run Code Online (Sandbox Code Playgroud)

如果要创建自己的自定义main(),请替换gtest_main.libgtest.lib.
如果你想使用gmock,你可以用gmock_main.lib或替换它gmock.lib.

  • 谢谢,它有帮助。但是自从回答的那一刻起可能发生了一些变化,因为当前调试库的名称以“d”结尾 - “gtestd.lib”和“gtest_maind.lib”。 (2认同)