Skip to content

CMake

Commandline

cmake -G <generator> -B <build_dir> -D <build_type> <path_to_proj>
# Or
mkdir <build_dir> && cd <build_dir> && cmake -G <generator> ..
  • CMAKE_BUILD_TYPE

    • Debug 產生除錯資訊
    • Release 進行執行速度最佳化
    • RelWithDebInfo 進行執行速度最佳化,但仍然會啟用 debug flag
    • MinSizeRel 進行程式碼最小化
  • Compile

cmake --build <build_dir> --config Release

# makefile
cd <build> && make -j<process>
  • Install
cmake --build <build_dir> --target install

# makefile
cd <build_dir> && make install

CMakeLists.txt

  • Set compile option for a target
    target_compile_options(target_name
        PUBLIC -optionA -optionB ...
    )
    
    • e.g.
      -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Woverloaded-virtual -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wnull-dereference -Wno-sign-compare
      

FetchContent

  • Need cmake version >= 3.14

    cmake_minimum_required(VERSION 3.14)
    # ...
    include(FetchContent)
    FetchContent_Declare(
        DocTest
        GIT_REPOSITORY "https://github.com/onqtam/doctest"
        GIT_TAG "932a2ca50666138256dae56fbb16db3b1cae133a"
    )
    FetchContent_MakeAvailable(DocTest)
    # ...
    target_link_libraries(${PROJECT_NAME} doctest)
    

  • Included libraries must be installable.

References

Build types in CMake
https://codeyarns.com/2015/05/14/build-types-in-cmake/

CMake 入門/加入編譯選項
https://zh.m.wikibooks.org/zh-hant/CMake_%E5%85%A5%E9%96%80/%E5%8A%A0%E5%85%A5%E7%B7%A8%E8%AD%AF%E9%81%B8%E9%A0%85