2012-04-19 7 views
5

나는 정말 바보 같은 것을 놓치고 있다고 생각합니다.cppunit에 어떻게 링크합니까?

는 I가 설치 libcppunit했다 : (나는 우분투 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

내가 찾은 주위의 작품은 "-Wl을 - 필요에 따라 노이 -"배치 할 수 없습니다 전에 "-lcppunit는" – Naddiseo

답변

8

당신은

이 즉, 컴파일 할 파일을 알려 후 링크 도서관 컴파일러,

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

이 그 것이다 googletest에도이 작업을 수행 할 수 있습니까? –

+2

@MartinPfeffer 컴파일 옵션 후 링크 옵션을 배치하는 것은 일반적으로 * 필요합니다. – steffen

1

나는 (우분투 11.04와) 같은 문제로 실행 말할 필요 우분투에서 bug 인 것으로 보입니다. 당신의 workaround "-Wl, - no-as-needed"는 나를 위해 작동하며 링크 된 버그 보고서의 해결 방법으로도 언급됩니다. 나는 실제 원인을 발견하기에 충분하지 못했습니다.

+0

https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition을 참조하십시오. 슬프게도 이것은 잘못된 접근법 인 것 같습니다. – moggi

1

같아요 근본 원인은

MoneyApp_LDFLAGS = $(CPPUNIT_LIBS) -ldl 
대신 올바른

MoneyApp_LDADD = $(CPPUNIT_LIBS) -ldl 

또는 라인에게 Makefile.am에 추가 할 제안 CppUnit을의 문서 튜토리얼 파일 "money_example.html" 더 정확한

CPPUNIT_LIBS는에서 제공
MoneyApp_LDADD = $(CPPUNIT_LIBS) 

이후 어쨌든. LDFLAGS은 링커 실행 파일 이름 바로 뒤에 플래그를 추가하고 끝에 원래 LDADD을 추가하여 원본 게시물의 오류를 제거합니다. CppUnit을하고 DL (-lcppunit -ldl) 라이브러리와