7

我安装并集成了最新版本的 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.

我安装了谷歌测试:

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.

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

#include <gtest/gtest.h>

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

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)

显然,Visual Studio 不知道它应该与gtest.lib. 我不明白为什么。Vcpkg 只说“链接将被自动处理”。不知道它将如何做到这一点。

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

$(VcpkgRoot)lib
$(VcpkgRoot)lib\manual-link

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

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

4

2 回答 2

8

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

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

每个项目所需的配置:
在:Configuration Properties> Linker> Input>Additional Dependencies
对于发布版本:

$(VcpkgRoot)lib\manual-link\gtest_main.lib

对于调试版本:

$(VcpkgRoot)debug\lib\manual-link\gtest_main.lib

如果要创建自己的自定义 main(),请替换gtest_main.libgtest.lib.
如果要使用 gmock,可以将其替换为gmock_main.libgmock.lib

于 2016-12-24T18:16:44.313 回答
0

这是一个旧线程,但我想指出我发现的内容。

您需要链接手动链接目录中的库,但您需要以正确的顺序链接它们。首先链接 gmock_main 然后 gtest_main。反过来只会导致 0 测试。

于 2020-11-15T17:51:08.833 回答