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
#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)
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.
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 .,
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.
# 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)

Want to learn more
Quickly scan the code to follow