【问题标题】:vcpkg does not work for google testvcpkg 不适用于谷歌测试
【发布时间】:2016-12-24 18:08:24
【问题描述】:

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

我在 Visual Studio 2015 Update 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)lib\manual-link

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

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

【问题讨论】:

    标签: c++ visual-studio googletest vcpkg


    【解决方案1】:

    我认为gtest 的自动链接行为已被有意禁用,请参阅vcpkg issue #306。 问题原评论:here.

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

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

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

    对于调试版本:

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

    如果您想创建自己的自定义 main(),请将 gtest_main.lib 替换为 gtest.lib
    如果你想使用gmock,可以用gmock_main.lib或者gmock.lib替换。

    【讨论】:

    • 所以推荐的链接gtest的方法就像我做的那样:实际上将它添加到“附加依赖项”,对吧?
    • 谢谢,它有帮助。但可能从回答的那一刻起发生了一些变化,因为目前调试库的名称以“d”结尾 - “gtestd.lib”和“gtest_maind.lib”。
    • 是的,自 2018 年 3 月 12 日以来,它已经有了,请参阅 PR #3013。自 2018 年 11 月 14 日(PR #4220)以来,只有*_main.lib 版本位于manual-link 文件夹中。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多