问题描述
我正在通过cmake构建一个名为myBinary的可执行文件
I am building an executable via cmake called myBinary
它是由2个C ++源文件A.cxx和B.cxx构建的,并与myLibrary链接
it is built with 2 C++ source files A.cxx and B.cxx and linked with myLibrary
我在同一目录中有一个测试文件myTest.txt
I have a test file myTest.txt in the same directory
我使用一个生成目录,该目录将makefiles文件/ p>
I use a build directory which I make the makefiles file
mkdir build
cd build
cmake ../src
我怎么可能将此txt文件(myTest.txt)添加到cmake文件中
How is it possible for me to add this txt file (myTest.txt) to the cmake file to have it
a)复制到Visual Studio解决方案树中包含的生成目录
b)
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)
[更新1]
[Update 1]
如果我将txt作为源文件添加,它将出现在Visual Studio解决方案中,但不会出现在复制的目录中,
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,
理想情况下,我还需要将文件复制到 Debug目录中,即Target可执行目录。这样,当我测试myBinary目录时,文件将位于正确的位置。
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.
将myBinary视为可能的googletest可执行文件,我想在其中检查文件的读取情况
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
)
推荐答案
使用添加构建后步骤, a href = https://cmake.org/cmake/help/v3.4/manual/cmake-generator-expressions.7.html#informational-expressions rel = nofollow noreferrer>生成器表达式:
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"
)
参考
- Copy target file to another location in a post build step in CMake
这篇关于如何将外部测试文件添加到cmake项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!