vcpkg does not work for google test

7.6k views Asked by At

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.

2

There are 2 answers

3
traversaro On BEST ANSWER

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.

0
Sackzement On

This is an old thread but I would like to point out what I've found.

You need to link the libs in the manual-link directory, but you need to link them in the right order. First link gmock_main then gtest_main. The other way around just results in 0 test.