NotePublic/Software/Development/Environment/CMake/CMake_基本语法.md

125 lines
2.1 KiB
Markdown
Raw Normal View History

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