0

我是 CMake 的新手,但我能够为我的 C++ 项目设置 CMakeLists.txt 文件,它有点像这个设置,顺便说一句。我需要使用 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] ~> 

我做错了什么?

4

1 回答 1

0

这似乎是拼写错误,我的错!

在该行set(LIBS ${LIBS} ${UNITTEST_LIBRARIES} ResourceManager)而不是${UNITTEST_LIBRARIES}它应该是${UNITTEST++_LIBRARY}.

单元测试二进制文件现在链接成功,哈哈。

于 2013-12-08T23:42:00.517 回答