VCPKG不适用于Google测试



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

e:workvcpkg>vcpkg version
Vcpkg package management program version 0.0.65-692a363701156f1bc319306fbde93fb6748325f6
See LICENSE.txt for license information.
e:workvcpkg>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.

我安装了Google测试:

e:workvcpkg>vcpkg list
gtest:x64-windows           1.8              GoogleTest and GoogleMock testing frameworks.
gtest:x86-windows           1.8              GoogleTest and GoogleMock testing frameworks.

我在Visual Studio 2015更新3:

的项目中包括gtest.h
#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)libmanual-link

$(VcpkgRoot)已解决为e:workvcpkginstalledx64-windows。因此,整合似乎是成功的。但是Visual Studio如何知道它应该与gtest.lib链接?

请注意,如果我手动将gtest.lib添加到"其他依赖关系"中,则所有功能都可以正常工作,并且gtest.dll自动复制到输出目录。

我认为自动链接行为已故意禁用gtest,请参见VCPKG问题#306。有关此问题的原始评论:此处。

VCPKG实现需要手动链接,因为Google测试可以重新定义main(),并且在所有四个独立的库文件中都重复了GTEST功能。
官方文件。

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

$(VcpkgRoot)libmanual-linkgtest_main.lib

用于调试构建:

$(VcpkgRoot)debuglibmanual-linkgtest_main.lib

如果要创建自己的自定义main(),请用gtest.lib替换gtest_main.lib
如果要使用gmock,则可以用gmock_main.libgmock.lib替换。

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

您需要将LIB链接在手动链接目录中,但是您需要以正确的顺序链接它们。首先链接gmock_main然后gtest_main。另一种围绕的方式只会导致0个测试。

最新更新