[英]How to resolve “undefined reference” error while statically linking cppunit?
I am not sure why this question is marked as a duplicate of Why does the order in which libraries are linked sometimes cause errors in GCC? 我不确定为什么将这个问题标记为重复的原因 , 为什么链接库的顺序有时会导致GCC错误? . 。 That question and this question are two totally different questions. 这个问题和这个问题是两个完全不同的问题。
I have installed libcppunit-dev
with apt-get
on my Debian system. 我在Debian系统上用apt-get
安装了libcppunit-dev
。
$ 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
I have written a simple test. 我写了一个简单的测试。 Since my concern right now is only linking cppunit, I don't have a test runner yet. 由于我现在关心的只是链接cppunit,所以我还没有测试人员。 Ignore the lack of test runner for now. 暂时忽略缺少测试运行程序。 Here is my test.cpp
: 这是我的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;
}
Dynamic linking works fine: 动态链接可以正常工作:
$ g++ -lcppunit test.cpp && ./a.out
hello
Static linking fails: 静态链接失败:
$ 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&)'
What can I do to ensure that static linking with cppunit succeeds? 如何确保与cppunit的静态链接成功?
Try to link your program and the library in another order 尝试以其他顺序链接程序和库
g++ test.cpp -l:libcppunit.a
Or 要么
g++ test.cpp /usr/lib/x86_64-linux-gnu/libcppunit.a
Or 要么
g++ test.cpp -static -lcppunit -Wl,-Bdynamic
The last -Wl,-Bdynamic
is required to avoid static linkage of system libraries like libc++ or glibc. 最后一个-Wl,-Bdynamic
是必需的,以避免像libc ++或glibc这样的系统库静态链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.