CMake

CMake 官方教程

一、CMake介绍

CMake 是个一个开源的跨平台自动化建构系统,用来管理软件建置的程序,并不依赖于某特定编译器,并可支持多层目录、多个应用程序与多个函数库。
CMake 通过使用简单的配置文件 CMakeLists.txt,自动生成不同平台的构建文件(如 Makefile、Ninja 构建文件、Visual Studio 工程文件等),简化了项目的编译和构建过程。
CMake 本身不是构建工具,而是生成构建系统的工具,它生成的构建系统可以使用不同的编译器和工具链。

从创建项目到编译和运行的整个过程包含以下步骤:

  1. 创建 CMakeLists.txt 文件:定义项目、库、可执行文件和测试。
  2. 编写源代码和测试:编写代码和测试文件。
  3. 创建构建目录:保持源代码目录整洁。
  4. 配置项目:生成构建系统文件。
  5. 编译项目:生成目标文件。
  6. 运行可执行文件:执行程序。
  7. 运行测试:验证功能正确性。
  8. 使用自定义命令和目标:执行额外操作。
  9. 跨平台和交叉编译:支持不同平台和架构。

二、实战举例

具体命令用法查看官方文档

假设项目结构如下:

.
├── CMakeLists.txt
├── MathFunctions
│   ├── CMakeLists.txt
│   ├── MathFunctions.cxx
│   ├── MathFunctions.h
│   ├── mysqrt.cxx
│   └── mysqrt.h
├── TutorialConfig.h.in
└── tutorial.cxx

根目录下的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# Replace the following code by:
# * Creating an interface library called tutorial_compiler_flags
#   Hint: use add_library() with the INTERFACE signature
# * Add compiler feature cxx_std_11 to tutorial_compiler_flags
#   Hint: Use target_compile_features()

# specify the C++ standard
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add the MathFunctions library
add_subdirectory(MathFunctions)
list(APPEND "${PROJECT_SOURCE_DIR}/MathFunctions")

# add the executable
add_executable(Tutorial tutorial.cxx)

# Link Tutorial to tutorial_compiler_flags
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

MathFunctions 目录下的 CMakeLists.txt 文件:

add_library(MathFunctions MathFunctions.cxx)

# State that anybody linking to MathFunctions needs to include the
# current source directory, while MathFunctions itself doesn't.
# Hint: Use target_include_directories with the INTERFACE keyword
target_include_directories(MathFunctions
                           INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
                           )

# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)
  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")

  # library that just does sqrt
  add_library(SqrtLibrary STATIC
              mysqrt.cxx
              )

  # Link SqrtLibrary to tutorial_compiler_flags
  target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)

  target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

# Link MathFunctions to tutorial_compiler_flags
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)

编译步骤:

/home/bb/cmake-4.0.0-tutorial-source/Step2# mkdir build
/home/bb/cmake-4.0.0-tutorial-source/Step2# cd build
/home/bb/cmake-4.0.0-tutorial-source/Step2# cmake ..
/home/bb/cmake-4.0.0-tutorial-source/Step2# cmake --build .
/home/bb/cmake-4.0.0-tutorial-source/Step2# ./Tutorial 25 // 运行
posted @ 2025-07-18 15:44  DanceMonkey26  阅读(14)  评论(0)    收藏  举报