I have a large project with multiple subdirectories. In the parent directory, I have a CMakeLists.txt file which calls functions defined in other cmake
files in the same parent directory. I have a custom Makefile
in one of the subdirectories that contains some target "run"
. When I call cmake
from the parent directory, I want the "run"
target located in the subdirectory makefile to execute. How should I do this ?
I understand that some people have suggested to use add_custom_target
and add_custom_command
, but I am still confused as to how to apply these commands to accomplish this task.
CMake will create makefiles inside many of the folders inside a build directory and you can run make or make -j8 in any of these directories and it will do the right thing. You can even run make in your src tree, but since there is no Makefile in your source tree, only CMakeLists. txt files, nothing will happen.
CMake executes the child process using operating system APIs directly: On POSIX platforms, the command line is passed to the child process in an argv[] style array. On Windows platforms, the command line is encoded as a string such that child processes using CommandLineToArgvW will decode the original arguments.
cmake is a system to generate make files based on the platform (i.e. CMake is cross platform) which you can then make using the generated makefiles. While make is you directly writing Makefile for a specific platform that you are working with. If your product is crossplatform, then cmake is a better choice over make .
CMake is an extensible, open-source system that manages the build process in an operating system and in a compiler-independent manner. Unlike many cross-platform systems, CMake is designed to be used in conjunction with the native build environment.
If you know, which file(s) are produced by Makefile in the subdirectory, and want to depend on these files, use add_custom_command
:
add_custom_command(OUTPUT <output-file>
COMMAND make run
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)
This assumes that your CMakeLists.txt
have a target, which depends or uses given file.
Otherwise, if you do not care which files are produced by Makefile, use add_custom_target
:
add_custom_target(<target_name> COMMAND make run
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)
In both cases WORKING_DIRECTORY specifies directory which should be current for command executed.
If you want the target (in the second case) to be executed by default, add ALL option before the COMMAND.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With