1

我有以下项目文件夹结构:

project
|-CMakeLists.txt
|
|-src
|  |-mybiglib
|     |-CMakeLists.txt
|
|-tests
   |-mybiglibtest
      |-CMakeLists.txt

CMakeLists.txt对于单元测试项目,我有以下内容。我要测试的库是mybiglib,并且在另一个库中正确构建CMakeLists.txt

cmake_minimum_required (VERSION 3.10.0)
include(CTest)
enable_testing()
project (mybiglibtest)

message (STATUS "Building project ${PROJECT_NAME}")

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/${CMAKE_BUILD_TYPE}/tests)
set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
unset (Boost_INCLUDE_DIR CACHE)
unset (Boost_LIBRARY_DIRS CACHE)
set (CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH})
find_package (Boost COMPONENTS unit_test_framework log system REQUIRED)

include_directories (${CMAKE_SOURCE_DIR}/src ${Boost_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})

set (TEST_SRC
  stdafx.cpp
  Test1.cpp
  )

add_executable ( ${PROJECT_NAME} ${TEST_SRC})
target_link_libraries (${PROJECT_NAME} mybiglib ${Boost_LIBRARIES})
add_custom_command (OUTPUT "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/mybiglib.dll" COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:mybiglib> "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

我在 Visual Studio 2019 中打开了该项目。

如果我执行命令Build All,问题是 .dllmybiglib.dll未复制到tests文件夹中,因此无法启动测试。

相反,如果我执行该命令Rebuild All,库将被复制到测试文件夹中,但显然每次都重新构建非常耗时。

如何在构建时复制测试文件夹中的库,而不仅仅是在重建时?

4

0 回答 0