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 popd
Finally, 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
Try to use this in the CMake
add_definitions("-DGTEST_HAS_PTHREAD=0")