While trying to add gmock to an existing project that already was using gtest, I have found a series of low-level errors related to pthread. My guess is this is related to how GoogleMock & GoogleTest are built (see details below), but unfortunately there is not much information about the best way of using these libraries in Ubuntu 14.
After reducing the code to a minimal representative example, what happens is:
I replaced the GoogleTest headers with the GoogleMock ones, as well as the "main" function:
// Declarations at foo.h class Foo { public: int sum(int a, int b); // it returns a+b (defined in foo.cpp) }; // Test code at foo.test.cpp #include <gmock/gmock.h> // replaced <gtest/gtest.h> #include "foo.h" TEST(Foo,ReturnsSumOfTwoNumbers){ Foo foo; ASSERT_EQ( foo.sum(2,8), 10 ); } TEST(Foo,ReturnsSumOfTwoIntegerNumbers){ Foo foo; ASSERT_EQ( 6,foo.sum(-2,8) ); } // main.cpp #include <gmock/gmock.h> // replaced <gtest/gtest.h> int main(int argc, char **argv) { ::testing::InitGoogleMock(&argc, argv); // replaced InitGoogleTest return RUN_ALL_TESTS(); }
This works FINE.
When I try to use a GoogleMock feature (e.g a matcher):
// foo.test.cpp using ::testing::Eq; //... TEST(Foo,ReturnsSumOfTwoNumbers){ Foo foo; // replaced ASSERT_EQ ASSERT_THAT( foo.sum(2,8), Eq(10) ); }
.. It crashes with the following error:
[ FATAL ] /path/to/gmock-1.7.0/gtest/include/gtest/internal/gtest-port.h:1340:: pthread_mutex_lock(&mutex_)failed with error 22 Aborted (core dumped)
Using other gmock features yielded similar errors.
Build info:
- Ubuntu 14.04
- gcc 4.8.2
gmock 1.7.0 is downloaded and built with CMAKE:
wget https://googlemock.googlecode.com/files/gmock-1.7.0.zip unzip gmock-1.7.0.zip GMOCK_PATH=$PWD/gmock-1.7.0 GTEST_PATH=$GMOCK_PATH/gtest mkdir $GMOCK_PATH/lib $GTEST_PATH/lib pushd $GMOCK_PATH/lib cmake -DBUILD_SHARED_LIBS=ON .. make cd $GTEST_PATH/lib cmake -DBUILD_SHARED_LIBS=ON .. make popdFinally, the whole project is built using Qt's qmake:
INCLUDEPATH += $${GMOCK_PATH}/include $${GTEST_PATH}/include LIBS += -L$${GTEST_PATH}/lib -L$${GMOCK_PATH}/lib -lgmock -lgtest -lpthread
Compiler output is:
g++ -m64 -Wl,-rpath,/path/to/gmock-1.7.0/gtest/lib -Wl,-rpath,/path/to/gmock-1.7.0/lib -o all_tests foo.test.o foo.o main.o -L/path/to/gmock-1.7.0/gtest/lib -L/path/to/gmock-1.7.0/lib -lgmock -lgtest -lpthread
My gut feeling is that you're right; the error is likely the result of differences in compiler and/or compiler settings between googlemock and your code. If you continue to use googlemock as a separately compiled library, it should just be a matter of verifying that the compiler versions and all compiler flags match. Pay special attention to threading-related flags (-pthread), standard-related flags (-std=c++11), etc.
That being said, my preferred solution to these kinds of problems is to embed the googlemock code into your own application instead of compiling it as a separate library. I'm not at all familiar with the Qt build system so I can't offer specific directions, but the gist of the idea is that you take the source(s) in the /fused-src directory of the gmock distribution and add them to your project, so that they're compiled alongside your own code. This guarantees that compiler settings will match, and you won't have to worry about contorting either your own or googlemock's build system.