1

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
              )
MyDeveloperDay
  • 2,485
  • 1
  • 17
  • 20
  • Add the source file into visual studio solution (e.g., as you do that in your code under `[Update 1]`) and create target/command for copy the file into build directory. What is wrong with this approach? As for copiing file, see also [that question](http://stackoverflow.com/questions/33938239/cmake-does-not-copy-file-in-custom-command). – Tsyvarev Dec 01 '15 at 09:24

1 Answers1

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149