how do I add external test files to a cmake projec

2019-08-10 04:36发布

问题:

I am building an executable via cmake called myBinary

it is built with 2 C++ source files A.cxx and B.cxx and linked with myLibrary

I have a test file myTest.txt in the same directory

I use a build directory which I make the makefiles file

mkdir build
cd build
cmake ../src

How is it possible for me to add this txt file (myTest.txt) to the cmake file to have it

a) copied to the build directory
b) included in the Visual Studio solution tree when cmake is built on windows


cmake_minimum_required (VERSION 2.8.11)

include_directories(../../framework)

add_executable(myBinary A.cxx B.cxx)

target_include_directories (mylibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries (mylibrary)

[Update 1]

if I add the txt as a source file it will appear in the visual studio solution, but it won't appear in the copied directory,

Also ideally I would need the file copied into the "Debug" directory, i.e. the Target executable directory. That way when I test the myBinary directory the file would be in the correct place.

Think of myBinary as a possible googletest executable where I want to check the reading of a file in the unit test.

add_executable(myBinary 
               A.cxx 
               B.cxx
               myTest.txt
              )

回答1:

You could just add a post-build step with add_custom_command() using generator expressions:

add_custom_command(
    TARGET myBinary 
    POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy  
                     "${CMAKE_CURRENT_SOURCE_DIR}/myTest.txt" 
                     "$<TARGET_FILE_DIR:myBinary>/myTest.txt"
)

Reference

  • Copy target file to another location in a post build step in CMake


标签: cmake