cmake使用
2020-02-13 19:56:02 # other
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# CMake 最低版本号
cmake_minimum_required(VERSION 2.8)

# 项目信息
project(main)

# 添加版本号
set (main_VERSION_MAJOR 1)
set (main_VERSION_MINOR 0)

set (CMAKE_INCLUDE_CURRENT_DIR ON)

# 是否使用自己的库
option(USE_MYMATH
"USE provided math implementation" ON)

# 检查系统环境 必须放在configure_file命令前
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (pow HAVE_POW)

# 加入一个配置头文件,用于处理CMake对源码的设置
# $前面加"可以使用目录名中间有空格
configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)

# 是否加入MathFunctions库
if(USE_MYMATH)
# 设置头文件
include_directories("${PROJECT_SOURCE_DIR}/math")
# 添加子目录目录
add_subdirectory(math)
# 设置库名
set (EXTAR_LIB ${EXTAR_LIB} MathFunctions)
endif(USE_MYMATH)

# 查找当前目录下的所有源文件
# 将名称保存到DIR_SRCS变量
aux_source_directory(. DIR_SRCS)

# 指定生成目标
add_executable(main_cmake ${DIR_SRCS})

# 添加链接库
target_link_libraries(main_cmake ${EXTAR_LIB})

# 指定安装路径
install (TARGETS main_cmake DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)

# 启用测试
enable_testing()

# 测试程序是否成功运行
add_test (test_run main_cmake 5 2)

# 测试帮助信息是否可以正常提示
add_test (test_usage main_cmake)
set_tests_properties (test_usage PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")

# 测试 5 的平方
add_test (test_5_2 main_cmake 5 2)
set_tests_properties (test_5_2 PROPERTIES PASS_REGULAR_EXPRESSION "is 25")

# 测试 10 的 5 次方
add_test (test_10_5 main_cmake 10 5)
set_tests_properties (test_10_5 PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")

# 测试 2 的 10 次方
add_test (test_2_10 main_cmake 2 10)
set_tests_properties (test_2_10 PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")

# 定义一个宏,用来简化测试工作
macro (do_test arg1 arg2 result)
add_test (test_${arg1}_${arg2} main_cmake ${arg1} ${arg2})
set_tests_properties (test_${arg1}_${arg2} PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)

# 使用该宏进行一系列的数据测试
do_test (5 2 "is 25")
do_test (2 10 "is 1024")
do_test (10 5 "is 100000")

# 支持gdb 让CMake 支持gdb的设置也很容易,只需要指定Debug模式下开启-g选项
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pthread")

# 生成安装包
# 构建一个 CPack 包
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${main_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${main_VERSION_MINOR}")
include (CPack)

# 总结
# 建议1
# 项目中每个模块各自写一个CMakeLists.txt,在项目根目录写一个CMakeLists.txt,用来统一调用各个模块的CMakeLists.txt进行生成。
# 建议2
# 在根目录新建一个build 进行cmake .. 这样生成出来的一些杂项不会到处都有

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 校验CMake版本
cmake_minimum_required(VERSION 2.8)

# 工程名
project(learn)

# 头文件路径
include_directories(
core/include
proto/cpp
/usr/include/mysql/
)

# 库路径
link_directories(
/usr/local/lib
)

# 编译参数
add_definitions(-O2 -g -Wall -std=c++14 -pthread)

# 设置可执行文件输出目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# 递归遍历目录 获得的目录list
macro(SUBDIRLIST result curdir)
file(GLOB children RELATIVE ${curdir} ${curdir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${curdir}/${child})
LIST(APPEND dirlist ${child})
endif()
endforeach()
set(${result} ${dirlist})
endmacro()

# 递归添加子目录
SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
set(ignore build bin)
foreach(subdir ${SUBDIRS})
list(FIND ignore ${subdir} result)
if(${result} EQUAL -1)
add_subdirectory(${subdir})
endif()
endforeach()

# 生成子目录下的可执行文件
SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})

foreach(subdir ${SUBDIRS})
set(DIR_SRCS "")
aux_source_directory(${subdir} DIR_SRCS)
add_executable(${subdir} ${DIR_SRCS})
target_link_libraries(${subdir} pthread)
endforeach()

Boost使用

1
2
3
4
5
6
7
8
9
10
11
12
13
# Boost 设置
SET (BOOST_ROOT ${PROJECT_SOURCE_DIR})
SET (BOOST_INCLUDEDIR "${PROJECT_SOURCE_DIR}/include")
SET (BOOST_LIBRARYDIR "${PROJECT_SOURCE_DIR}/lib")
FIND_PACKAGE(Boost ${BOOST_MIN_VERSION} REQUIRED)
SET (BOOST_MIN_VERSION "1.72.0")

if (NOT Boost_FOUND)
message(FATAL_ERROR "Fatal error: Boost (version >= 1.72) required.")
else()
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
endif (NOT Boost_FOUND)