title: pthread-related errors when upgrading test from GoogleTest to GoogleMock (Ubuntu 14)
body: <p>While trying to add <code>gmock</code> to an existing project that already was using <code>gtest</code>, I have found a series of low-level errors related to <code>pthread</code>. My guess is this is related to how GoogleMock &amp; GoogleTest are built (see details below), but unfortunately there is not much information about the best way of using these libraries in Ubuntu 14.</p>
  
  <p>After reducing the code to a minimal representative example, what happens is: </p>
  
  <ol>
  <li><p>I replaced the GoogleTest headers with the GoogleMock ones, as well as the "main" function:</p>
  
  <pre><code>// 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 &lt;gmock/gmock.h&gt; // replaced &lt;gtest/gtest.h&gt;
  #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 &lt;gmock/gmock.h&gt; // replaced &lt;gtest/gtest.h&gt;
  
  int main(int argc, char **argv) {
   ::testing::InitGoogleMock(&amp;argc, argv); // replaced InitGoogleTest
   return RUN_ALL_TESTS();
  }
  </code></pre></li>
  </ol>
  
  <p>This works FINE. </p>
  
  <ol start="2">
  <li><p>When I try to use a GoogleMock feature (e.g a matcher): </p>
  
  <pre><code>// foo.test.cpp
  using ::testing::Eq;
  //...
  TEST(Foo,ReturnsSumOfTwoNumbers){
   Foo foo;
   // replaced ASSERT_EQ
   ASSERT_THAT( foo.sum(2,8), Eq(10) );
  }
  </code></pre></li>
  </ol>
  
  <p>.. It crashes with the following error: </p>
  
  <blockquote>
    <p>[ FATAL ] /path/to/gmock-1.7.0/gtest/include/gtest/internal/gtest-port.h:1340:: pthread_mutex_lock(&amp;mutex_)failed with error 22
    Aborted (core dumped)</p>
  </blockquote>
  
  <p>Using other gmock features yielded similar errors.</p>
  
  <p><strong>Build info:</strong> </p>
  
  <ul>
  <li>Ubuntu 14.04</li>
  <li>gcc 4.8.2</li>
  <li><p>gmock 1.7.0 is downloaded and built with CMAKE: </p>
  
  <pre><code>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 
  </code></pre></li>
  <li><p>Finally, the whole project is built using Qt's qmake: </p>
  
  <pre><code>INCLUDEPATH += $${GMOCK_PATH}/include $${GTEST_PATH}/include
  LIBS += -L$${GTEST_PATH}/lib -L$${GMOCK_PATH}/lib -lgmock -lgtest -lpthread
  </code></pre></li>
  </ul>
  
  <p>Compiler output is: </p>
  
  <blockquote>
    <p>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 </p>
  </blockquote>