cmake 编译工程(多文件夹)
我的工程如图所示
liukai@ubuntu:~/test/code$ tree .
.
├── a.out
├── BlueHelper.c
├── BlueHelper.h
├── btgatt-client.c
├── CMakeLists.txt
├── includes
│ ├── lib
│ │ ├── bluetooth.h
│ │ ├── hci.h
│ │ ├── hci_lib.h
│ │ ├── l2cap.h
│ │ └── uuid.h
│ └── src
│ └── shared
│ ├── att.h
│ ├── att-types.h
│ ├── crypto.h
│ ├── gatt-client.h
│ ├── gatt-db.h
│ ├── gatt-helpers.h
│ ├── gatt-server.h
│ ├── io.h
│ ├── mainloop.h
│ ├── queue.h
│ ├── timeout.h
│ └── util.h
├── libbluetooth
│ ├── bluetooth.c
│ ├── CMakeLists.txt
│ ├── hci.c
│ └── uuid.c
└── libshared
├── att.c
├── CMakeLists.txt
├── crypto.c
├── gatt-client.c
├── gatt-db.c
├── gatt-helpers.c
├── gatt-server.c
├── io-mainloop.c
├── mainloop.c
├── queue.c
├── timeout-mainloop.c
└── util.c
6 directories, 38 files
这是典型的多文件工程项目,现在我们要使用cmake将其编译成静态或者动态库。
主要是CMakeLists.txt文件如何编写。如下:
cmake_minimum_required(VERSION 3.0)
#工程名字
project(bThelper)
# 设置库的名称和版本号
set(LIB_NAME bThelper)
// 选择编译器为aarch64,不写的话,默认是gcc
#set(CMAKE_C_COMPILER "/usr/bin/aarch64-linux-gnu-gcc")
# 设置源文件列表
#[[
set(SOURCES
btgatt-client.c
libbluetooth/bluetooth.c
libbluetooth/hci.c
libbluetooth/uuid.c
libshared/att.c
libshared/crypto.c
libshared/gatt-client.c
libshared/gatt-db.c
libshared/gatt-helpers.c
libshared/gatt-server.c
libshared/io-mainloop.c
libshared/mainloop.c
libshared/queue.c
libshared/timeout-mainloop.c
libshared/util.c
)
]]
# #[[ 多行注释的内容]]
#如果嫌文件太多,一个一个添加很麻烦,可以用下面方式添加源文件
FILE(GLOB SOURCES
btgatt-client.c
libbluetooth/*.c
libshared/*.c
)
# 添加头文件的索引目录
include_directories(includes/lib)
include_directories(includes/src/shared)
# 生成动态库
add_library(${LIB_NAME} SHARED ${SOURCES})
#生成静态库
#add_library(${LIB_NAME} STATIC ${SOURCES})
# 设置动态库的版本号
#set_target_properties(${LIB_NAME} PROPERTIES VERSION ${LIB_VERSION})
# 指定头文件的搜索路径
target_include_directories(${LIB_NAME} PUBLIC includes)
#以最小的体积生成库(这个选项会造成编译的库的性能问题,甚至导致某些奇奇怪怪的异常问题,慎用)
target_compile_options(${LIB_NAME} PRIVATE -Os)
# 添加其他依赖(如果需要)
# 链接其他库(如果需要)
# 指定安装路径(如果需要)