[英]CMake unit structure with many tests
I would like to set up cmake to run on a project with unit testing. 我想设置cmake在带有单元测试的项目上运行。 The project structure is as follows:
项目结构如下:
|---CMakeLists.txt
|---build
|---source
| |---CMakeLists.txt
| |---many .cpp and .h files
|---tests
| |---CMakeLists.txt
| |---many .cpp files
The source files in tests
use Boost unit_test.h
library. tests
的源文件使用Boost unit_test.h
库。
My outer CMakeLists.txt
is as follows: 我的外部
CMakeLists.txt
如下:
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_CXX_FLAGS "-std=c++11")
project(MyProject CXX)
add_subdirectory(source)
add_subdirectory(tests)
enable_testing()
The CMakeLists.txt
in the source
folder is: source
文件夹中的CMakeLists.txt
是:
file( GLOB LIB_SOURCES *.cpp )
file( GLOB LIB_HEADERS *.h )
add_library( MyProject_lib ${LIB_SOURCES} ${LIB_HEADERS} )
And the CMakeLists.txt
in the tests
folder is: 而
tests
文件夹中的CMakeLists.txt
是:
# require old policy to allow for source files containing the name "test"
cmake_policy(SET CMP0037 OLD)
# Locate Boost libraries: unit_test_framework, date_time and regex
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework date_time regex)
include_directories("${CMAKE_SOURCE_DIR}/source" ${Boost_INCLUDE_DIRS})
file(GLOB APP_SOURCES *.cpp)
foreach(testsourcefile ${APP_SOURCES})
string(REPLACE ".cpp" "" testname ${testsourcefile})
add_executable(${testname} ${testsourcefile})
target_link_libraries(${testname} LINK_PUBLIC ${Boost_LIBRARIES} MyProject_lib)
add_custom_target(targetname)
add_custom_command(TARGET targetname POST_BUILD COMMAND ${testname})
endforeach(testsourcefile ${APP_SOURCES})
If I comment the lines with add_custom_target
and add_custom_command
, the result of this configuration is that the library is correctly built and linked, but none of the tests is compiled. 如果我用
add_custom_target
和add_custom_command
注释行,则此配置的结果是正确构建并链接了库,但未编译任何测试。
If I do not comment the two lines with add_custom_target
and add_custom_command
, The error I get by running cmake
is: 如果我不使用
add_custom_target
和add_custom_command
注释这两行, add_custom_target
通过运行cmake
会收到以下错误:
CMake Error at tests/CMakeLists.txt:25 (add_custom_target):
add_custom_target cannot create target "targetname" because another target
with the same name already exists. The existing target is a custom target
created in source directory "/tests". See
documentation for policy CMP0002 for more details.
Since I have a large number of tests, listing them one by one is not practical. 由于我进行了大量测试,因此将它们一一列出是不切实际的。 I would like to have cmake to find all the .cpp files, as done for the library in the source folder, compile and run them.
我想让cmake像在源文件夹中为库一样查找所有.cpp文件,然后编译并运行它们。
This is the solution I use in my library: 这是我在库中使用的解决方案:
FILE( GLOB log_testprograms *.cpp )
FOREACH( testsource ${log_testprograms} )
GET_FILENAME_COMPONENT( filename ${testsource} NAME_WE )
celma_add_celma_boost_testprogram( ${filename} )
ENDFOREACH()
celma_add_celma_boost_testprogram
is a macro like this: celma_add_celma_boost_testprogram
是这样的宏:
macro( celma_add_celma_boost_testprogram filename )
add_executable( ${filename} ${filename}.cpp )
target_link_libraries( ${filename} celma ${Boost_Test_Link_Libs} )
add_test( ${filename} ${CMAKE_CURRENT_BINARY_DIR}/${filename} )
endmacro( celma_add_celma_boost_testprogram )
Celma is the name of my library, the variable Boost_Test_Link_Libs
contains the list of Boost libraries to link against, including the Boost.Test library. Celma是我的库的名称,变量
Boost_Test_Link_Libs
包含要链接的Boost库列表,包括Boost.Test库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.