我想我在这里错过了一些非常愚蠢的事情。
我安装了libcppunit :(我正在使用Ubuntu 12.04)
$ apt-cache policy libcppunit-dev
libcppunit-dev:
Installed: 1.12.1-4
Candidate: 1.12.1-4
Version table:
*** 1.12.1-4 0
500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
100 /var/lib/dpkg/status
$ apt-cache policy libcppunit-1.12-1
libcppunit-1.12-1:
Installed: 1.12.1-4
Candidate: 1.12.1-4
Version table:
*** 1.12.1-4 0
500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
100 /var/lib/dpkg/status
我有一个简单的测试:
#include <iostream>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
int main() {
CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;
runner.addTest(suite);
runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
return runner.run() ? 0 : 1;
}
我这是编译器输出:
$ g++ -lcppunit -o test.bin test.cpp
/tmp/ccoQDuGC.o: In function `main':
test.cpp:(.text+0x36): undefined reference to `CppUnit::TestFactoryRegistry::getRegistry(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0x75): undefined reference to `CppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)'
test.cpp:(.text+0x8b): undefined reference to `CppUnit::TestRunner::addTest(CppUnit::Test*)'
test.cpp:(.text+0x9a): undefined reference to `CppUnit::TextTestRunner::result() const'
test.cpp:(.text+0xe2): undefined reference to `CppUnit::CompilerOutputter::CompilerOutputter(CppUnit::TestResultCollector*, std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0xf4): undefined reference to `CppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)'
test.cpp:(.text+0x150): undefined reference to `CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool)'
test.cpp:(.text+0x189): undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()'
test.cpp:(.text+0x227): undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()'
collect2: ld returned 1 exit status
为了确保这些库确实存在于/ usr / lib
下$ ls /usr/lib/ | grep cppunit
libcppunit-1.12.so.1
libcppunit-1.12.so.1.0.0
libcppunit.a
libcppunit.la
libcppunit.so
我错过了什么导致了这个?
答案 0 :(得分:8)
您必须告诉编译器在之后链接到的库,告诉它要编译哪些文件,即
g++ test.cpp -lcppunit -o test.bin
答案 1 :(得分:1)
我遇到了同样的问题(使用Ubuntu 11.04)
这在Ubuntu中似乎是bug。您的解决方法“-Wl, - no-as-needed”对我有用,并且在链接的错误报告中也被提及为变通方法。我没有足够深入探究实际原因。
答案 2 :(得分:1)
我猜的根本原因是cppunit doc教程文件“money_example.html”,它建议添加到Makefile.am一行
MoneyApp_LDFLAGS = $(CPPUNIT_LIBS) -ldl
而不是正确的
MoneyApp_LDADD = $(CPPUNIT_LIBS) -ldl
甚至更正确
MoneyApp_LDADD = $(CPPUNIT_LIBS)
因为CPPUNIT_LIBS
无论如何都会带来-ldl
。 LDFLAGS
在链接器可执行文件名后面添加标记,LDADD
最后添加它们,消除了原始帖子中的错误。
答案 3 :(得分:0)
使用cppunit和dl(-lcppunit -ldl)库添加链接