我正在通过使用Visual Studio 2017的C ++爱好项目学习CMake,以及我设置它的方式,我有一个用于源代码的文件夹和一个用于测试代码的文件夹。我将源代码构建为静态库,并将测试代码构建为可执行文件(使用Catch2)。我现在的问题是这些是两个独立的目标,每当重建这些目标中的一个或两个时,我想运行测试可执行文件。现在,我可以了解如何使用ADD_CUSTOM_COMMAND运行后期构建事件,但这仅适用于单个目标。在“TARGET”之后放置多个目标会导致只使用最后一个目标(我对此进行了测试),并且复制自定义命令可能会导致测试运行两次,而且看起来代码样式也很糟糕。有没有办法优雅地做到这一点?我的CMake文件如下所示:
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("SheepyEngine")
set (CMAKE_CXX_STANDARD 17)
set (HEADER_FILES 3rdParty/CImg/CImg.h)
set (SOURCE_DIRECTORY Source)
set (TEST_DIRECTORY Test)
# Include sub-projects.
add_subdirectory ("Source")
add_subdirectory ("Test")
# Include libraries
include_directories (
"${CMAKE_CURRENT_LIST_DIR}/3rdParty/CImg"
"${CMAKE_CURRENT_LIST_DIR}/3rdParty/Catch2/single_include"
)
add_library (SheepyEngine STATIC
"${SOURCE_DIRECTORY}/Game.cpp"
"${SOURCE_DIRECTORY}/Game.h"
"${SOURCE_DIRECTORY}/GameObject.h"
${HEADER_FILES})
target_include_directories(SheepyEngine PRIVATE ${CMAKE_CURRENT_LIST_DIR}/3rdParty/CImg/)
add_executable(SheepyEngineTest "${TEST_DIRECTORY}/test.cpp" "3rdParty/Catch2/single_include/catch.hpp")
target_include_directories(SheepyEngineTest PRIVATE ${CMAKE_CURRENT_LIST_DIR}/3rdParty/Catch2/)
# TODO: Add tests and install targets if needed.
if(${RUN_TESTS})
ADD_CUSTOM_COMMAND(
TARGET SheepyEngineTest SheepyEngine
POST_BUILD
COMMAND ${CMAKE_CURRENT_LIST_DIR}/Build/Debug/SheepyEngineTest.exe
)
endif()
答案 0 :(得分:0)
SheepyTestProgram目标需要依赖于SheepyEngine:
target_link_libraries(SheepyEngineTest SheepyEngine)
然后add_custom_command
的目标只是SheepyEngineTest(add_custom_command
只接受一个目标)。