Creating a directory in CMake

cmake

Solution

When do you want to create the directory?

At build system generation

To create a directory when CMake generates the build system,

file(MAKE_DIRECTORY ${directory})

At build time

In the `add_custom_command()` command (which adds a custom build rule to the generated build system), and the `add_custom_target()` command (which adds a target with no output so it will always be built), you specify the commands to execute at build time. Create a directory by executing the command `${CMAKE_COMMAND} -E make_directory`. For example:

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

At install time

To create a directory at install time,

install(DIRECTORY DESTINATION ${directory})

Problem

In CMake, I want to create a directory if it doesn't already exist. How can I do this?

Original source