如何将外部测试文件添加到cmake项目

编程入门 行业动态 更新时间:2024-10-25 10:26:25
本文介绍了如何将外部测试文件添加到cmake项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在通过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 )

推荐答案

使用 add_custom_command()添加构建后步骤, a href = cmake/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" )

参考

  • 在CMake的后期构建步骤中,将目标文件复制到另一个位置
  • Copy target file to another location in a post build step in CMake

更多推荐

如何将外部测试文件添加到cmake项目

本文发布于:2023-11-14 11:32:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   文件   测试   项目   cmake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!