Fundamentals 33 min read

Comprehensive CMake Tutorial: Basics, Advanced Features, and Integration with CLion

This article provides a step‑by‑step guide to using CMake for cross‑platform builds, covering fundamental concepts, basic project setup, adding versioning and configuration headers, creating libraries, optional components, installation, testing, packaging with CPack, dashboard integration, and practical usage within JetBrains CLion for C/C++ development.

Deepin Linux
Deepin Linux
Deepin Linux
Comprehensive CMake Tutorial: Basics, Advanced Features, and Integration with CLion

CMake is an open‑source, cross‑platform build automation tool that generates native build files (e.g., Makefiles, Visual Studio projects) without being tied to a specific compiler, enabling the "write once, run everywhere" workflow.

What is CMake

CMake uses CMakeLists.txt files to describe the build process. It does not compile the final binaries itself; instead, it produces standard build scripts that are processed by the underlying build system.

Basic Build Steps

A minimal project can be built with two lines in CMakeLists.txt :

cmake_minimum_required(VERSION 2.6)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)

The accompanying tutorial.cxx computes the square root of a number and prints the result.

Adding Version Information and Config Header

Version numbers can be injected via a configuration header:

set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
configure_file("${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" "${PROJECT_BINARY_DIR}/TutorialConfig.h")
include_directories("${PROJECT_BINARY_DIR}")

The generated TutorialConfig.h defines Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR , which the source code can use.

Building the Project and Running

On Windows a batch script can automate the process:

echo off
echo build:
cmake -G "Visual Studio 15 2017 Win64" ..
echo compile:
devenv Tutorial.sln /build "Debug|x64"
echo run:
start ./Debug/Tutorial.exe %1

Adding a Library (MathFunctions)

Create a library source mysqrt.cxx and a header MathFunctions.h . Add the library with:

add_library(MathFunctions mysqrt.cxx)
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial MathFunctions)

An option can make the library optional:

option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  set(EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif()
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial ${EXTRA_LIBS})

Installation and Testing

Install rules copy binaries and headers to a destination:

install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include)

CTest can define tests such as:

add_test(TutorialRuns Tutorial 25)
add_test(TutorialComp25 Tutorial 25)
set_tests_properties(TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")

Using CLion with CMake

In CLion, a simple CMakeLists.txt for a C program looks like:

# CMake minimum version
cmake_minimum_required(VERSION 3.10.2)
project(CMakeDemo C)
set(CMAKE_C_STANDARD 99)
add_executable(CMakeDemo main.c)

CLion automatically detects the configuration, allowing you to build and run the executable directly from the IDE.

Advanced CMake Features

Multiple source files can be gathered with aux_source_directory or add_subdirectory . Dynamic libraries are added with add_library . Options, custom commands, and generated files (e.g., a sqrt table) are demonstrated using add_custom_command and add_executable(MakeTable) .

Packaging with CPack

CPack creates binary and source installers. Example configuration:

include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
include(CPack)

Running cpack generates the packages.

Dashboard (CTest) Integration

Enable dashboard scripting with:

include(CTest)
set(CTEST_PROJECT_NAME "Tutorial")

Then run ctest -D Experimental to submit results to Kitware's dashboard.

The article concludes with a detailed curriculum outline for advanced C++/game‑server development, covering topics such as backend architecture, database connection pools, networking models, AI, map handling, and more.

CI/CDAutomationC++packagingTutorialcmakeBuild System
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.