在CMake中创建目录


143

在CMake中,如果目录不存在,我想创建一个目录。我怎样才能做到这一点?

Answers:


250

您何时要创建目录?

在生成系统时

要在CMake生成构建系统时创建目录,

file(MAKE_DIRECTORY ${directory})

在构建时

add_custom_command()命令(将自定义构建规则添加到生成的构建系统中)和add_custom_target()命令(添加无输出目标,以便始终构建目标)中,您指定要在构建时执行的命令。通过执行命令创建目录${CMAKE_COMMAND} -E make_directory。例如:

add_custom_target(build-time-make-directory ALL
    COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})

在安装时

要在安装时创建目录,

install(DIRECTORY DESTINATION ${directory})

简洁快速。立即击中目标!谢谢。
8:51

25

要在安装时创建目录,

install(DIRECTORY DESTINATION ${directory})

这些将在配置时运行:

file(MAKE_DIRECTORY ${directory})
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})

要在构建期间创建,请使用自定义目标:

add_custom_target(mytargetname ALL COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})

10

除了Chin Huang的回复,您还可以在构建时使用以下方法进行操作add_custom_command

add_custom_command(TARGET ${target_name} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})

您还可以更改使用PRE_BUILD| 创建目录的时刻。PRE_LINK| POST_BUILD参数。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.