Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compilation groups with cmake

Tags:

c++

cmake

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.

like image 865
tnorgd Avatar asked Jun 18 '15 15:06

tnorgd


1 Answers

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

like image 173
lakshayg Avatar answered Sep 21 '22 12:09

lakshayg