CMake Tutorial: Building and Managing Projects

Click the above“Mechanical and Electronic Engineering Technology” to follow us
CMake is a free, cross-platform build tool designed for building, testing, and packaging software. CMake is used to control the compilation process of software using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used with your chosen compiler environment.
By writing a CMakeLists.txt file, you can control the generated Makefile, thus controlling the compilation process. The Makefile generated by CMake not only allows building project targets using the make command, but also supports installation (make install), testing whether the installed program runs correctly (make test or ctest), generating installation packages for the current platform (make package), generating source packages (make package_source), producing Dashboard display data and uploading, and other advanced features, all of which can be accomplished with simple configuration in the CMakeLists.txt, including writing test cases.
If there are nested directories, each subdirectory can have its own CMakeLists.txt.

CMake Tutorial: Building and Managing Projects

CMake Tutorial: Building and Managing Projects

1. Macro Functions

PROJECT_SOURCE_DIR

The directory immediately following the cmake command, usually the root directory of the project

PROJECT_BINARY_DIR

The directory where the cmake command is executed

CMAKE_CURRENT_SOURCE_DIR

The path where the current CMakeLists.txt being processed is located

CMAKE_CURRENT_BINARY_DIR

The compilation directory

EXECUTABLE_OUTPUT_PATH

Redefines the storage location for the target binary executable file

LIBRARY_OUTPUT_PATH

Redefines the storage location for the target link library files

PROJECT_NAME

Returns the project name defined by the PROJECT command

CMAKE_BINARY_DIR

The actual build path of the project; assuming the build is done in the build directory, this will be the path of that directory

2. Common Commands

CMake uses # for line comments, which can be placed anywhere.

CMake uses #[[ ]] for block comments.

Specify the minimum version of CMake to use

cmake_minimum_required

Define that the project will generate an executable program

add_executable(executable_name source_file_name)

Specify the C++ standard to use

set(CMAKE_CXX_STANDARD 11)

Define a variable

SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])

set(SRC_LIST add.c div.c main.c mult.c sub.c)

set(SRC_LIST add.c;div.c;main.c;mult.c;sub.c)

add_executable(app ${SRC_LIST})

Specify the output path

set(HOME /home/robin/Linux/Sort)

set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin)

Find all source files in a given path

aux_source_directory(

)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_LIST)

add_executable(app ${SRC_LIST})

file(GLOB/GLOB_RECURSE variable_name file_path_and_file_type)

file(GLOB MAIN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)

file(GLOB MAIN_HEAD ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

Include header files

include_directories(headpath)

include_directories(${PROJECT_SOURCE_DIR}/include)

Create a static library

add_library(library_name STATIC source_file1 [source_file2] …)

Create a dynamic library

add_library(library_name SHARED source_file1 [source_file2] …)

Specify the output path for static and dynamic libraries

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

Include static library files

link_libraries( […])

link_directories()

link_directories(${PROJECT_SOURCE_DIR}/lib)

link_libraries(calc)

Include dynamic library files

target_link_libraries(A B C)

target_link_libraries(D A)

target_link_libraries(app pthread)

Macro Definitions

add_definitions(-Dmacro_name)

3. Examples

3.1 Single Source File

Step One: Write the source code. The demo1 folder contains only one source file main.c, with the following content:
#include <stdio.h>#include <stdlib.h>/** * power - Calculate the power of number. * @param base: Base value. * @param exponent: Exponent value. * * @return base raised to the power exponent. */double power(double base, int exponent){    int result = base;    int i;        if (exponent == 0) {        return 1;    }        for(i = 1; i < exponent; ++i){        result = result * base;    }    return result;}int main(int argc, char *argv[]){    if (argc < 3){        printf("Usage: %s base exponent \n", argv[0]);        return 1;    }    double base = atof(argv[1]);    int exponent = atoi(argv[2]);    double result = power(base, exponent);    printf("%g ^ %d is %g\n", base, exponent, result);    return 0;}

Step Two: Write the CMakeLists.txt file and save it in the same directory as main.cc source file:

# CMake minimum version requiredcmake_minimum_required (VERSION 2.8)# Project informationproject (Demo1)# Specify the target to generateadd_executable(Demo main.cc)
The syntax of CMakeLists.txt is relatively simple, consisting of commands, comments, and whitespace, where commands are case insensitive. The content after the symbol # is considered a comment. Commands consist of the command name, parentheses, and parameters, with parameters separated by spaces. In the above CMakeLists.txt file, several commands appear in sequence:
cmake_minimum_required: Specifies the minimum version of CMake required to run this configuration file;
project: The parameter value is Demo1, indicating that the project name is Demo1.
add_executable: Compiles the source file named main.cc into an executable file named Demo.
Step Three: Execute cmake . in the current directory to obtain the Makefile, then use the make command to compile and obtain the Demo1 executable file.

Note:

  • cmake .. indicates looking for the CMakeLists.txt file in the parent directory.

  • cmake . indicates looking for the CMakeLists.txt file in the current directory.

3.2 Multiple Source Files in the Same Directory

Step One: Write the source code. The demo2 folder contains three files: main.c, MathFunctions.c, and MathFunctions.h.

Step Two: Write the CMakeLists.txt file and save it in the same directory as main.cc source file:

# CMake minimum version requiredcmake_minimum_required (VERSION 2.8)# Project informationproject (Demo2)# Specify the target to generateadd_executable(Demo main.cc MathFunctions.cc)

Find all source files in the specified directory

# CMake minimum version requiredcmake_minimum_required (VERSION 2.8)# Project informationproject (Demo2)# Find all source files in the current directory# and save the names to the DIR_SRCS variableaux_source_directory(. DIR_SRCS)# Specify the target to generateadd_executable(Demo ${DIR_SRCS})

Step Three: Execute cmake .,

3.3 Multiple Directories, Multiple Source Files

Step One: Write the source code. The demo3 folder contains a file main.c and a folder math, the math folder contains two files: MathFunctions.c and MathFunctions.h.

Step Two: Write the CMakeLists.txt file and save it in the same directory as main.cc source file:
# CMake minimum version requiredcmake_minimum_required (VERSION 2.8)# Project informationproject (Demo3)# Find all source files in the current directory# and save the names to the DIR_SRCS variableaux_source_directory(. DIR_SRCS)# Add math subdirectoryadd_subdirectory(math)# Specify the target to generate add_executable(Demo main.cc)# Add link librarytarget_link_libraries(Demo MathFunctions)
Step Three: Execute cmake .,
References
1. https://subingwen.cn/cmake/CMake-primer/
CMake Tutorial: Building and Managing Projects

Want to learn more

Quickly scan the code to follow

Leave a Comment