googletest 架构 x86_64 错误的未定义符号
让 GTEST_DIR
成为存储 googletest
目录路径的环境变量.(我从 googletest 的 github repo 克隆了 googletest-master.)
Let GTEST_DIR
be the environment variable storing the path to the googletest
directory. (I cloned googletest-master from googletest's github repo.)
我 cd
'ed 到 $GTEST_DIR
,做了一个 mkdir build &&cd build
,然后执行以下命令:
I cd
'ed into $GTEST_DIR
, did a mkdir build && cd build
, then executed the following command :
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
GNU-6.0.0
是我的 gnu 安装路径.这在 $GTEST_DIR/build
内生成了一个 Makefile
,我对其进行了如下调整:我已添加
where GNU-6.0.0
is the path to my gnu install. This generated a Makefile
inside $GTEST_DIR/build
that I tweaked as follows : I've added
CC = $GNU-6.0.0/bin/gcc-6.0.0
CXX = $GNU-6.0.0/bin/g++-6.0.0
在开始时,要确保将使用的 c 和 c++ 编译器将是我想要使用的那些.然后我运行 make
,它在 $GTEST_DIR/build
中生成了存档文件 libgtest.a
和 libgtest_main.a
.
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 make
which produced archive files libgtest.a
and libgtest_main.a
inside $GTEST_DIR/build
.
下一步:在同一个文件夹中,我放置了一个主测试源文件 main.cpp
包含:
Next step : in a same folder I put a main test source file main.cpp
containg :
#include "path/to/gtest.h"
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
和一个虚拟测试 dummy_test.cpp
包含:
and a dummy test dummy_test.cpp
containing :
#include "path/to/gtest.h"
TEST(dummy_test, test1)
{
EXPECT_EQ(1,1);
}
和一个 Makefile
包含:
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
运行 make
我有这个输出:
Running make
I have this output :
Undefined symbols for architecture x86_64:
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::end() const", referenced from:
testing::internal::XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::data() const", referenced from:
testing::internal::PrintStringTo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_ostream<char, std::char_traits<char> >*) in libgtest.a(gtest-all.cc.o)
__gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) const", referenced from:
bool testing::(anonymous namespace)::IsSubstringPred<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find(char, unsigned long) const", referenced from:
testing::internal::SplitString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*) in libgtest.a(gtest-all.cc.o)
testing::internal::FormatDeathTestOutput(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libgtest.a(gtest-all.cc.o)
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
testing::internal::(anonymous namespace)::SplitEscapedString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) 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
整个输出在 这个片段中,而 nm libgtest 的输出.a
在这个片段中.
The entire output is in this snippet while the output of nm libgtest.a
is in this snippet.
精度:我在mac os x 10.10.5下.由于我想继续工作,开发代码并对其进行测试,而 gcc
/g++
6.0.0 的上述错误已解决,我尝试切换到另一个编译器:clang
,我说我完全没有错误.
Precision : 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 gcc
/g++
6.0.0 is solved, I tried to switch to another compiler : clang
, and I remarked that I had no error at all with it.
Remark libgtest.a
最初(即在我问这个问题之前)用 clang
错误地"构建并用于遇到错误时使用 g++-6.1.0
进行测试,这就是我决定使用 g++-6.1.0
重建 libgtest.a
的原因,以为它会解决问题,但它没有,这导致我在这里发帖.
Remark libgtest.a
was initially (that is, before I asked this question) built "by error" with clang
and used in tests with g++-6.1.0
when I encountered the error, that's why I decided to rebuild libgtest.a
with g++-6.1.0
, thinking that it would solve the problem, but it didn't, which led me to post here.
推荐答案
问题似乎是由于一些链接问题.您是否正确使用了链接器标志.对于gtest,我们需要用-lgtest编译,链接器才能正确链接.同样,我们需要为我们链接的所有可能的库提供标志.
The problem seems to be due to some linking issues. Have you used the linker flags correctly. For gtest, we need to compile with -lgtest and the linker can link it correctly. Similarly, we need to have the flags for all possible libraries that we are linking against.
代码
TEST(dummy_test, test1) {EXPECT_EQ(1,1);}
TEST(dummy_test, test1) { EXPECT_EQ(1,1); }
int main(int argc, char **argv) {::testing::InitGoogleTest(&argc, argv);返回 RUN_ALL_TESTS();}
int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
用命令完美地为我编译
clang++ -std=c++11 main.cpp -lgtest
clang++ -std=c++11 main.cpp -lgtest
我可以毫无问题地运行单个测试.
and I could run the single test without an issue.
相关文章