Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vcpkg does not work for google test

I installed and integrated latest version of 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.

I installed google test:

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.

I included gtest.h in my project in Visual Studio 2015 Update 3:

#include <gtest/gtest.h>

It compiles fine, but I have linker errors:

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)

Apparently, Visual Studio does not know it should link with gtest.lib. And I cannot understand why. Vcpkg only says that "Linking will be handled automatically." No idea how it is going to do this.

In "Additional Library Dependencies" of my project I can see these inherited values:

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

And $(VcpkgRoot) is resolved to e:\work\vcpkg\installed\x64-windows\. So it seems like integration was successful. But how Visual Studio knows it should link with gtest.lib?

Note that if I add gtest.lib to "Additional Dependencies" manually, all works fine, and gtest.dll is automatically copied to output directory.

like image 909
Mikhail Avatar asked Dec 24 '16 18:12

Mikhail


1 Answers

I think that the autolinking behavior has been intentionally disabled for gtest, see vcpkg issue #306. Original comment on the issue: here.

The vcpkg implementation requires manual linking because Google Test can redefine main(), and the gtest functionality is duplicated in all of the four separate library files.
Official documentation.

The required per project configuration:
In: Configuration Properties > Linker > Input > Additional Dependencies
For release-builds:

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

For debug-builds:

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

If you want to create your own custom main(), replace gtest_main.lib with gtest.lib.
If you want to use gmock, you can replace it with gmock_main.lib or gmock.lib.

like image 102
traversaro Avatar answered Sep 19 '22 13:09

traversaro