一, 目錄結構ui
├── CMakeLists.txt
├── include
│ └── static
│ └── Hello.h
└── src
├── Hello.cpp
└── main.cppthis
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:include/static/Hello.h[] - The header file to include
* link:src/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with mainget
二,cmake基本腳本域名
cmake_minimum_required(VERSION 3.5)it
project(hello_library)table
# 根據庫文件代碼生成靜態庫
add_library(hello_library STATIC src/Hello.cpp)require
# 包含指定頭文件所在的目錄
target_include_directories(hello_library PUBLIC ${PROJECT_SOURCE_DIR}/include)擴展
# 建立可執行程序List
add_executable(hello_binary
src/main.cpp
)file
# 連接靜態庫文件
target_link_libraries( hello_binary PRIVATE hello_library)
三,擴展分析
1. add_library(hello_library STATIC src/Hello.cpp)將會建立 libhello_library.a 名稱的靜態庫。
2. 域名關鍵字
* +PRIVATE+ - the directory is added to this target's include directories* +INTERFACE+ - the directory is added to the include directores for any targets that link this library.* +PUBLIC+ - As above, it is included int his library and also any targets that link this library.