Files
.github
.husky
assets
docs
adb.md
adobe-lightroom.md
adobe-photoshop.md
adobe-xd.md
android-studio.md
ansible.md
apt.md
ascii-code.md
aspect-ratio.md
awk.md
bash.md
blender.md
bun.md
c.md
cargo.md
chatgpt.md
chmod.md
chown.md
cmake.md
cmd.md
cocoapods.md
colors-named.md
conan.md
conda.md
cpp.md
cron.md
cs.md
css.md
curl.md
dart.md
django.md
docker-compose.md
docker.md
dockerfile.md
ejs.md
elasticsearch.md
electron.md
elixir.md
emacs.md
emmet.md
emoji.md
erlang.md
es6.md
expressjs.md
fastapi.md
feds.md
ffmpeg.md
figma.md
find.md
finder.md
firefox.md
flask.md
flutter.md
ftp.md
gdb.md
git.md
github-actions.md
github-copilot.md
github.md
gitlab-ci.md
gitlab.md
glances.md
gmail.md
golang.md
google-chrome.md
graphql.md
grep.md
homebrew.md
hook.md
html-char.md
html.md
htmx.md
htop.md
http-status-code.md
ini.md
intelli-j-idea.md
iptables.md
iso-3166-1.md
iso-639-1.md
java.md
javascript.md
jest.md
jq.md
jquery.md
json.md
julia.md
jupyter.md
justfile.md
koajs.md
kotlin.md
kubernetes.md
laravel.md
latex.md
lerna.md
lessjs.md
linux-command.md
lsof.md
lua.md
make.md
markdown.md
matlab.md
mime.md
mitmproxy.md
mongodb.md
mysql.md
neo4j.md
nestjs.md
netcat.md
netstat.md
nextjs.md
nginx.md
npm.md
nvm.md
openssl.md
oracle.md
package.json.md
pacman.md
pandoc.md
php.md
phpstorm.md
pinia.md
pm2.md
pnpm.md
postgres.md
ps.md
pycharm.md
python.md
pytorch.md
quickreference.md
r.md
react-native.md
react.md
reactrouter.md
redis.md
regex.md
resolutions.md
ruby.md
rust.md
rxjs.md
sass.md
scala.md
screen.md
sdkman.md
sed.md
semver.md
sketch.md
springboot.md
ssh.md
styled-components.md
stylex.md
stylus.md
sublime-text.md
subversion.md
swift.md
swiftui.md
symbol-code.md
sysdig.md
systemd.md
tailwindcss.md
tar.md
taskset.md
tauri.md
tmux.md
toml.md
twitter.md
typescript.md
vim.md
vimium.md
vscode.md
vue.md
vue2.md
webstorm.md
xcode.md
xpath.md
yaml.md
yarn.md
yum.md
zip.md
.dockerignore
.editorconfig
.gitattributes
.gitignore
.lintstagedrc
.markdownlint.json
.npmrc
.prettierignore
.prettierrc
.refsrc.json
CONTRIBUTING.md
Dockerfile
LICENSE
README.md
package.json
renovate.json
reference/docs/cmake.md

163 lines
3.4 KiB
Markdown
Raw Normal View History

2022-11-03 00:27:39 +08:00
CMake 备忘清单
===
本清单提供了对 CMake 的入门简要概述,以及 CMake 常用示例
入门
---
### Hello CMake
CMake 是一个用于配置跨平台源代码项目应该如何配置的工具建立在给定的平台上。
```bash
├── CMakeLists.txt # 希望运行的 CMake命令
├── main.cpp # 带有main 的源文件
├── include # 头文件目录
│   └── header.h
└── src # 源代码目录
├── a.c
└── b.c
2022-11-03 00:27:39 +08:00
```
在此项目上运行 `CMake` 时,系统会要求您提供二进制目录,运行 `CMake` 不会创建最终的可执行文件,而是会为 `Visual Studio``XCode``makefile` 生成项目文件。 使用这些工具构建该项目
#### CMakeLists.txt
```cmake
# 设置可以使用的最低 CMake 版本
cmake_minimum_required(VERSION 3.5)
# 设置项目名称
project (hello_cmake)
# 添加可执行文件
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)
2022-11-03 00:27:39 +08:00
```
#### main.cpp
```c
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello CMake!" << std::endl;
return 0;
}
```
#### 编译示例
```bash
$ mkdir build # 创建 build 目录
$ cd build # 进入目录
$ cmake .. # 目录的上一级目录运行命令
$ make # 使用对应的编译工具
2022-11-03 00:27:39 +08:00
$ ./hello_cmake # 运行生成的 hello_cmake
Hello CMake!
```
### cmake
<!--rehype:wrap-class=col-span-2-->
生成项目构建系统
```bash
$ cmake [<options>] <path-to-source | path-to-existing-build>bash
$ cmake [<options>] -S <path-to-source> -B <path-to-build>
```
建立一个项目
```bash
$ cmake --build <dir> [<options>] [-- <build-tool-options>]
```
安装项目
```bash
$ cmake --install <dir> [<options>]
```
运行指定项目
```bash
cmake --build <dir> --target <project>
```
2022-11-03 00:27:39 +08:00
打开一个项目
```bash
$ cmake --open <dir>
```
运行脚本
```bash
$ cmake [-D <var>=<value>]... -P <cmake-script-file>
```
运行命令行工具
```bash
$ cmake -E <command> [<options>]
```
运行查找包工具
```bash
$ cmake --find-package [<options>]
```
运行工作流预设
```bash
$ cmake --workflow [<options>]
```
查看帮助
```bash
$ cmake --help[-<topic>]
```
#### 常用参数
- 方式一: 在`CMakeLists.txt`中使用`set(KEY VAL)`函数
- 方式二: 在执行`cmake ...` -D<arg> 指定(只需一次,推荐)
```cmake
# 指定编译参数(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"
# 设置Cpp标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # 在检测到不支持时出错
set(CMAKE_CXX_EXTENSIONS ON) #一般设为off否则在msvc上没有特性会出错
```
2022-11-03 00:27:39 +08:00
另见
----
- [CMake Examples](http://ttroy50.github.io/cmake-examples/) _(ttroy50.github.io)_