Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtest: Undefined symbols for architecture x86_64 error with clang++ and std::vector

I downloaded the gtest 1.6, and compiled it with clang++.

  1. export CC=/usr/bin/clang
  2. export CXX=/usr/bin/clang++
  3. configure
  4. make

I got the libgtest.a, and I copied it into /usr/local/lib/libgtest_clang.a.

When I tested with simple C++ code, everything works OK, however, when I tried to use vector in test code, I got these error messages in the build process. Compilation works fine.

Undefined symbols for architecture x86_64:
  "std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::find(wchar_t const*, unsigned long, unsigned long) const", referenced from:
      testing::AssertionResult testing::(anonymous namespace)::IsSubstringImpl<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >(bool, char const*, char const*, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) in libgtest_clang.a(gtest-all.o)
...

This is the command line I used for the build.

clang++ -DGTEST_USE_OWN_TR1_TUPLE=1 -std=c++11 -stdlib=libc++ main.cpp test_a.cc \
-L/usr/local/lib -I. -lgtest_clang -o t

This is the test code and code under test.

#include <limits.h>
#include <time.h>
#include <gtest/gtest.h>
#include <list>
#include <vector>
#include <string>
#include "a.h"

using namespace std;

class QuickTest : public testing::Test {
 protected:
  virtual void SetUp() {
  }
  virtual void TearDown() {
  }
};

class ErrorTest : public QuickTest {
 protected:
  virtual void SetUp() {
      QuickTest::SetUp();
  }

  virtual void TearDown() {
      QuickTest::TearDown();
  }
};

TEST_F(ErrorTest, catchMessage2) {
    vector<int> h {1,2,3,4,5};

    for (auto& i : h) {
        A* a = new A(i);   
        EXPECT_TRUE(a->get() == i);
        delete a;
    }
}

class A
{
    int x;
public:
    A(int x) : x(x) {}
    void set(int x) {this->x = x;}
    int get() {return x;}
};
like image 719
prosseek Avatar asked Jun 24 '13 02:06

prosseek


2 Answers

The issue was from not giving the same compiler options when building gtest.

export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
./configure 'CXXFLAGS=-std=c++11 -stdlib=libc++ -DGTEST_USE_OWN_TR1_TUPLE=1'
make

After the new build of gtest and the source, everything works fine.

like image 142
prosseek Avatar answered Dec 01 '22 00:12

prosseek


Just a hint for the fortunately minor partition of idiots like me. If you happen to compile gtest with the Apple g++. And meanwhile installed gcc with e.g. homebrew, linking to gtest will cause this error.

So compile gtest and your project with the same compiler. :D

like image 24
ManuelSchneid3r Avatar answered Nov 30 '22 23:11

ManuelSchneid3r