cmake之编译库

 

简介

下面一个基本库的编译安装为例

目录结构

编译前

math
 ├── CMakeLists.txt
 └── src
     ├── MathFunctions.cc
     └── MathFunctions.h

编译安装后

math
 ├── CMakeLists.txt
 ├── dist
 │   ├── include
 │   │   └── MathFunctions.h
 │   └── lib
 │       ├── libMathFunctions.a
 │       ├── libMathFunctions.dll
 │       └── libMathFunctions.dll.a
 └── src
     ├── MathFunctions.cc
     └── MathFunctions.h

CMakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required (VERSION 3.0)

# 项目信息
project (Demo9)

set(SRC_DIR ./src)
set(LIB_NAME MathFunctions)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(${SRC_DIR} DIR_LIB_SRCS)

# 指定生成 MathFunctions 链接库
add_library (${LIB_NAME} STATIC ${DIR_LIB_SRCS})
add_library (${LIB_NAME}_shared SHARED ${DIR_LIB_SRCS})

# 指定动态库的输出名称
set_target_properties(${LIB_NAME}_shared PROPERTIES OUTPUT_NAME ${LIB_NAME})
# 使动态库和静态库同时存在
set_target_properties(${LIB_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(${LIB_NAME}_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)


message(status "project source dir is ${PROJECT_SOURCE_DIR}")

set(INSTALL_PATH ${PROJECT_SOURCE_DIR}/dist)

# 指定 MathFunctions 库的安装路径
install (TARGETS MathFunctions DESTINATION ${INSTALL_PATH}/lib)
install (TARGETS MathFunctions_shared DESTINATION ${INSTALL_PATH}/lib)
install (FILES ${SRC_DIR}/MathFunctions.h DESTINATION ${INSTALL_PATH}/include)