c++ - 静态链接cppunit时如何解决 "undefined reference"错误?

标签 c++ static-linking cppunit

我不确定为什么这个问题被标记为重复 Why does the order in which libraries are linked sometimes cause errors in GCC? .那个问题和这个问题是两个完全不同的问题。

我已经在我的 Debian 系统上安装了 libcppunit-devapt-get

$ dpkg -l libcppunit-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                       Version            Architecture       Description
+++-==========================-==================-==================-==========================================================
ii  libcppunit-dev             1.13.2-2.1         amd64              Unit Testing Library for C++

$ dpkg -L libcppunit-dev | grep "libcppunit\."
/usr/lib/x86_64-linux-gnu/libcppunit.a
/usr/lib/x86_64-linux-gnu/libcppunit.so

我写了一个简单的测试。因为我现在只关心链接 cppunit,所以我还没有测试运行器。暂时忽略缺少测试运行程序。这是我的 test.cpp:

#include <cppunit/extensions/HelperMacros.h>
#include <iostream>

class FooTest : public CppUnit::TestFixture
{
protected:
    void testFoo()
    {
        CPPUNIT_ASSERT_EQUAL(1 + 1, 2);
    }

private:
    CPPUNIT_TEST_SUITE(FooTest);
    CPPUNIT_TEST(testFoo);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(FooTest);

int main()
{
    std::cout << "hello\n";
    return 0;
}

动态链接工作正常:

$ g++ -lcppunit test.cpp && ./a.out
hello

静态链接失败:

$ g++ -l:libcppunit.a test.cpp
/tmp/ccwMWIsX.o: In function `CppUnit::AdditionalMessage::~AdditionalMessage()':
test.cpp:(.text._ZN7CppUnit17AdditionalMessageD2Ev[_ZN7CppUnit17AdditionalMessageD5Ev]+0x14): undefined reference to `CppUnit::Message::~Message()'
/tmp/ccwMWIsX.o: In function `FooTest::testFoo()':
test.cpp:(.text._ZN7FooTest7testFooEv[_ZN7FooTest7testFooEv]+0x70): undefined reference to `CppUnit::SourceLine::SourceLine(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
test.cpp:(.text._ZN7FooTest7testFooEv[_ZN7FooTest7testFooEv]+0xa5): undefined reference to `CppUnit::SourceLine::~SourceLine()'
test.cpp:(.text._ZN7FooTest7testFooEv[_ZN7FooTest7testFooEv]+0xe9): undefined reference to `CppUnit::SourceLine::~SourceLine()'
/tmp/ccwMWIsX.o: In function `FooTest::getTestNamer__()':
test.cpp:(.text._ZN7FooTest14getTestNamer__Ev[_ZN7FooTest14getTestNamer__Ev]+0x41): undefined reference to `CppUnit::TestNamer::TestNamer(std::type_info const&)'

如何确保与 cppunit 的静态链接成功?

最佳答案

尝试以另一种顺序链接你的程序和库

g++ test.cpp -l:libcppunit.a

或者

g++ test.cpp /usr/lib/x86_64-linux-gnu/libcppunit.a

或者

g++ test.cpp -static -lcppunit -Wl,-Bdynamic

最后的-Wl,-Bdynamic是为了避免libc++或glibc等系统库的静态链接。

关于c++ - 静态链接cppunit时如何解决 "undefined reference"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50355035/

相关文章:

c++ - 对于 MySQL 和 c++ 来说,没有文件(即内存中)的 “Load DATA” 是否可行

c++ - 定义双感叹号?

c++ - Mac 应用程序的文件保存位置

c++ - 独立 Exe 的 Visual Studio 静态链接

c++ - 一个简单的c++程序的编译过程

c++ - 静态链接 g++ 时出错

c++ - 以阻塞方式在 OpenGL 中渲染纹理(视频),因此我收到的视频帧在渲染时不会被新帧替换

c++ - 如何使 CppUnit 日志记录更详细?

c++ - 我在哪里放置单元测试源并公开内部组件?

c++编译cppunit文件并执行xml报告