国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

CMake 備忘清單

本清單提供了對 CMake 的入門簡要概述,以及 CMake 常用示例

入門

Hello CMake

CMake 是一個(gè)用于配置跨平臺源代碼項(xiàng)目應(yīng)該如何配置的工具建立在給定的平臺上。

├── CMakeLists.txt  # 希望運(yùn)行的 CMake命令
├── main.cpp        # 帶有main 的源文件
├── include         # 頭文件目錄
│?? └── header.h
└── src             # 源代碼目錄
    ├── a.c
    └── b.c

在此項(xiàng)目上運(yùn)行 CMake 時(shí),系統(tǒng)會要求您提供二進(jìn)制目錄,運(yùn)行 CMake 不會創(chuàng)建最終的可執(zhí)行文件,而是會為 Visual StudioXCodemakefile 生成項(xiàng)目文件。 使用這些工具構(gòu)建該項(xiàng)目

CMakeLists.txt

# 設(shè)置可以使用的最低 CMake 版本
cmake_minimum_required(VERSION 3.5)
# 設(shè)置項(xiàng)目名稱
project (hello_cmake)
# 添加可執(zhí)行文件
add_executable(hello_cmake main.cpp)
# 添加頭文件目錄
target_include_directories(hello_cmake PRIVATE ./include)
# 批量添加源文件
file(GLOB SRCS CONFIGURE_DEPENDS ./src/*.cpp)
target_sources(hello_cmake PUBLIC ${SRCS})
# 添加第三方庫
find_package(OpenGL CONFIG REQUIRED)
# 鏈接第三方庫
target_link_libraries(hello_cmake PRIVATE OpenGL)
# 指定輸出路徑
set_property(TARGET hello_cmake ${CMAKE_SOURCE_DIR}/bin)

main.cpp

#include <iostream>

int main(int argc, char *argv[])
{
  std::cout << "Hello CMake!" << std::endl;
  return 0;
}

編譯示例

$ mkdir build   # 創(chuàng)建 build 目錄
$ cd build      # 進(jìn)入目錄
$ cmake ..      # 目錄的上一級目錄運(yùn)行命令
$ make          # 使用對應(yīng)的編譯工具
$ ./hello_cmake # 運(yùn)行生成的 hello_cmake
Hello CMake!

cmake

生成項(xiàng)目構(gòu)建系統(tǒng)

$ cmake [<options>] <path-to-source | path-to-existing-build>bash
$ cmake [<options>] -S <path-to-source> -B <path-to-build>

建立一個(gè)項(xiàng)目

$ cmake --build <dir> [<options>] [-- <build-tool-options>]

安裝項(xiàng)目

$ cmake --install <dir> [<options>]

運(yùn)行指定項(xiàng)目

cmake --build <dir> --target <project>

打開一個(gè)項(xiàng)目

$ cmake --open <dir>

運(yùn)行腳本

$ cmake [-D <var>=<value>]... -P <cmake-script-file>

運(yùn)行命令行工具

$ cmake -E <command> [<options>]

運(yùn)行查找包工具

$ cmake --find-package [<options>]

運(yùn)行工作流預(yù)設(shè)

$ cmake --workflow [<options>]

查看幫助

$ cmake --help[-<topic>]

常用參數(shù)

  • 方式一: 在CMakeLists.txt中使用set(KEY VAL)函數(shù)
  • 方式二: 在執(zhí)行cmake ... -D 指定(只需一次,推薦)
# 指定編譯參數(shù)(Debug/Release/MinSizeRel/RelWithDebInfo)
$ cmake ... -D CMAKE_BUILD_TYPE=DEBUG
# 指定編譯鏈工具(windows下vcpkg需要)
$ cmake ... -D CMAKE_TOOLCHAIN_FILE=<vcpkg_path>/scripts/buildsystems/vcpkg.cmake
# 指定編譯器
$ cmake ... -D CAMKE_C_COMPILER=...
$ cmake ... -D CAMKE_CXX_COMPILER=...
# 指定生成器
$ cmake .. -G "Unix Makefile"
$ cmake .. -G "Ninja"
$ cmake .. -G "Visual Studio 17 2022"

# 設(shè)置Cpp標(biāo)準(zhǔn)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # 在檢測到不支持時(shí)出錯(cuò)
set(CMAKE_CXX_EXTENSIONS ON) #一般設(shè)為off,否則在msvc上沒有特性會出錯(cuò)

另見