Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved external symbol error with Google Mock and Vcpkg

Tags:

c++

gmock

vcpkg

I have created a simple C++ test project with one mocking class:

#include <gtest/gtest.h>
#include <gmock/gmock.h>

class TestMock
{
public:
    MOCK_CONST_METHOD0(Method1, void());
};

TEST(Test, Test1)
{
    TestMock mock;
}

int main(int argc, char * argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

I use Visual Studio 2017 (15.3.3). This is a x86 debug project with all default settings.

I have added gtest.lib and gmock.lib to linker. When I compile and link, I get this error:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_linked_ptr_mutex" (?g_linked_ptr_mutex@internal@testing@@3VMutex@12@A)
1>ConsoleApplication1.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_gmock_mutex" (?g_gmock_mutex@internal@testing@@3VMutex@12@A)
1>d:\Develop\CPP\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 2 unresolved externals
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have downloaded the gtest package with Vcpkg. It is compiled as a 32-bit DLL. Gtest is working fine, but when I instantiate the mock class I get the linking error. I have also tested with a CMake project and I get the same error.

like image 488
Hein Gustavsen Avatar asked Sep 03 '17 16:09

Hein Gustavsen


1 Answers

Taken from Murat Şeker's comment:

Add "GTEST_LINKED_AS_SHARED_LIBRARY" preprocessor definiton to your project. See : github.com/google/googletest/issues/292

This worked for me.

like image 158
Hein Gustavsen Avatar answered Oct 21 '22 04:10

Hein Gustavsen