2012-04-19 31 views
5

Tôi nghĩ rằng tôi đang thiếu một cái gì đó thực sự ngu ngốc ở đây.Làm cách nào để liên kết tới cppunit?

Tôi đã libcppunit cài đặt: (Tôi đang sử dụng 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 

Và tôi có một thử nghiệm đơn giản:

#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; 
} 

Và tôi đây là trình biên dịch ra:

$ 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 

Để đảm bảo thư viện tồn tại trong/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 

Tôi thiếu điều gì đang gây ra điều này?

+0

Một tác phẩm xung quanh tôi đã tìm thấy là đặt "-Wl, - không như cần thiết" trước khi "-lcppunit" – Naddiseo

Trả lời

8

Bạn phải nói với trình biên dịch mà các thư viện liên kết đến sau bạn nói với nó những file nào để biên dịch, tức là

g++ test.cpp -lcppunit -o test.bin 
+0

là nó có thể làm điều này cho googletest không? –

+2

@MartinPfeffer thông thường là * cần thiết để đặt tùy chọn liên kết sau khi tùy chọn biên dịch. – steffen

1

Tôi chạy vào cùng một vấn đề (với Ubuntu 11.04)

này dường như là một bug trong Ubuntu. Cách giải quyết của bạn "-Wl, - không cần thiết" làm việc cho tôi và cũng được đề cập đến như một giải pháp trong báo cáo lỗi được liên kết. Tôi chưa đi sâu vào nó đủ để khám phá ra nguyên nhân thực sự.

+0

Xem https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition. Đáng buồn thay, điều này dường như là cách tiếp cận sai lầm. – moggi

1

Các nguyên nhân gốc rễ Tôi đoán là CppUnit doc hướng dẫn tập tin "money_example.html" trong đó đề xuất để thêm vào Makefile.am một dòng

MoneyApp_LDFLAGS = $(CPPUNIT_LIBS) -ldl 

thay vì đúng

MoneyApp_LDADD = $(CPPUNIT_LIBS) -ldl 

hoặc thậm chí chính xác hơn

MoneyApp_LDADD = $(CPPUNIT_LIBS) 

kể từ CPPUNIT_LIBS mang đến -ldl ở bất kỳ mức nào. LDFLAGS thêm cờ ngay sau tên thực thi liên kết, LDADD thêm chúng vào cuối, loại bỏ lỗi trong bài đăng gốc.

0

add liên kết với CppUnit và dl (-lcppunit -ldl) thư viện

Các vấn đề liên quan