一, 目录结构

├── CMakeLists.txt
├── include
│          └── Hello.h
└── src
          ├── Hello.cpp
          └── main.cpp

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

* link:CMakeLists.txt[CMakeLists.txt] - Contains the CMake commands you wish to run.
* link:include/Hello.h[include/Hello.h] - The header file to include.
* link:src/Hello.cpp[src/Hello.cpp] - A source file to compile.
* link:src/main.cpp[src/main.cpp] - The source file with main.

 

二,cmake基本脚本

cmake_minimum_required(VERSION 3.5)

project (hello_headers)

# 创建源文件变量,列出所有源文件,并设置到变量中
set(SOURCES
src/Hello.cpp
src/main.cpp
)

# 可执行程序中添加源文件
add_executable(hello_headers ${SOURCES})

# 设置要包含的目录, g++编译时将include头文件,形式为 -l/path/
target_include_directories(hello_headers PRIVATE  ${PROJECT_SOURCE_DIR}/include)

三,扩展分析

1. 目录路径

另有专门的文章描述

2. 添加源文件的另外一种方式,采用通配符的方式添加子目录中的所有指定类型的文件

file(GLOB SOURCES "src/*.cpp")

但是这种方式每次添加文件时,必须重新执行cmake才能编译代码。

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄