title: googletest Undefined symbols for architecture x86_64 error
body: <p>Let <code>GTEST_DIR</code> be the environment variable storing the path to the <code>googletest</code> directory. (I cloned googletest-master from <a href="https://github.com/google/googletest" rel="nofollow">googletest's github repo</a>.)</p>
  
  <p>I <code>cd</code>'ed into <code>$GTEST_DIR</code>, did a <code>mkdir build &amp;&amp; cd build</code>, then executed the following command :</p>
  
  <pre><code>cmake .. -DCMAKE_C_COMPILER=$GNU-6.0.0/bin/gcc-6.0.0 -DCMAKE_CXX_COMPILER=$GNU-6.0.0/bin/g++-6.0.0
  </code></pre>
  
  <p>where <code>GNU-6.0.0</code> is the path to my gnu install. This generated a <code>Makefile</code> inside <code>$GTEST_DIR/build</code> that I tweaked as follows : I've added</p>
  
  <pre><code>CC = $GNU-6.0.0/bin/gcc-6.0.0
  CXX = $GNU-6.0.0/bin/g++-6.0.0
  </code></pre>
  
  <p>at its beginning, to be sure that the c and c++ compilers that will be used will be those I want to be used. Then I ran <code>make</code> which produced archive files <code>libgtest.a</code> and <code>libgtest_main.a</code> inside <code>$GTEST_DIR/build</code>.</p>
  
  <p>Next step : in a same folder I put a main test source file <code>main.cpp</code> containg :</p>
  
  <pre><code>#include "path/to/gtest.h"
  
  int main(int argc, char* argv[])
  {
      ::testing::InitGoogleTest(&amp;argc,argv);
      return RUN_ALL_TESTS();
  }
  </code></pre>
  
  <p>and a dummy test <code>dummy_test.cpp</code> containing :</p>
  
  <pre><code>#include "path/to/gtest.h"
  
  TEST(dummy_test, test1)
  {
      EXPECT_EQ(1,1);
  }
  </code></pre>
  
  <p>and a <code>Makefile</code> containing :</p>
  
  <pre><code>CC = gcc-6.0.0
  CXX = g++-6.0.0
  
  CPPFLAGS += -isystem $(GTEST_DIR)/include
  LDFLAGS := -L/usr/lib -lpthread -L$(GTEST_DIR)/build -lgtest
  
  all :
      $(CXX) -o cpptests $(CPPFLAGS) ./main.cpp ./dummy_test.cpp $(LDFLAGS)
  
  clean :
      rm -rf ./cpptests
  </code></pre>
  
  <p>Running <code>make</code> I have this output :</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::end() const", referenced from:
        testing::internal::XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) in libgtest.a(gtest-all.cc.o)
    "std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::data() const", referenced from:
        testing::internal::PrintStringTo(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;*) in libgtest.a(gtest-all.cc.o)
        __gnu_cxx::__enable_if&lt;std::__is_char&lt;char&gt;::__value, bool&gt;::__type std::operator==&lt;char&gt;(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) in libgtest.a(gtest-all.cc.o)
    "std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::find(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, unsigned long) const", referenced from:
        bool testing::(anonymous namespace)::IsSubstringPred&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) in libgtest.a(gtest-all.cc.o)
    "std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::find(char, unsigned long) const", referenced from:
        testing::internal::SplitString(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, char, std::vector&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;*) in libgtest.a(gtest-all.cc.o)
        testing::internal::FormatDeathTestOutput(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) in libgtest.a(gtest-all.cc.o)
    "std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::size() const", referenced from:
        testing::internal::(anonymous namespace)::SplitEscapedString(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) in libgtest.a(gtest-all.cc.o)
  
  ...
  
        ...
  ld: symbol(s) not found for architecture x86_64
  collect2: error: ld returned 1 exit status
  make: *** [all] Error 1
  </code></pre>
  
  <p>The entire output is in <a href="https://bitbucket.org/snippets/MisesEnForce/ox8RB/output" rel="nofollow">this snippet</a> while the output of <code>nm libgtest.a</code> is in <a href="https://bitbucket.org/snippets/MisesEnForce/rjB94" rel="nofollow">this snippet</a>.</p>
  
  <p><strong>Precision :</strong> I am under mac os x 10.10.5. As I want to continue working, developping code and testing it, while the aforementionned error with <code>gcc</code>/<code>g++</code> 6.0.0 is solved, I tried to switch to another compiler : <code>clang</code>, and I remarked that I had no error at all with it.</p>
  
  <p><strong>Remark</strong> <code>libgtest.a</code> was initially (that is, before I asked this question) built "by error" with <code>clang</code> and used in tests with <code>g++-6.1.0</code> when I encountered the error, that's why I decided to rebuild <code>libgtest.a</code> with <code>g++-6.1.0</code>, thinking that it would solve the problem, but it didn't, which led me to post here.</p>