2013-12-08 103 views
0

我是新來的CMake,但我能夠爲我的C++項目設置CMakeLists.txt文件,它類似於this setup,順便說一下。我需要使用UnitTest ++進行單元測試,但出於某種原因鏈接單元測試可執行文件失敗。這裏是shell輸出:cmake - UnitTest ++鏈接單元測試二進制文件失敗

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] ~> 

這是我的根的CMakeLists.txt文件:

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] ~> 

,這是關係到我的可執行的單元測試的二進制文件我的CMakeLists.txt文件:

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] ~> 

這裏是與構建我的項目的庫有關的最後一個CMakeLists.txt文件:

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

我在做什麼錯?

回答

0

這似乎是錯字錯誤,我的壞!

在行set(LIBS ${LIBS} ${UNITTEST_LIBRARIES} ResourceManager)而不是${UNITTEST_LIBRARIES}它應該是${UNITTEST++_LIBRARY}

單元測試二進制文件現在鏈接成功,哈哈。