2014-10-28 85 views
0

對不起,這可能是一個完整的noob問題,但我越來越絕望。我想獲得的CppUnit與KDevelop的/ CMake的Ubuntu上運行鏈接錯誤與cppunit,cmake,ubuntu

我安裝libcppunit-dev的:

[email protected]:~$ 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://at.archive.ubuntu.com/ubuntu/ precise/main i386 Packages 
     100 /var/lib/dpkg/status 

我的CMakeLists.txt看起來是這樣的:

project(simpletest) 

include_directories(/usr/local/include/) 
link_directories(/usr/lib/) 

add_executable(simpletest main.cpp) 
LINK_LIBRARIES(simpletest cppunit) 

和我SimpleTest的這樣:

#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++ simpletest.cpp -lcppunit -o simpletest.bin 

一切都編譯和鏈接就好了。但是,如果我生成使用CMake的,我得到的鏈接錯誤:

/home/markus/projects/simpletest/build> make 
-- Configuring done 
-- Generating done 
-- Build files have been written to: /home/markus/projects/simpletest/build 
Scanning dependencies of target simpletest 
[100%] Building CXX object CMakeFiles/simpletest.dir/main.cpp.o 
Linking CXX executable simpletest 
CMakeFiles/simpletest.dir/main.cpp.o: In function `main': 
/home/markus/projects/simpletest/main.cpp:9: undefined reference to `CppUnit::TestFactoryRegistry::getRegistry(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' 
/home/markus/projects/simpletest/main.cpp:11: undefined reference to `CppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)' 
/home/markus/projects/simpletest/main.cpp:12: undefined reference to `CppUnit::TestRunner::addTest(CppUnit::Test*)' 
/home/markus/projects/simpletest/main.cpp:13: undefined reference to `CppUnit::TextTestRunner::result() const' 
/home/markus/projects/simpletest/main.cpp:13: 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&)' 
/home/markus/projects/simpletest/main.cpp:13: undefined reference to `CppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)' 
/home/markus/projects/simpletest/main.cpp:15: undefined reference to `CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool)' 
/home/markus/projects/simpletest/main.cpp:15: undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()' 
/home/markus/projects/simpletest/main.cpp:15: undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()' 
collect2: ld returned 1 exit status 
make[2]: *** [simpletest] Error 1 
make[1]: *** [CMakeFiles/simpletest.dir/all] Error 2 
make: *** [all] Error 2 
*** Failed *** 

由於其工作調用g當++直接我認爲所有的圖書館都存在和運作(沒有編譯器hickup等),但問題是我的CMake文件。 - 圖書館宣言應如何運作 - 例如是名爲libcppunit或cppunit的庫。 我認爲我只是犯了一些愚蠢的錯誤,但任何幫助將不勝感激。

+1

您已通過徹底閱讀[**什麼是未定義的引用/無法解析的外部符號錯誤,以及如何解決它?**],檢查了此錯誤的所有可能原因(http://stackoverflow.com/questions/ 12573816/what-is-unde-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix),是嗎? – 2014-10-28 21:51:31

+0

感謝您的鏈接。我假設我的問題是列表中的第一個(未能鏈接到適當的庫/對象文件或編譯實現文件)。我只是不知道如何告訴Cmake文件在哪裏可以找到正確的庫文件(直接使用g ++它與-lcppunit一起工作)。任何想法,我缺少什麼? – markus 2014-10-28 22:01:08

回答

1

好吧,我剛剛發現了錯誤。我用LINK_LIBRARIES代替target_link_libraries

此的CMakeLists.txt非常有效:

cmake_minimum_required(VERSION 2.8) 
project(simpletest) 

add_executable(simpletest main.cpp) 
target_link_libraries (simpletest cppunit) 

@πάνταῥεῖ感謝出色的鏈路

-1

CMakeFiles/test.dir/canonunittest.cpp.o:在函數CppUnit::AutoRegisterSuite<canonunittest>::AutoRegisterSuite()': canonunittest.cpp:(.text._ZN7CppUnit17AutoRegisterSuiteI13canonunittestEC2Ev[_ZN7CppUnit17AutoRegisterSuiteI13canonunittestEC5Ev]+0x44): undefined reference to CppUnit :: TestFactoryRegistry :: getRegistry(stlp_std :: basic_string,stlp_std :: allocator> const &)' CMakeFiles/test.dir/canonunittest.cpp.o:在函數中`CppUnit :: TestSuiteFactory :: makeTest()' :

+0

我不確定你是否有同樣的問題(鏈接器沒有找到cppunit目標文件)或想共享一個答案,以防萬一。我的解決方案是使用target_link_libraries(yourTarget cppunit)而不是LINK_LIBRARIES。之後,一切正常。 – markus 2014-11-04 15:57:27