一、CMake 是什么
CMake
是一个跨平台的自动化构建系统,它使用配置文件 CMakeLists.txt
来管理软件构建过程。CMake
基于 Makefile
做了二次开发。
二、单个文件目录
# CMake 最低版本号要求
cmake_minimum_required ( VERSION 3.16 . 3 )
# 工程名
project ( CMakeSingle)
# 变量赋值
set ( SRC_LIST main. cpp)
set ( SRC_LIST2 main2. cpp)
# 输出信息
message ( STATUS "this is binary dir" ${ PROJECT_BINARY_DIR} )
message ( STATUS "this is source dir" ${ PROJECT_SOURCE_DIR} )
# 生产执行文件名
add_executable ( main ${ SRC_LIST} )
add_executable ( main2 ${ SRC_LIST2} )
三、子目录
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeSub)
message ( STATUS "PROJECT_BINARY_DIR " ${ PROJECT_BINARY_DIR} )
message ( STATUS "PROJECT_SOURCE_DIR " ${ PROJECT_SOURCE_DIR} )
message ( STATUS "CMAKE_CURRENT_SOURCE_DIR " ${ CMAKE_CURRENT_SOURCE_DIR} )
# 添加子目录
add_subdirectory ( src)
cmake_minimum_required ( VERSION 3.16 . 3 )
message ( STATUS "PROJECT_BINARY_DIR " ${ PROJECT_BINARY_DIR} )
message ( STATUS "PROJECT_SOURCE_DIR " ${ PROJECT_SOURCE_DIR} )
message ( STATUS "CMAKE_CURRENT_SOURCE_DIR " ${ CMAKE_CURRENT_SOURCE_DIR} )
SET ( SRC_LIST main. cpp)
add_executable ( main ${ SRC_LIST} )
add_executable ( main2 ${ SRC_LIST} )
四、多个文件目录,子目录编译成库文件
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeMany1)
add_subdirectory ( src)
cmake_minimum_required ( VERSION 3.16 . 3 )
message ( STATUS "CMAKE_CURRENT_SOURCE_DIR ->" ${ CMAKE_CURRENT_SOURCE_DIR} )
SET ( SRC_LIST main. cpp)
# 添加 dir1 头文件路径
include_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/dir1" ) # 或 include_directories ( dir1) 相对路径的方式
# 添加 dir1 子目录
add_subdirectory ( "${CMAKE_CURRENT_SOURCE_DIR}/dir1" )
# 添加 dir2 头文件路径
include_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/dir2" )
# 添加 dir2 子目录
add_subdirectory ( "${CMAKE_CURRENT_SOURCE_DIR}/dir2" )
add_executable ( zcoder ${ SRC_LIST} )
target_link_libraries ( zcoder dir1 dir2)
# 安装目录到某个路径,
# 默认路径:/ usr/ local/
# 指定路径:cmake - DCMAKE_INSTALL_PREFIX= / . . / build/ . .
# 将执行文件安装到 bin 目录
install ( TARGETS zcoder RUNTIME DESTINATION bin)
# 将目录安装到指定位置
install ( DIRECTORY . . / doc/ DESTINATION share/ doc/ )
# 加载所有的源码
aux_source_directory ( . DIR_SRCS) # <=> set ( DIR_SRCS dir1. cpp dir11. cpp)
# 默认是静态库 . a
# add _library ( dir1 SHARED ${ DIR_SRCS} ) 动态库 . so
add_library ( dir1 ${ DIR_SRCS} )
aux_source_directory ( . DIR_SRCS)
add_library ( dir2 ${ DIR_SRCS} )
五、多个文件目录,子目录使用源码编译
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeMany2)
add_subdirectory ( src)
cmake_minimum_required ( VERSION 3.16 . 3 )
set ( SRC_LIST main. cpp)
# 设置子目录
set ( SUB_DIR_LIST "${CMAKE_CURRENT_SOURCE_DIR}/dir1" "${CMAKE_CURRENT_SOURCE_DIR}/dir2" )
foreach ( SUB_DIR ${ SUB_DIR_LIST} )
# 自动添加头文件
include_directories ( ${ SUB_DIR} )
# 遍历源文件
aux_source_directory ( ${ SUB_DIR} SRC_LIST)
message ( STATUS "SUB_DIR -> " ${ SUB_DIR} )
message ( STATUS "SUB_LSIT" -> ${ SRC_LIST} )
endforeach ( )
add_executable ( zcoder ${ SRC_LIST} )
六、生成动态库
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeSHARED)
# cmake - DCMAKE_BUILD_TYPE= Release . .
# 设置 Release 版本还是 Debug 版本
if ( ${ CMAKE_BUILD_TYPE} MATCHES "Release" ) # 不带 - g
set ( BuildType "Release" )
message ( STATUS "Release版本" )
else ( ) # 带 - g
set ( BuildType "Debug" )
message ( STATUS "Debug版本" )
endif ( )
add_compile_options ( - fPIC) # 动态库必须的选项
aux_source_directory ( . DIR_LIB_SRCS)
# 生成动态库
add_library ( Dir1 SHARED ${ DIR_LIB_SRCS} )
# 设置动态库最后输出的路径
set ( LIBRARY_OUTPUT_PATH ${ PROJECT_SOURCE_DIR} / ${ BuildType} )
七、生成静态库
# cmake - DCMAKE_INSTALL_PREFIX= / . . / build/ . .
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeSTATIC)
AUX_SOURCE_DIRECTORY ( . DIR_LIB_SRCS)
ADD_LIBRARY ( Dir1 STATIC ${ DIR_LIB_SRCS} )
# 将库文件安装到 / lib 目录
INSTALL ( TARGETS Dir1 ARCHIVE DESTINATION lib)
# 将头文件安装到 / include 目录
INSTALL ( FILES dir1. hpp DESTINATION include)
八、调用动态库、静态库
如果同时存在动态库和静态库,优先链接动态库 。 强制链接静态库使用 target_link_libraries(zcoder libDir1.a)
。
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeLink)
set ( SRC_LIST main. cpp)
include_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/lib" )
link_directories ( "${CMAKE_CURRENT_SOURCE_DIR}/lib" )
# 引用动态库
add_executable ( zcoder ${ SRC_LIST} )
target_link_libraries ( zcoder Dir1)
# target _link_libraries ( zcoder libDir1) # 错误
# target _link_libraries ( zcoder libDir1. so) # 正确
# 强制找静态库
# target _link_libraries ( zcoder libDir1. a)
九、find_package()
find_package()
会查找 Findxx.cmake
文件。
cmake_minimum_required ( VERSION 3.16 . 3 )
project ( CMakeFind)
set ( SRC_LIST main. cpp)
add_compile_options ( - std= c++ 11 )
# 是否启用 openssl
set ( ENABLE_OPENSSL true )
# 查找 openssl 是否安装
find_package ( OpenSSL REQUIRED)
if ( OPENSSL_FOUND AND ENABLE_OPENSSL)
message ( STATUS "找到 openssl 库:\"${OPENSSL_INCLUDE_DIR}\"" )
# 添加 openssl 的头文件路径
include_directories ( ${ OPENSSL_INCLUDE_DIR} )
# 添加 ENABLE_OPENSSL 的宏定义
add_definitions ( - DENABLE_OPENSSL)
# 将 OPENSSL_LIBRARIES 的库文件名(包含路径)追加到 LINK_LIB_LIST
list ( APPEND LINK_LIB_LIST ${ OPENSSL_LIBRARIES} )
else ( )
message ( STATUS "没有找到 openssl 库" )
endif ( )
# 编译动态库
# 1. 生成动态库
add_library ( ${ CMAKE_PROJECT_NAME} _shared SHARED ${ SRC_LIST} )
# 2. 链接其他库
target_link_libraries ( ${ CMAKE_PROJECT_NAME} _shared ${ LINK_LIB_LIST} )
# 3. 动态库输出时,不以 xx_shared 输出,而是以 xx 输出
set_target_properties ( ${ CMAKE_PROJECT_NAME} _shared PROPERTIES OUTPUT_NAME ${ CMAKE_PROJECT_NAME} )
# 编译静态库
add_library ( ${ CMAKE_PROJECT_NAME} _static STATIC ${ SRC_LIST} )
target_link_libraries ( ${ CMAKE_PROJECT_NAME} _static ${ LINK_LIB_LIST} )
set_target_properties ( ${ CMAKE_PROJECT_NAME} _static PROPERTIES OUTPUT_NAME ${ CMAKE_PROJECT_NAME} )
add_executable ( zcoder ${ SRC_LIST} )
target_link_libraries ( zcoder ${ LINK_LIB_LIST} )