【问题标题】:gtest - linker finds `libgtest.a` but still complains undefined reference to `testing::Test::Test()` [duplicate]gtest - 链接器找到`libgtest.a`,但仍然抱怨对`testing::Test::Test()`的未定义引用[重复]
【发布时间】:2020-08-30 05:39:43
【问题描述】:

我是谷歌 C++ 测试的新手。 我通过downloading source code from GitHub 安装了谷歌测试。 我用 CMake 构建它并将其安装到 $HOME/.local 中。 然后我用下面的代码进行测试。

#include "gtest/gtest.h"

TEST(Foo, Bar) {
  EXPECT_TRUE(true);
}

我用它编译过

clang++ -std=c++1z -I$HOME/.local/include -L$HOME/.local/lib -lgtest -lgtest_main a.cc

给出以下链接器错误

/tmp/a-dc64ec.o: In function `Foo_Bar_Test::TestBody()':
a.cc:(.text+0x5f): undefined reference to `testing::Message::Message()'
a.cc:(.text+0x81): undefined reference to `testing::internal::GetBoolAssertionFailureMessage[abi:cxx11](testing::AssertionResult const&, char const*, char const*, char const*)'
a.cc:(.text+0xaa): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
a.cc:(.text+0xbc): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
a.cc:(.text+0xca): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
a.cc:(.text+0x109): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/tmp/a-dc64ec.o: In function `__cxx_global_var_init.1':
a.cc:(.text.startup+0x8a): undefined reference to `testing::internal::GetTestTypeId()'
a.cc:(.text.startup+0x134): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/tmp/a-dc64ec.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x6a): undefined reference to `testing::internal::IsTrue(bool)'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x9b): undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x122): undefined reference to `testing::internal::GTestLog::~GTestLog()'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x137): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/tmp/a-dc64ec.o: In function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x6a): undefined reference to `testing::internal::IsTrue(bool)'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x9b): undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x118): undefined reference to `testing::internal::GTestLog::~GTestLog()'
a.cc:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x12d): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/tmp/a-dc64ec.o: In function `Foo_Bar_Test::~Foo_Bar_Test()':
a.cc:(.text._ZN12Foo_Bar_TestD2Ev[_ZN12Foo_Bar_TestD2Ev]+0x14): undefined reference to `testing::Test::~Test()'
/tmp/a-dc64ec.o: In function `Foo_Bar_Test::Foo_Bar_Test()':
a.cc:(.text._ZN12Foo_Bar_TestC2Ev[_ZN12Foo_Bar_TestC2Ev]+0x1b): undefined reference to `testing::Test::Test()'
/tmp/a-dc64ec.o:(.rodata+0x20): undefined reference to `testing::Test::SetUp()'
/tmp/a-dc64ec.o:(.rodata+0x28): undefined reference to `testing::Test::TearDown()'
/tmp/a-dc64ec.o:(.rodata+0x60): undefined reference to `typeinfo for testing::Test'
/home/wyq/.local/lib/libgtest_main.a(gtest_main.cc.o): In function `main':
gtest_main.cc:(.text+0x32): undefined reference to `testing::InitGoogleTest(int*, char**)'
/home/wyq/.local/lib/libgtest_main.a(gtest_main.cc.o): In function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

将 --verbose 添加到 clang 会给出以下链接器命令

"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/home/wyq/.local/lib -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/home/wyq/.local/bin/../lib -L/lib -L/usr/lib -lgtest -lgtest_main a.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o

将--verbose 添加到ld 表明它实际上找到了libgtest.a 和libgtest_main.a。

"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/home/wyq/.local/lib -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/home/wyq/.local/bin/../lib -L/lib -L/usr/lib -lgtest -lgtest_main a.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o --verbose 2>&1 | grep gtest
attempt to open /home/wyq/.local/lib/libgtest.so failed
attempt to open /home/wyq/.local/lib/libgtest.a succeeded
attempt to open /home/wyq/.local/lib/libgtest_main.so failed
attempt to open /home/wyq/.local/lib/libgtest_main.a succeeded
(/home/wyq/.local/lib/libgtest_main.a)gtest_main.cc.o
/home/wyq/.local/lib/libgtest_main.a(gtest_main.cc.o): In function `main':
gtest_main.cc:(.text+0x32): undefined reference to `testing::InitGoogleTest(int*, char**)'
/home/wyq/.local/lib/libgtest_main.a(gtest_main.cc.o): In function `RUN_ALL_TESTS()':
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
gtest_main.cc:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'

这是为什么?如何正确链接 gtest 库?

【问题讨论】:

    标签: c++ linker linker-errors googletest clang++


    【解决方案1】:

    链接器参数的顺序错误

    a.cc -lgtest -lgtest_main 
    

    不是

    -lgtest -lgtest_main a.cc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2020-11-08
      • 2014-09-09
      • 1970-01-01
      • 2019-03-12
      • 2015-10-30
      • 2017-12-23
      相关资源
      最近更新 更多