CMakeLists的一个编写样例

0

本文涉及:

  • CMakeLists

使用CMAKE的最低版本要求

cmake_minimum_required(VERSION 3.10)

设置工程名称

project(hello VERSION 1.0)
set(PROJECT_NAME out)

添加编译选项

add_definitions("-g -Wall") -std=c++20 -o1
设置编译目标类型是release版还是debug版本
set(CMAKE_BUILD_TYPE "Release")
设置C++版本
set(CMAKE_CXX_STANDARD 20)
编译参数指定,如生成位置无关的动态库
set(CMAKE_CXX_FLAGS "-fPIC")
为当前目录及以下目录中的可执行文件、共享库或模块库目标的链接步骤添加选项,如链接phread库
add_link_options(-pthread)

设置包含的目录

1
2
3
4
include_directories(
"${PROJECT_SOURCE_DIR}/include"
"C:/Program Files/MySQL/MySQL Server 8.0/include"
)

添加cmake执行编译的子目录
add_subdirectory(${PROJECT_SOURCE_DIR}/src/base)

选取编译所用到的源文件

将main.cpp等存到变量SRC_FILE内

1
2
3
4
5
set(SRC_FILE 
${PROJECT_SOURCE_DIR}/src/main.cpp
${PROJECT_SOURCE_DIR}/src/_mysql.cpp
${PROJECT_SOURCE_DIR}/src/LogFile.cpp
)

自动扫描当前路径下的所有源文件存到变量SRC_FILE内
aux_source_directory(${PROJECT_SOURCE_DIR}/src/ SRC_FILE)
自定义搜索规则
file(GLOB SRC_LIST "*.cpp" "protocol/*.cpp")
file(GLOB SRC_LIST ${PROJECT_SOURCE_DIR}/src/*.cpp)
设置输出路径—————————————–
set(HOME /home/robin/Linux/calc)
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin/)
设置动态库生成路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
设置动态库/静态库生成路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

设置编译类型

add_executable(demo demo.cpp) 生成可执行文件
add_executable(${PROJECT_NAME} ${SRC_LIST})
add_executable(${CMAKE_PROJECT_NAME} ${SRC_LIST})
add_library(common STATIC util.cpp) 生成静态库
add_library(common SHARED util.cpp) 生成动态库或共享库

指定链接动态库或静态库

链接api.so,注意路径要写全
target_link_libraries(${PROJECT_NAME} libface.a) 链接libface.a
target_link_libraries(demo libface.so) 链接libface.so
全路径
target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/libs/libface.a)
target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/libs/libface.so)
多个库

1
2
3
4
5
target_link_libraries(${PROJECT_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/libs/libface.a
boost_system.a
boost_thread
pthread)

只用来链接静态库
link_directories(<lib path>)
查找指定的库文件
find_library(VAR name path)
设置链接库搜索目录
在 CMake 中可以在生成可执行程序之前,通过命令指定出要链接的动态库的位置,指定静态库位置使用的也是这个命令:

1
2
3
4
5
6
7
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/libs
)
target_link_libraries(${PROJECT_NAME} libmysql.dll)
target_link_libraries(${PROJECT_NAME} libmysql.lib)
target_link_libraries(${PROJECT_NAME} "D:/glog/bin/glog.dll")
target_link_libraries(${PROJECT_NAME} "D:/glog/lib/glog.lib")

日志

message([STATUS|WARNING|AUTHOR_WARNING|FATAL_ERROR|SEND_ERROR] "message to display" ...)
(无) :重要消息
STATUS :非重要消息
WARNING:CMake 警告, 会继续执行
AUTHOR_WARNING:CMake 警告 (dev), 会继续执行
SEND_ERROR:CMake 错误, 继续执行,但是会跳过生成的步骤
FATAL_ERROR:CMake 错误, 终止所有处理过程
输出一般日志信息
message(STATUS "source path: ${PROJECT_SOURCE_DIR}")

CMakeLists样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cmake_minimum_required(VERSION 3.10)
project(hello VERSION 1.0)
set(PROJECT_NAME out)
add_definitions("-g -Wall")
set(CMAKE_CXX_STANDARD 20)
include_directories(
"${PROJECT_SOURCE_DIR}/include"
"C:/Program Files/MySQL/MySQL Server 8.0/include"
)
file(GLOB SRC_LIST ${PROJECT_SOURCE_DIR}/src/*.cpp)
link_directories("C:/Program Files/MySQL/MySQL Server 8.0/lib/")
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} libmysql.dll)
message(STATUS "source path: ${PROJECT_SOURCE_DIR}")
-------------本文结束感谢您的阅读-------------