首页 > 代码库 > cmake常用工程示例大集合
cmake常用工程示例大集合
1 简单的可执行文件生成工程
1.1 源文件
main.cpp
#include <stdio.h>int main(int argc, char *argv[]){ printf("Hello CMake!/n"); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
1.2 CMakeLists.txt文件
CMakeLists.txt
# 版本限定CMAKE_MINIMUM_REQUIRED(VERSION 2.8) # 项目名称PROJECT(Test1)# 添加源文件列表变量SET(SRC_LIST main.cpp)# 打印编译目录和项目目录路径MESSAGE(STATUS "This is BINARY dir " ${PROJECT_BINARY_DIR})MESSAGE(STATUS "This is SOURCE dir " ${PROJECT_SOURCE_DIR})# 生成可执行文件ADD_EXECUTABLE(HelloCMake ${SRC_LIST})
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2 只单独生成静态库和动态库
2.1 工程目录
.├── build├── CMakeLists.txt├── include│ └── mymath.h└── src └── mymath.c
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.2 工程文件
mymath.h
#ifndef _MYMATH_H_#define _MYMATH_h_int add(int a, int b);int sub(int a, int b);#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
mymath.c
#include "mymath.h"int add(int a, int b){ return a + b;}int sub(int a, int b){ return a - b;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.3 CMakeLists.txt
# 版本限定 CMAKE_MINIMUM_REQUIRED(VERSION 2.8) # 项目名称 PROJECT(Test2) # 设置编译器选项:O3优化,显示所有警告SET(CMAKE_C_FLAGS "$ENV{CFLAGS} -O3 -Wall") # 定义头文件的路径变量SET(INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include) MESSAGE(STATUS "Include Path, ${INCLUDE_PATH}") # 定义源文件路径变量SET(SOURCE_PATH ${PROJECT_SOURCE_DIR}/src) MESSAGE(STATUS "Source Path , ${SOURCE_PATH}") # 包含头文件路径INCLUDE_DIRECTORIES(${INCLUDE_PATH}) # 添加源文件路径下所有源文件存放到变量中(*.c && *.cpp),当然也可以手动一个个文件添加进来AUX_SOURCE_DIRECTORY(${SOURCE_PATH} SRC_LIST) # 设置动态库输出路径SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) MESSAGE(STATUS "Library Output Path, " ${PROJECT_BINARY_DIR/lib}) # 生成动态库(libmymath.so)ADD_LIBRARY(mymath SHARED ${SRC_LIST}) # 生成静态库(libmymath.a,target名字只能有一个,所以不能与动态库的名字一样)ADD_LIBRARY(mymath_static STATIC ${SRC_LIST})# 但是可以通过下面的命令更改静态库target生成的库名,这样就和动态库的名字一样的了SET_TARGET_PROPERTIES(mymath_static PROPERTIES OUTPUT_NAME "mymath")
- 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
- 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
3 外链接动态库和静态库生成可执行文件
3.1 工程目录
.├── build├── CMakeLists.txt├── include│ └── mymath.h├── libs│ ├── libmymath.a│ └── libmymath.so└── src └── main.c
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.2 工程文件
mymath.h
#ifndef _MYMATH_H_#define _MYMATH_h_int add(int a, int b);int sub(int a, int b);#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
main.c(注意:因为编译的动/静态库是采用的C语言,所以只能写为main.c,而不是main.cpp,否则在链接的时候出问题,当然比较好的做法就是在生成库的时候加入extern "C"
关键字,增加兼容性)
#include <stdio.h>#include "mymath.h"int main(){ printf("Hello CMake!\n"); printf("1 + 5 = %d\n", add(1, 5)); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.3 CMakeLists.txt
# 版本限定 CMAKE_MINIMUM_REQUIRED(VERSION 2.8) # 项目名称 PROJECT(Test3)# 设置编译器选项:O3优化,显示所有警告SET(CMAKE_C_FLAGS "$ENV{CFLAGS} -O3 -Wall") # 定义头文件的路径变量SET(INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include) MESSAGE(STATUS "Include Path, ${INCLUDE_PATH}")# 定义库文件路径SET(LIB_PATH ${PROJECT_SOURCE_DIR}/libs)MESSAGE(STATUS "Libs Path, ${LIB_PATH}")# 定义源文件路径变量SET(SOURCE_PATH ${PROJECT_SOURCE_DIR}/src) MESSAGE(STATUS "Source Path , ${SOURCE_PATH}") # 包含头文件搜索路径INCLUDE_DIRECTORIES(${INCLUDE_PATH}) # 包含库搜索路径LINK_DIRECTORIES(${LIB_PATH})# 定义待链接库名字# 链接静态库SET(LIBS_LIST libmymath.a) # 链接动态库#SET(LIBS_LIST libmymath.so)# 添加源文件路径下所有源文件存放到变量中(*.c && *.cpp)AUX_SOURCE_DIRECTORY(${SOURCE_PATH} SRC_LIST)SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)# 设置生成可执行文件的名称SET(EXECUTABLE_FILE_NAME mymath.exe)# 生成可执行文件ADD_EXECUTABLE(${EXECUTABLE_FILE_NAME} ${SRC_LIST})# 设置可执行文件链接的库名称TARGET_LINK_LIBRARIES(${EXECUTABLE_FILE_NAME} ${LIBS_LIST})
- 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
- 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
4 CMake常用系统变量
# 编译目录路径,下面三个变量均是同样的值CMAKE_BINARY_DIRPROJECT_BINARY_DIR<projectname>_BINARY_DIR# 工程顶层路径,三个均是同样的值CMAKE_SOURCE_DIRPROJECT_SOURCE_DIR<projectname>_SOURCE_DIR# 当前CMakeLists.txt的路径,如果子目录下有CMakeLists.txt,那么该值与CMAKE_SOURCE_DIR不一样CMAKE_CURRENT_SOURCE_DIR# 当前CMakeLists.txt的目标文件输出路径,意义同上CMAKE_CURRRENT_BINARY_DIRADD_SUBDIRECTORY(src "更改为子目录新的目标文件输出路径")# 设置目标文件的存放路径EXECUTABLE_OUTPUT_PATH# 设置库文件的存放路径LIBRARY_OUTPUT_PATH# 设置工程名PROJECT_NAME# 主版本号,比如2.4.6中的2,通过SET命令来设置CMAKE_MAJOR_VERSION,CMAKE# 次版本号,比如2.4.6中的4CMAKE_MINOR_VERSION,CMAKE# 补丁等级,比如2.4.6 中的6CMAKE_PATCH_VERSION,CMAKE# 系统名称,比如Linux-2.6.22CMAKE_SYSTEM# 不包含版本的系统名,比如LinuxCMAKE_SYSTEM_NAME# 系统版本,比如2.6.22CMAKE_SYSTEM_VERSION# 处理器名称,比如i686CMAKE_SYSTEM_PROCESSOR# 在所有的类UNIX平台为TRUE,包括OS X和cygwinUNIX# 在所有的win32平台为TRUE,包括cygwinWIN32# 动/静态库生成全局控制BUILD_SHARED_LIBS# C编译器选项(ADD_DEFINITIONS也可以添加选项)CMAKE_C_FLAGS/CMAKE_C_FLAGS_DEBUG/CMAKE_C_FLAGS_RELEASESET(CMAKE_C_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")# C++编译器选项CMAKE_CXX_FLAGS/CMAKE_CXX_FLAGS_DEBUG/CMAKE_CXX_FLAGS_RELEASESET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -lpthread -lm")
- 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
- 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
5 CMake常用的指令说明
add_definitions(-DXXX1 -DXXX2 ...)
remove_definitions(-DXXX1 -DXXX2 ...)
# 添加/移出宏定义
- 1
- 1
aux_source_directory(dir variable)
# 作用是发现一个目录下所有的源代码文件并将列表存储在一个变量中,这个指令临时被用来自动构建源文件列表。如将当前文件夹中所有源文件放入一个变量中:AUX_SOURCE_DIRECTORY(. DIR_SRCS)
- 1
- 2
- 3
- 1
- 2
- 3
include_directories([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...)
# 添加编译所需的头文件INCLUDE_DIRECTORIES(${XXX_INCLUDE_PATH} ${Other_INCLUDE_PATH} ${PROJECT_INCLUDE_PATH})
- 1
- 2
- 1
- 2
link_directories(directory1 directory2 ...)
# 添加库搜索文件夹LINK_DIRECTORIES(${XX1_LIB_PATH} ${XX2_LIB_PATH})
- 1
- 2
- 1
- 2
target_link_libraries(<target> [item1 [item2 [...]]] [[debug|optimized|general] <item>] ...)
# 指明可执行文件或者库文件需要连接的链接库(静态or动态)add_library(A STATIC a.c)add_library(B STATIC b.c)target_link_libraries(A B)target_link_libraries(B A)add_executable(main main.c)target_link_libraries(main A)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
add_library(<name> <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED [GLOBAL])
# 参数 name 为欲生成的库名称,默认不加修饰为静态库,但是最后会受BUILD_SHARED_LIBS全局变量的影响,此变量默认为OFF,生成库文件是,不需要加前缀libadd_library(calc calc.c)add_library(mylib ${mylib_sources})
- 1
- 2
- 3
- 1
- 2
- 3
get_filename_component(<VAR> <FileName> <COMP> [CACHE])
# 获取一个完整名的特定部分# 获取PROJECT_SOURCE_DIR路径中去除项目名称的路径部分# 保存到变量WORKSPACE_DIR中(当cmake版本低于2.8.11时,component的值取PATH与大于2.8.11版本中的DIRECTORY的效果是一样的)# <= 2.8.11get_filename_component(WORKSPACE_DIR ${PROJECT_SOURCE_DIR} PATH)# > 2.8.11get_filename_component(WORKSPACE_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)# 各component说明如下 DIRECTORY = Directory without file name NAME = File name without directory EXT = File name longest extension (.b.c from d/a.b.c) NAME_WE = File name without directory or longest extension ABSOLUTE = Full path to file REALPATH = Full path to existing file with symlinks resolved PATH = Legacy alias for DIRECTORY (use for CMake <= 2.8.11)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
参考资料和扩展阅读
CMake和Vistual Studio
DLL动态库导出
https://cmake.org/cmake/help/v2.8.12/cmake.html
http://blog.csdn.net/freeape/article/details/52567087
cmake常用工程示例大集合