链接错误与cppunit,cmake,ubuntu

时间:2014-10-28 21:47:48

标签: c++ cppunit

对不起,这可能是一个完整的菜鸟问题,但我很绝望。我想在Ubuntu上使用KDevelop / CMake运行cppunit

我安装了libcppunit-dev:

markus@hp-notebook:~$ 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)

和我这样简单:

#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。 我认为我只是犯了一些愚蠢的错误,但任何帮助都会非常感激。

2 个答案:

答案 0 :(得分: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 :(得分:-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&gt; const&amp;)' CMakeFiles / test.dir / canonunittest.cpp.o:在函数`CppUnit :: TestSuiteFactory :: makeTest()'中: