2020-01-20 16:10:21 +08:00
|
|
|
|
---
|
|
|
|
|
layout: post
|
|
|
|
|
title: "CMake 基本语法"
|
|
|
|
|
subtitle: ""
|
|
|
|
|
description: "CMake 的简明语法。"
|
|
|
|
|
excerpt: "通过简明的方式对 CMake 构建系统基本语法进行说明。"
|
|
|
|
|
date: 2020-01-20 15:17:00
|
|
|
|
|
author: "Rick Chan"
|
2020-05-21 15:58:25 +08:00
|
|
|
|
tags: ["Development", "Environment", "CMake"]
|
2020-01-20 16:10:21 +08:00
|
|
|
|
categories: ["Software"]
|
|
|
|
|
published: true
|
|
|
|
|
---
|
2019-12-31 11:55:45 +08:00
|
|
|
|
|
|
|
|
|
CMake 支持大写、小写、混合大小写的命令。
|
|
|
|
|
|
|
|
|
|
## 最小版本号
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
cmake_minimum_required(VERSION 2.8)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 工程名
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
project(<Project Name>)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Include 路径
|
|
|
|
|
|
|
|
|
|
*需要出现在 add_executable 和 add_library 等之前。*
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
include_directories(
|
|
|
|
|
<Path1>
|
|
|
|
|
<Path2>
|
|
|
|
|
...
|
|
|
|
|
<PathN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 添加库路径
|
|
|
|
|
|
|
|
|
|
*需要出现在 add_executable 和 add_library 等之前。*
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
link_directories(
|
|
|
|
|
<Path1>
|
|
|
|
|
<Path2>
|
|
|
|
|
...
|
|
|
|
|
<PathN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 添加库文件
|
|
|
|
|
|
|
|
|
|
*已经被废弃了,需要出现在 add_executable 和 add_library 等之前。*
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
link_libraries(
|
|
|
|
|
<Library1>
|
|
|
|
|
<Library2>
|
|
|
|
|
...
|
|
|
|
|
<LibraryN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
支持直接全路径的写法。
|
|
|
|
|
|
|
|
|
|
## 可执行文件目标和源码
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
add_executable(
|
|
|
|
|
<Executable Name>
|
|
|
|
|
<Source1>
|
|
|
|
|
<Source2>
|
|
|
|
|
...
|
|
|
|
|
<SourceN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 库文件目标和源码
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
add_library(
|
|
|
|
|
<Library>
|
|
|
|
|
<SHARED/STATIC>
|
|
|
|
|
<Source1>
|
|
|
|
|
<Source2>
|
|
|
|
|
...
|
|
|
|
|
<SourceN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 为目标添加库文件
|
|
|
|
|
|
|
|
|
|
*可添加的动态库或静态库,可以在 add_executable 和 add_library 等之后。*
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
target_link_libraries(
|
|
|
|
|
<Target Name>
|
|
|
|
|
<Library1>
|
|
|
|
|
<Library2>
|
|
|
|
|
...
|
|
|
|
|
<LibraryN>
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 添加依赖工程
|
|
|
|
|
|
2020-05-20 16:18:39 +08:00
|
|
|
|
```cpp
|
2019-12-31 11:55:45 +08:00
|
|
|
|
add_dependencies(
|
|
|
|
|
<Target Name>
|
|
|
|
|
<Depend Target1>
|
|
|
|
|
<Depend Target2>
|
|
|
|
|
...
|
|
|
|
|
<Depend TargetN>
|
|
|
|
|
)
|
|
|
|
|
```
|
2019-12-31 12:04:15 +08:00
|
|
|
|
|
|
|
|
|
## 参考资料
|
|
|
|
|
|
|
|
|
|
[cmake手册详解](https://blog.csdn.net/chengde6896383/article/details/81330564)
|
|
|
|
|
[cmake 的link_libraries和target_link_libraries](https://blog.csdn.net/harryhare/article/details/89143410)
|
|
|
|
|
[【学习cmake】cmake如何使用链接库 (link_directories, LINK_LIBRARIES, target_link_libraries,FIND_PACKAGE)实践篇2](https://blog.csdn.net/KYJL888/article/details/85109782)
|