0

I am sorry this probably a complete noob question but I am getting desperate. I am trying to get cppunit running with KDevelop/CMake on Ubuntu

I installed 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

My CMakeLists.txt looks like this:

project(simpletest)

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

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

and my simpletest like this:

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

If I simply do a:

g++ simpletest.cpp -lcppunit -o simpletest.bin

Everything compiles and links just fine. But if I build using CMake I get linker errors:

/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 ***

Since its working when invoking g++ directly I assume that all the libraries are there and working (no compiler hickup, etc.) but that the problem is with my CMake file. - How is the library declaration supposed to work - e.g. is the library called libcppunit or just cppunit. I assume that I am just making some stupid mistake but any help would be greatly appreciated.

markus
  • 1,631
  • 2
  • 17
  • 31
  • 1
    You have already checked all the possible reasons for this error, by thoroughly reading [**What is an undefined reference/unresolved external symbol error and how do I fix it?**](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix), did you? – πάντα ῥεῖ Oct 28 '14 at 21:51
  • thanks for the link. I assume that my problem is the first in the list (Failure to link against appropriate libraries/object files or compile implementation files). I just don't know how to tell the Cmake file where to find the correct library file (with g++ directly it is working with -lcppunit). Any idea as to what I am missing? – markus Oct 28 '14 at 22:01

2 Answers2

1

Okay I just found the mistake. I used LINK_LIBRARIES instead of target_link_libraries

This CMakeLists.txt works just fine:

cmake_minimum_required(VERSION 2.8)
project(simpletest)

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

@πάντα ῥεῖ Thanks for the excellent link

markus
  • 1,631
  • 2
  • 17
  • 31
-1

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

  • i'm not sure if you are having the same problem (linker not finding cppunit object file) or wanted to share an answer so just in case. My solution was to use target_link_libraries (yourTarget cppunit) instead of LINK_LIBRARIES. After that everything worked fine. – markus Nov 04 '14 at 15:57