Binglong's space

Random notes on computer, phone, life, anything

Build Project with CMake

Posted by binglongx on July 10, 2024

While some projects use traditional configure and make approach, a lot of projects now use CMake to help building.

CMake is a meta build system. CMake itself does not build your project.

  • Projects using CMake would store project settings in CMakeLists.txt files.
  • You can run cmake to create actual project files suitable for concrete build tools to build. Examples are:
    • cmake generates Makefile, to be consumed by make to build the project;
    • cmake generates Xcode project files, to be consumed by Xcode to build the project on macOS;
    • cmake generates Visual Studio project files, to be consumed by Visual Studio/C++ to build the project on Windows;

As you can see, CMake enables you to create projects with build portability.

The diagram above is generated from Mermaid:

%% Mermaid drawing, view with https://mermaid.live/
flowchart LR

    classDef Executable fill:#411,stroke-width:2px;
    classDef File fill:#141,stroke-width:2px;

    src[source code]:::File
    cmake_list[CMakeLists.txt]:::File 

    cmake([cmake]):::Executable
    cmakex([cmake -G Xcode]):::Executable

    makefile[Makefile]
    xproj[Xcode project]
    
    make([make]):::Executable
    make_c([cmake --build]):::Executable
    xcode([Xcode]):::Executable

    exe[executable]
    exe_x[executable]

    cmake_list --> cmake --> makefile
    cmake_list --> cmakex --> xproj

    makefile --> make --> exe
    makefile --> make_c --> exe
    xproj --> xcode --> exe_x

    src --> make
    src --> make_c
    src --> xcode

    subgraph source code repo
        cmake_list
        src
    end

    subgraph cmake stage
        cmake
        cmakex
        makefile
        xproj
    end

    subgraph build stage
        make
        make_c
        xcode
        exe
        exe_x
    end

One Response to “Build Project with CMake”

  1. […] Build Project with CMake […]

Leave a comment