googletest架构x86_64错误的未定义符号 [英] googletest Undefined symbols for architecture x86_64 error
问题描述
让 GTEST_DIR
作为环境变量,存储到 googletest
目录的路径。 (我从 googletest的github存储库中克隆了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
放入 $ GTEST_DIR
,进行了 mkdir构建和安装。 & 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
,它生成了存档文件 libgtest.a
和 libgtest_main.a $
$ GTEST_DIR / build
中的c $ c>。
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
containsg:
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
包含:
#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
我有以下输出:
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.
备注 libgtest。
最初(即在我问这个问题之前)是使用 clang
错误地构建的,并用于 g ++-6.1.0
遇到错误时,这就是为什么我决定用 g ++-重建
,以为它可以解决问题,但没有解决,这导致我在此处发布。 libgtest.a
的原因6.1.0
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(); }
对于我来说,可以完美地编译
compiles perfectly for me with command
clang ++ -std = c ++ 11 main.cpp -lgtest
clang++ -std=c++11 main.cpp -lgtest
我可以运行单个测试而不会出现问题。
and I could run the single test without an issue.
这篇关于googletest架构x86_64错误的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!