I have a project where compilation produces a number of executables. I use cmake to generate Makefiles. Then when I say make
, all of them are compiled. I know I may use make target1
to compile a desired target. But I would like to split all my targets into groups and be able to use, say make group_A
to compile a subset of the targets. How to achieve this?
The project is written in C++ and developed under Linux and OSX.
Have a look at add_custom_target
and add_dependencies
in the CMake documentation. You can add a group as a custom target with the targets you want to build as a dependency for the group.
http://www.cmake.org/cmake/help/v3.2/command/add_custom_target.html http://www.cmake.org/cmake/help/v3.2/command/add_dependencies.html
EDIT (after @m.s.'s comment)
You can do
add_custom_target(<group-name> DEPENDS <target1> <target2> ...)
Here's a small example
hello1.cpp
#include <stdio.h>
int main() {
printf("Hello World 1\n");
return 0;
}
hello2.cpp
#include <stdio.h>
int main() {
printf("Hello World 2\n");
return 0;
}
hello3.cpp
#include <stdio.h>
int main() {
printf("Hello World 3\n");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(groups_demo)
add_executable(hello1 hello1.cpp)
add_executable(hello2 hello2.cpp)
add_executable(hello3 hello3.cpp)
add_custom_target(hello12 DEPENDS hello1 hello2)
add_custom_target(hello23 DEPENDS hello3 hello2)
add_custom_target(hello13 DEPENDS hello1 hello3)
You can now use make hello12
to build hello1
and hello2
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