Google Mock and Google Test as DLLs - link error g_gmock_mutex, and g_linked_ptr_mutex unresolved externals

5,036 views
Skip to first unread message

Jorge Costa

unread,
Feb 21, 2012, 10:47:23 AM2/21/12
to Google C++ Mocking Framework
Hi,

I been looking for some time now, and failed to find the information
about this issue:
http://groups.google.com/group/googlemock/browse_thread/thread/a68996453749a230/4ec464cefdc0edf8?lnk=gst&q=g_gmock_mutex#4ec464cefdc0edf8

it seemed the solution was to include the
GTEST_LINKED_AS_SHARED_LIBRARY=1 to the dll code to export the methods
correctly. However for me it failed to solve the problem.

This is what i've done:

1. SVN checkout, External at revision 605, At revision 404. In Cygwin
in windows 7
2. with cmake version 2.8.7, generate sln files for gmock in directory
build.

3. Run Visual studio 2010 and configure gtest and gmock projects in
the following way:
. Change configuration to dll, and change extension
. Change Runtime Library to Multi-threaded Debug DLL (/MDd), to be
inline with my test project
. add preprocessor flag GTEST_CREATE_SHARED_LIBRARY=1 to both
projects, additionally to this the following values were defined:
gmock - WIN32 _WINDOWS _DEBUG _UNICODE UNICODE _WIN32 STRICT
WIN32_LEAN_AND_MEAN GTEST_HAS_PTHREAD=0 _HAS_EXCEPTIONS=1
CMAKE_INTDIR="Debug" _ITERATOR_DEBUG_LEVEL=0
GTEST_CREATE_SHARED_LIBRARY=1
gtest - WIN32 _WINDOWS _ITERATOR_DEBUG_LEVEL=0 _DEBUG _UNICODE
UNICODE _WIN32 STRICT WIN32_LEAN_AND_MEAN GTEST_HAS_PTHREAD=0
_HAS_EXCEPTIONS=1 CMAKE_INTDIR="Debug" GTEST_CREATE_SHARED_LIBRARY=1

4. Compile, this creates gtest.dll, gtest.lib, gtest.pdb, gmock.dll,
gmock.lib and gmock.pdb.

5. My project has several static libraries that are used to create a
main dll. Each of these static libs have several tests defined. What i
do now is in one of these static libraries defined a
"googletest" (there are already cppunit tests defined there). I link
also against the lib files created above. In this step i do not set
the GTEST_LINKED_AS_SHARED_LIBRARY, since this makes my library fails
to compile with loads of: warnings like: needs to have dll-interface
to be used by clients of class 'testing::internal::ExpectationBase'.
All the methods that are exported from the DLL are throwing warnings.

6. Create a test executable and reference the main DLL. Define the
PullInMyLibrary dummy functions in both test dll and test exe.

This is my main:
::testing::InitGoogleMock(&argc, argv);
::testing::InitGoogleTest(&argc, argv);

7. This compiles without any problem. Im able to add new tests and run
them. This also proves that im using google mock that is in the
library.

Now i want to add a mock, so in the same source file ive created the
dummy mock:

class Turtle {
public:
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual void Forward(int distance) = 0;
virtual void Turn(int degrees) = 0;
virtual void GoTo(int x, int y) = 0;
virtual int GetX() const = 0;
virtual int GetY() const = 0;
};

class MockTurtle : public Turtle {
public:
MOCK_METHOD0(PenUp, void());
MOCK_METHOD0(PenDown, void());
MOCK_METHOD1(Forward, void(int distance));
MOCK_METHOD1(Turn, void(int degrees));
MOCK_METHOD2(GoTo, void(int x, int y));
MOCK_CONST_METHOD0(GetX, int());
MOCK_CONST_METHOD0(GetY, int());
};

Now add to the test method MockTurtle Mock;

And ups, get the same link errors as the discussion above.

.obj) : error LNK2001: unresolved external symbol "class
testing::internal::Mutex testing::internal::g_gmock_mutex" (?
g_gmock_mutex@internal@testing@@3VMutex@12@A)
.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)

Ive exported the functions from the dll and they are present in both
DLLs. This is the exported report:
gmock.dll
class testing::internal::Mutex testing::internal::g_gmock_mutex
0x100aa704 0x000aa704 500 (0x1f4) gmock.dll C:\GMOCK\googlemock-read-
only\build\Debug\gmock.dll Exported Function
class testing::internal::Mutex testing::internal::g_linked_ptr_mutex
0x100aa657 0x000aa657 503 (0x1f7) gmock.dll C:\GMOCK\googlemock-read-
only\build\Debug\gmock.dll Exported Function

gtest.dll
class testing::internal::Mutex testing::internal::g_linked_ptr_mutex
0x1008ffd7 0x0008ffd7 370 (0x172) gtest.dll C:\GMOCK\googlemock-read-
only\build\gtest\Debug\gtest.dll Exported Function

Why doesn't the g_linked_ptr_mutex complains when only gtest are
defined.

Im struggling with this one, hope someone can help me.

Thank you

Jorge Costa








Vlad Losev

unread,
Feb 21, 2012, 11:14:43 AM2/21/12
to Google C++ Mocking Framework
Jorge -

Google Mock does not support building in a separate DLL on Windows. The workaround is to simply add the source files of gtest project to the gmock project and build with GTEST_LINKED_AS_SHARED_LIBRARY=1.
HTH,
Vlad

Jorge Costa

unread,
Feb 21, 2012, 3:48:32 PM2/21/12
to Google C++ Mocking Framework
HI Vlad,
Thank you for help and the fast reply, unfortunely i was unable to
follow your instructions. First creating the solution above with cmake
already provides in the gmock project the include of gtest-all.cc that
will compile gtest as part of gmock. However changing the
GTEST_LINKED_AS_SHARED_LIBRARY=1 will results in loads of inconsistent
dll linkage. I've also try adding the sources in the gtest src folder
but with the same result as below.


2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(434) : see previous definition of
'ClearDefaultActionsLocked'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(773):
warning C4273: 'testing::Expectation::Expectation' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(482) : see previous definition of '{ctor}'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(777):
warning C4273: 'testing::Expectation::Expectation' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(529) : see previous definition of '{ctor}'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(779):
warning C4273: 'testing::Expectation::~Expectation' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(484) : see previous definition of '{dtor}'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(782):
warning C4273: 'testing::Sequence::AddExpectation' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(618) : see previous definition of 'AddExpectation'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(793):
warning C4273: 'testing::InSequence::InSequence' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(654) : see previous definition of '{ctor}'
2>C:\GMOCK\googlemock-read-only\src/gmock-spec-builders.cc(804):
warning C4273: 'testing::InSequence::~InSequence' : inconsistent dll
linkage
2> C:\GMOCK\googlemock-read-only\include\gmock/gmock-spec-
builders.h(655) : see previous definition of '{dtor}'
2>C:\GMOCK\googlemock-read-only\src/gmock.cc(42): error C2491:
'testing::FLAGS_gmock_catch_leaked_mocks' : definition of dllimport
data not allowed
2>C:\GMOCK\googlemock-read-only\src/gmock.cc(49): error C2491:
'testing::FLAGS_gmock_verbose' : definition of dllimport data not
allowed
2>C:\GMOCK\googlemock-read-only\src/gmock.cc(172): error C2491:
'testing::InitGoogleMock' : definition of dllimport function not
allowed
2>C:\GMOCK\googlemock-read-only\src/gmock.cc(178): error C2491:
'testing::InitGoogleMock' : definition of dllimport function not
allowed

Jorge COsta

Vlad Losev

unread,
Feb 22, 2012, 2:15:02 AM2/22/12
to Google C++ Mocking Framework
Jorge -

Sorry for the confusion: the gmock project should be built with -DGTEST_CREATE_SHARED_LIBRARY=1 and the tests built with -DGTEST_LINKED_AS_SHARED_LIBRARY=1. I was able to configure cmake with VC 10 with the patch supplied below using this command:

cmake <gtest_dir> -DBUIlD_SHARED_LIBS=ON -Dgmock_build_tests=ON

and build and run the gmock tests successfully.

HTH,
Vlad

VC 10 compatibility patch:

Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt      (revision 404)
+++ CMakeLists.txt      (working copy)
@@ -133,18 +133,21 @@
   cxx_library(gmock_main_no_rtti "${cxx_no_rtti}"
     "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

-  cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
-    "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)
-
   cxx_test_with_flags(gmock-more-actions_no_exception_test "${cxx_no_exception}"
     gmock_main_no_exception test/gmock-more-actions_test.cc)

   cxx_test_with_flags(gmock_no_rtti_test "${cxx_no_rtti}"
     gmock_main_no_rtti test/gmock-spec-builders_test.cc)

-  cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
-    gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
+  if (MSVC_VERSION LESS 1600)
+    # MSVC 10 implements its own tuple which conflicts with gtest-defined one.
+    cxx_library(gmock_main_use_own_tuple "${cxx_use_own_tuple}"
+      "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

+    cxx_test_with_flags(gmock_use_own_tuple_test "${cxx_use_own_tuple}"
+      gmock_main_use_own_tuple test/gmock-spec-builders_test.cc)
+  endif()
+
   cxx_shared_library(shared_gmock_main "${cxx_default}"
     "${gtest_dir}/src/gtest-all.cc" src/gmock-all.cc src/gmock_main.cc)

Index: gtest/cmake/internal_utils.cmake
===================================================================
--- gtest/cmake/internal_utils.cmake    (revision 605)
+++ gtest/cmake/internal_utils.cmake    (working copy)
@@ -65,6 +65,14 @@
       # Compatibility warnings not applicable to Google Test.
       # Resolved overload was found by argument-dependent lookup.
       set(cxx_base_flags "${cxx_base_flags} -wd4675")
+    elseif (MSVC_VERSION EQUAL 1600)
+      # 4413 is a spurious warning under VC 10. It doesn't consider
+      # the lifetimes of objects in question.
+      set(cxx_base_flags "${cxx_base_flags} -wd4413")
+      set(cxx_base_flags "${cxx_base_flags} -wd4996")
+      # Compatibility warnings not applicable to Google Test.
+      # Virtual function overrides one that differs only in const/volatile
+      # parameter qualifiers.
+      set(cxx_base_flags "${cxx_base_flags} -wd4373")
     endif()
     set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
     set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
Reply all
Reply to author
Forward
0 new messages