C语言 使用 CMake 安装其他文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15694347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:51:56  来源:igfitidea点击:

Installing additional files with CMake

ccmakeopencl

提问by Constantin

I am attempting to supply some "source" files with some executables. I was wondering if there was a way to copy these source files to the build directory (From the source directory) then to the install directory using CMake.

我试图提供一些带有一些可执行文件的“源”文件。我想知道是否有办法使用 CMake 将这些源文件复制到构建目录(从源目录)然后复制到安装目录。

My more specific goal here is to include OpenCL kernels that I write in their own *.cl files.

我在这里更具体的目标是包含我在他们自己的 *.cl 文件中编写的 OpenCL 内核。

Example:

例子:

mkdir build
cd build
cmake ..
make

Now my directory should have an executable (standard CMake) and some_opencl_kernel.clwhich I open in my executable.

现在我的目录应该有一个可执行文件(标准 CMake)some_opencl_kernel.cl,我在我的可执行文件中打开它。

回答by Fraser

You can copy the file to your build tree using add_custom_commandby adding something like the following:

您可以add_custom_command通过添加以下内容将文件复制到构建树中:

add_custom_command(TARGET MyExe POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy_if_different
                       ${CMAKE_CURRENT_SOURCE_DIR}/src/some_opencl_kernel.cl
                       $<TARGET_FILE_DIR:MyExe>
                   )

This adds a post-build event to your target (I've called it MyExe) which copies the file src/some_opencl_kernel.clto the same directory in your build tree as your executable.

这会向您的目标(我称之为MyExe)添加一个构建后事件,该事件将文件复制src/some_opencl_kernel.cl到构建树中与可执行文件相同的目录中。

There are various other ways of copying a file into the build tree, but I like this one since it uses the "generator expression" $<TARGET_FILE_DIR:MyExe>to identify the location of the executable's directory in the build tree. This can vary depending on e.g. build-type or platform, so the generator expression is about the most reliable, cross-platform way of specifying this location I feel.

有多种其他方法可以将文件复制到构建树中,但我喜欢这种方法,因为它使用“生成器表达式”$<TARGET_FILE_DIR:MyExe>来标识构建树中可执行文件目录的位置。这可能因构建类型或平台而异,因此生成器表达式是我认为指定此位置的最可靠的跨平台方式。

As for installing, you can just use the install(FILES ...)command. Assuming for your executable you have something like:

至于安装,你可以直接使用install(FILES ...)命令。假设您的可执行文件有以下内容:

install(TARGETS MyExe RUNTIME DESTINATION bin)

you can just add:

你可以添加:

install(FILES src/some_opencl_kernel.cl DESTINATION bin)

which will install it to ${CMAKE_INSTALL_PREFIX}/bin/some_opencl_kernel.clalongside the executable.

这将把它安装到${CMAKE_INSTALL_PREFIX}/bin/some_opencl_kernel.cl可执行文件旁边。

回答by vgonisanz

If you want to copy a folder's tree with some type of files:

如果要使用某种类型的文件复制文件夹的树:

  # Copy all assets to resources file
  INSTALL(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/assets/ DESTINATION ${INSTALL_PATH}/assets
          FILES_MATCHING PATTERN "*.dae"  PATTERN "*.jpg")

If you want to copy all files in folders, just remove the FILES_MATCHING patterns.

如果要复制文件夹中的所有文件,只需删除 FILES_MATCHING 模式。