一, 目錄結構
├── 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才能編譯代碼。