亚洲成A人片在线观看网站_成年网站免费视频A在线双飞_日日日日做夜夜夜夜无码_久久夜色撩人精品国产小说

CMake 備忘清單

本(ben)清(qing)單提供了對(dui) CMake 的入門簡要概述,以及 CMake 常(chang)用示例

入門

Hello CMake

CMake 是一個用于配置(zhi)跨平(ping)臺(tai)源代(dai)碼項目應該如何配置(zhi)的工具(ju)建立(li)在(zai)給定(ding)的平(ping)臺(tai)上。

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

在此項目上運行 CMake 時,系統會要求您提供二進制目錄,運行 CMake 不會創建最終的可執行文件,而是會為 Visual StudioXCodemakefile 生成項目文件。 使用這(zhe)些(xie)工具構建該項目

CMakeLists.txt

# 設置可以使用的(de)最低 CMake 版本
cmake_minimum_required(VERSION 3.5)
# 設置項目名稱
project (hello_cmake)
# 添加可執行文件
add_executable(hello_cmake main.cpp)
# 添加(jia)頭(tou)文件目錄
target_include_directories(hello_cmake PRIVATE ./include)
# 批(pi)量(liang)添加源文(wen)件
file(GLOB SRCS CONFIGURE_DEPENDS ./src/*.cpp)
target_sources(hello_cmake PUBLIC ${SRCS})
# 添加(jia)第(di)三方(fang)庫
find_package(OpenGL CONFIG REQUIRED)
# 鏈接(jie)第三方(fang)庫(ku)
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   # 創建 build 目錄(lu)
$ cd build      # 進入目錄
$ cmake ..      # 目(mu)錄的上一級目(mu)錄運行命令
$ make          # 使用(yong)對(dui)應的編譯工具
$ ./hello_cmake # 運行生成(cheng)的(de) hello_cmake
Hello CMake!

cmake

生成項目構建系統

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

建立一個項目

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

安裝項目

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

運行指定項目

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

打開一個項目

$ cmake --open <dir>

運行腳本

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

運行命令行工具

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

運行查找包工具

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

運行工作流預設

$ cmake --workflow [<options>]

查看幫助

$ cmake --help[-<topic>]

常用參數

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

# 設置Cpp標(biao)準
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # 在檢(jian)測到不支持時出錯
set(CMAKE_CXX_EXTENSIONS ON) #一般設為off,否(fou)則在msvc上沒有特性會出錯(cuo)

另見

  • (ttroy50.github.io)