I downloaded the gtest 1.6, and compiled it with clang++.
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;}
};
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With