google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.98k stars 10.18k forks source link

gtest and gmock as shared dlls on windows - latest master linking errors gmock #2535

Open jmecosta opened 5 years ago

jmecosta commented 5 years ago

hi,

i get these linking errors:

3>NumberingTestFixture.obj : error LNK2001: unresolved external symbol "class testing::internal::Mutex testing::internal::g_gmock_mutex" (?g_gmock_mutex@internal@testing@@3VMutex@12@A) 3>NumberingTestFixture.obj : error LNK2001: unresolved external symbol "class testing::internal::ThreadLocal<class testing::Sequence *> testing::internal::g_gmock_implicit_sequence" (?g_gmock_implicit_sequence@internal@testing@@3V?$ThreadLocal@PEAVSequence@testing@@@12@A) 3>D:\prod\structures\BuildDrop\Work\bin_x64\NumberingTest.exe : fatal error LNK1120: 2 unresolved externals

class MockDatabaseHelper_i : public numbering::DatabaseHelper_i { public: MOCK_METHOD1(GetLock, numbering::SharedLock_c(dbMultiUserLockRetryCallback_t LockRetryCallback)); MOCK_METHOD0(AreEnoughPrivileges, bool()); MOCK_CONST_METHOD0(IsServerConnectionOk, bool()); };

Using: MockDatabaseHelper_i Db; EXPECTCALL(Db, GetLock(::testing::)).Times(1).WillOnce(::testing::Return(numbering::SharedLock_c(new Lock_c())));

I did generate the mock with gmock generator... the funny part is that we have tons of gmock code in other projects that compile and link just fine.

Do you have any idea whats going on here?

thanks

Knitschi commented 5 years ago

I am having the same problem. I think the core problem is the "unconventional" usage of the GTEST_API_ macro.

There should be a different macro for each of the four libraries gtest, gtest_main, gmock and gmock_main. Then you would add CMake function calls for each of the four targets like this

target_compile_definitions(<target> PRIVATE <target>_CREATE_SHARED_LIBRARY)
target_compile_definitions(<target> INTERFACE <target>_LINKED_AS_SHARED_LIBRARY)

Then clients get the correct macros by only linking to one of the targets. You can even use CMake to generate the export macro header for you.

With the current way you have to manually call target_compile_definitions(<your_target> PUBLIC GTEST_LINKED_AS_SHARED_LIBRARY) for each of your targets that link with gtest.

cmdremily commented 2 years ago

I'm also running into this after gmock initially linking and running fine.

Shawn1874 commented 8 months ago

I'm attempting to upgrade my project to use google test 1.13 from the previous version of 1.8. However it doesn't successfully link any project that uses synchronization such as boost mutex. I'm just using visual studio to build, not cmake.

Shawn1874 commented 8 months ago

This article does suggest that the preprocessor definition should be added when linking to google test built as a shared library.
https://github.com/google/googletest/blob/main/googletest/README.md

Can the original poster for this defect write whether you built google test to be used a shared library? If so then google test seems to be working as designed. The documentation does state that using that preprocessor definition will become necessary at some point for some compilers and recommends that we always define it when linking google test as a shared library.