A target in CMake represents a logical unit in your project, such as an executable, a library, or a custom command. Targets encapsulate properties, dependencies, and build rules, allowing you to manage your project modularly.
Here is a minimal example of a CMakeLists.txt
:
cmake_minimum_required(VERSION 3.31)
project(MyProject
VERSION 1.0
LANGUAGES CXX)
# set compiler configurations
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_VERSION 19.1.5)
# enforce modern C++ standards
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# add executable
add_executable(MyApp
src/main.cpp)
# add include directory
include_directories(include)
# add library
add_library(MyLib
src/MyLib.cpp
src/MyLibHelper.cpp)
# link library to executable
target_link_libraries(MyApp PRIVATE MyLib)
Here 2 target commands are used:
add_executable()
: defines the main executable app namedMyApp
.target_link_libraries()
: links the library to theMyApp
executable.
Created using add_executable()
.
For example, define an executable target from specified source files:
add_executable(MyApp main.cpp)
Created using add_library()
.
For example, create a library target, which can be static, shared, or module:
add_library(MyLib STATIC lib.cpp)
Defines a target that performs custom commands, which is always considered out-of-date and can be used to run arbitrary build steps.
Created using add_custom_target()
for custom build actions.
add_custom_target(run_tests
COMMAND ${CMAKE_COMMAND} -E echo "Running tests..."
COMMAND ${CMAKE_COMMAND} -E touch run_tests.stamp
COMMENT "Executing custom target: run_tests"
)
Associates source files with a target, allowing for dynamic source file specification.
add_library(MyLib STATIC)
target_sources(MyLib
PRIVATE
lib.cpp
PUBLIC
lib.h
)
Sets include directories for a target, specifying where the compiler should look for header files.
add_library(MyLib STATIC lib.cpp)
target_include_directories(MyLib
PUBLIC
${CMAKE_SOURCE_DIR}/include
PRIVATE
${CMAKE_SOURCE_DIR}/src
)
This command specifies that the compiler should search for headers in ${CMAKE_SOURCE_DIR}/
include when compiling MyLib and in ${CMAKE_SOURCE_DIR}/src
only for the target itself.
Specifies libraries that a target depends on.
add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE MyLib)
Adds preprocessor definitions to a target, which can be used to conditionally compile code.
add_library(MyLib STATIC lib.cpp)
target_compile_definitions(MyLib
PRIVATE
MYLIB_INTERNAL=1
PUBLIC
MYLIB_API
)
This defines MYLIB_INTERNAL
for the compilation of MyLib
and MYLIB_API
for both MyLib
and any targets that link against it.
Specifies additional compiler options for a target.
add_executable(MyApp main.cpp)
target_compile_options(MyApp
PRIVATE
-Wall
-Wextra
)
This adds the -Wall
and -Wextra
compiler flags when compiling MyApp.
Specifies that a target depends on other targets, ensuring the dependent targets are built first.
add_executable(MyApp main.cpp)
add_custom_target(generate_code
COMMAND ${CMAKE_COMMAND} -E echo "Generating code..."
)
add_dependencies(MyApp generate_code)
Here, MyApp
depends on generate_code, so the generate_code target will be built before MyApp.