一, 目录结构
├── CMakeLists.txt
├── include
│ └── shared
│ └── Hello.h
└── src
├── Hello.cpp
└── main.cpp
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:include/shared/Hello.h[] - The header file to include
* link:src/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main
二,cmake基本脚本
cmake_minimum_required(VERSION 3.5)
project(hello_library)
# 根据库文件代码生成动态库
add_library(hello_library SHARED src/Hello.cpp)
# 创建动态库别名
add_library(hello::library ALIAS hello_library)
# 包含指定头文件所在的目录
target_include_directories(hello_library PUBLIC ${PROJECT_SOURCE_DIR}/include)
# 创建可执行程序
add_executable(hello_binary
src/main.cpp
)
# 链接动态库文件
target_link_libraries( hello_binary PRIVATE hello::library)
三,扩展分析
1. add_library(hello_library SHARED src/Hello.cpp)将会创建 libhello_library.so 名称的动态库。
2. 目标别名
add_library(hello::library ALIAS hello_library)
hello::library是hello_library的别名, 别名允许使用别名来链接对应的库文件。
原文地址:https://www.cnblogs.com/svenzhang9527/p/10703754.html