Question

I'm new to CMake, but I was able to setup the CMakeLists.txt files for my C++ project, it resembles a little bit this setup, BTW. I need to use UnitTest++ for unit testing, but for some reason linking the unit test executables fails. Here is the shell output:

jorge [~/coders/desarrollo/santotomasdeaquino] ~> mkdir build
jorge [~/coders/desarrollo/santotomasdeaquino] ~> cd build/
jorge [~/coders/desarrollo/santotomasdeaquino/build] ~> cmake VERBOSE=1 ..
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   filesystem
-- Librerias: /usr/lib64/libboost_filesystem.so
-- Found UnitTest++: /usr/lib64/libUnitTest++.a
-- Librerias: /usr/lib64/libboost_filesystem.so;ResourceManager
-- dir='/usr/include'
-- dir='/home/jorge/coders/desarrollo/santotomasdeaquino/include'
-- dir='/home/jorge/coders/desarrollo/santotomasdeaquino/tests'
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jorge/coders/desarrollo/santotomasdeaquino/build
jorge [~/coders/desarrollo/santotomasdeaquino/build] ~> LC_ALL=C make
[ 50%] Built target ResourceManager
Linking CXX executable test_resourcemanager
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o: In function `TestPruebaDeCMake::RunImpl() const':
test_resourcemanager.cpp:(.text+0x23): undefined reference to `UnitTest::CurrentTest::Details()'
test_resourcemanager.cpp:(.text+0x3a): undefined reference to `UnitTest::TestDetails::TestDetails(UnitTest::TestDetails const&, int)'
test_resourcemanager.cpp:(.text+0x3f): undefined reference to `UnitTest::CurrentTest::Results()'
test_resourcemanager.cpp:(.text+0x56): undefined reference to `UnitTest::TestResults::OnTestFailure(UnitTest::TestDetails const&, char const*)'
test_resourcemanager.cpp:(.text+0x7f): undefined reference to `UnitTest::CurrentTest::Details()'
test_resourcemanager.cpp:(.text+0x96): undefined reference to `UnitTest::TestDetails::TestDetails(UnitTest::TestDetails const&, int)'
test_resourcemanager.cpp:(.text+0x9b): undefined reference to `UnitTest::CurrentTest::Results()'
test_resourcemanager.cpp:(.text+0xb2): undefined reference to `UnitTest::TestResults::OnTestFailure(UnitTest::TestDetails const&, char const*)'
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o: In function `main':
test_resourcemanager.cpp:(.text+0xc8): undefined reference to `UnitTest::RunAllTests()'
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
test_resourcemanager.cpp:(.text+0x10a): undefined reference to `UnitTest::Test::GetTestList()'
test_resourcemanager.cpp:(.text+0x11c): undefined reference to `UnitTest::ListAdder::ListAdder(UnitTest::TestList&, UnitTest::Test*)'
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o: In function `TestPruebaDeCMake::TestPruebaDeCMake()':
test_resourcemanager.cpp:(.text._ZN17TestPruebaDeCMakeC2Ev[_ZN17TestPruebaDeCMakeC5Ev]+0x2c): undefined reference to `UnitTest::Test::Test(char const*, char const*, char const*, int)'
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o: In function `TestPruebaDeCMake::~TestPruebaDeCMake()':
test_resourcemanager.cpp:(.text._ZN17TestPruebaDeCMakeD2Ev[_ZN17TestPruebaDeCMakeD5Ev]+0x1f): undefined reference to `UnitTest::Test::~Test()'
CMakeFiles/test_resourcemanager.dir/test_resourcemanager.cpp.o:(.rodata._ZTI17TestPruebaDeCMake[_ZTI17TestPruebaDeCMake]+0x10): undefined reference to `typeinfo for UnitTest::Test'
collect2: error: ld returned 1 exit status
tests/CMakeFiles/test_resourcemanager.dir/build.make:90: recipe for target 'tests/test_resourcemanager' failed
make[2]: *** [tests/test_resourcemanager] Error 1
CMakeFiles/Makefile2:128: recipe for target 'tests/CMakeFiles/test_resourcemanager.dir/all' failed
make[1]: *** [tests/CMakeFiles/test_resourcemanager.dir/all] Error 2
Makefile:85: recipe for target 'all' failed
make: *** [all] Error 2
jorge [~/coders/desarrollo/santotomasdeaquino/build] ~> 

This is my root CMakeLists.txt file:

jorge [~/coders/desarrollo/santotomasdeaquino] ~> cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")

# project name
project(tomas-aquino)

# In this project, Boost is gonna be use
find_package(Boost 1.48.0 REQUIRED COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${Boost_LIBRARIES})
message(STATUS "Librerias: ${LIBS}")

include_directories(include)
include_directories(tests)

enable_testing()

add_subdirectory(src)
add_subdirectory(tests)
jorge [~/coders/desarrollo/santotomasdeaquino] ~> 

and this is my CMakeLists.txt file related to my executable unit testing binaries:

jorge [~/coders/desarrollo/santotomasdeaquino] ~> cat tests/CMakeLists.txt 
enable_testing()
find_package(UnitTest++ REQUIRED)
include_directories(${UNITTEST++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${UNITTEST_LIBRARIES} ResourceManager)

add_executable(test_resourcemanager test_resourcemanager.cpp)
message(STATUS "Librerias: ${LIBS}")
target_link_libraries(test_resourcemanager ${LIBS})

add_test(test_resourcemanager ${CMAKE_CURRENT_BINARY_DIR}/test_resourcemanager)

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
  message(STATUS "dir='${dir}'")
endforeach()
jorge [~/coders/desarrollo/santotomasdeaquino] ~> 

and here is the last CMakeLists.txt file related to building the libraries of my project:

jorge [~/coders/desarrollo/santotomasdeaquino] ~> cat src/CMakeLists.txt 
include_directories(./tomasaquino)
add_library(ResourceManager ./tomasaquino/ResourceManager.cpp)
jorge [~/coders/desarrollo/santotomasdeaquino] ~> 

What I'm doing wrong?

Was it helpful?

Solution

It seems to be typo errors, my bad!

in the line set(LIBS ${LIBS} ${UNITTEST_LIBRARIES} ResourceManager) instead of ${UNITTEST_LIBRARIES} it should be ${UNITTEST++_LIBRARY}.

The unit test binaries now links successfully, haha.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top