Unresolved external symbol error with Google Mock and Vcpkg
Asked Answered
R

1

10

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.

Repudiate answered 3/9, 2017 at 16:19 Comment(2)
Add "GTEST_LINKED_AS_SHARED_LIBRARY" preprocessor definiton to your project. See : github.com/google/googletest/issues/292Brackett
Thanks @MuratŞeker. This worked!Repudiate
R
9

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.

Repudiate answered 30/1, 2019 at 14:50 Comment(1)
Library name must have changed at some point. The new paths are: $(VcpkgRoot)debug\lib\gtestd.lib; $(VcpkgRoot)debug\lib\gmockd.libZacek

© 2022 - 2024 — McMap. All rights reserved.