一, 目录结构
├── CMakeLists.txt├── include│ └── Hello.h└── src ├── Hello.cpp └── main.cpp
* 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才能编译代码。
转载于:https://www.cnblogs.com/svenzhang9527/p/10703260.html