Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose which CMake executable target will be the default one?

Tags:

build

cmake

I have written a CMakeLists.txt file including 2 executables (target1 and target2):

ADD_EXECUTABLE(target1 ${CXX_FILES})
TARGET_LINK_LIBRARIES(target1 ${requiredlibs})

ADD_EXECUTABLE(target2 ${CXX_FILES} ${OTHER_CXX_FILES})
TARGET_LINK_LIBRARIES(target2 ${requiredlibs})

Now every time when I run make without any parameters both targets are rebuilt. But I want to define target1 as default executable so that running make without any parameters only builds target1. For building target2 I would run make target2.

Is this possible?

In the Makefile created by CMake there is the following definition: default_target: all

I think I need a way to set this default_target to target1.

Another problem I have is that make always rebuilds the targets, even if no source file has been changed.

like image 669
user1346791 Avatar asked Apr 20 '12 14:04

user1346791


People also ask

Where is my CMake executable?

Run cmake-gui.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory.

What is a CMake target?

A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.

What is an executable target?

An IMPORTED executable target references an executable file located outside the project. No rules are generated to build it, and the IMPORTED target property is True . The target name has scope in the directory in which it is created and below, but the GLOBAL option extends visibility.

What is a CMake executable?

The cmake executable is the command-line interface of the cross-platform buildsystem generator CMake. The above Synopsis lists various actions the tool can perform as described in sections below. To build a software project with CMake, Generate a Project Buildsystem.


1 Answers

An example CMakeLists.txt that does what you requested:

ADD_EXECUTABLE(target1 a.c)

ADD_EXECUTABLE(target2 EXCLUDE_FROM_ALL b.c)

For me it does not rebuild the target if the source files are not changed (or modification time did not change). Output I get:

$ make -f Makefile 
Scanning dependencies of target target1
[100%] Building C object CMakeFiles/target1.dir/a.c.o
Linking C executable target1
[100%] Built target target1
[$ make -f Makefile 
[100%] Built target target1

Note that the second make does not rebuild anything.

(you could read the CMake manual for this type of information)

like image 50
vladmihaisima Avatar answered Oct 05 '22 04:10

vladmihaisima