-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
107 lines (89 loc) · 3.16 KB
/
CMakeLists.txt
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
104
105
106
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# 设置库的名称
set(LIB_NAME ProjectXX)
# 设置可执行文件的名字
PROJECT(${LIB_NAME}_exe)
#设置版本
set(version 1.0)
# 拼接名字
set(LIB_NAME_VERSION ${LIB_NAME}-${version})
#设置安装位置,将头文件和库都安装到这个文章,默认安装到系统的/usr/local/
set(CMAKE_INSTALL_PREFIX /usr/local/)
# 依赖的外部库
#find_package(OpenCV REQUIRED)
# 自己的库依赖的头文件
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
lib
lib/Module1
# 以下为外部依赖
# ${OpenCV_INCLUDE_DIRS}
)
file(GLOB_RECURSE source
lib/*.hpp
lib/*.cpp
lib/Module1/*.cpp
lib/Module1/*.hpp
)
file(GLOB_RECURSE include
lib/*.h
lib/Module1/*.h
)
# ----------------- 静态库 -----------------
add_library(${LIB_NAME} STATIC ${source})
target_link_libraries(${LIB_NAME} PUBLIC
# 以下为静态库的外部依赖
# ${OpenCV_LIBS}
)
# ----------------- 结束静态库的生成 -----------------
# ----------------- 可执行文件 -----------------
# 可执行文件生成
add_executable(${PROJECT_NAME} main.cpp)
# 设置可执行文件的依赖
target_link_libraries(${PROJECT_NAME}
# 静态库
${LIB_NAME}
# 以下为外部依赖
# ${OpenCV_LIBS}
)
# ----------------- 结束可执行文件的生成 -----------------
# ----------------- 生成 CMake 配置文件(不需要修改) -----------------
# 生成 CMake 配置文件
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${LIB_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIB_NAME_VERSION}/
)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${LIB_NAME}ConfigVersion.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIB_NAME_VERSION}/
)
# 安装 CMake 配置文件
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIB_NAME_VERSION}/
)
# ----------------- 结束生成 CMake 配置文件 -----------------
# ----------------- 安装(不需要修改) -----------------
# 安装源码文件
install(FILES ${include}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${LIB_NAME_VERSION}/
)
# 安装静态库、可执行文件
install(TARGETS ${LIB_NAME} ${PROJECT_NAME}
EXPORT ${LIB_NAME_VERSION}-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/${LIB_NAME_VERSION}/
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIB_NAME_VERSION}/
)
# 安装导出文件
install(EXPORT ${LIB_NAME_VERSION}-targets
NAMESPACE "${LIB_NAME}::"
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${LIB_NAME_VERSION}/
)
# ----------------- 结束安装 -----------------