文档地址
Mastering CMake — Mastering CMake
CMake Reference Documentation — CMake 3.15.7 Documentation
最小工程文件
PROJECT(app)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(app ${DIR_SRCS})
添加版本号配置头文件
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the executable
add_executable(Tutorial tutorial.cxx)
创建:TutorialConfig.h.in
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
cmake 宏信息会被替换
#include <TutorialConfig.h>
就可以使用了
添加共享库
add_library(MathFunctions mysqrt.cxx) //添加共享库
根节点使用:
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions") //包含头目录
add_subdirectory (MathFunctions) //添加子目录 必须有cmakelist.txt
link_directories(/usr/lib)
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions) //链接库依赖
添加编译选项
在顶层CMakeList.txt添加
option (USE_MYMATH "Use tutorial provided math implementation" ON)
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial ${EXTRA_LIBS})
//.cpp中使用
在.TutorialConfig.h.in中添加
#cmakedefine USE_MYMATH
安装和测试
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
//应用程序
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include)
CMAKE_INSTALL_PREFIX 指定安装路径
使用:
cmake -DCMAKE_INSTALL_PREFIX=<你想要安装的路径>
或者
SET(CMAKE_INSTALL_PREFIX <install_path>)
//测试
include(CTest)
# does the application run
add_test (TutorialRuns Tutorial 25)
# does it sqrt of 25
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")
# does it handle negative numbers
add_test (TutorialNegative Tutorial -25)
set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")
# does it handle small numbers
add_test (TutorialSmall Tutorial 0.0001)
set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")
# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")
命令解析
aux_source_directory(<dir> <variable>) 添加源文件到变量
例如: aux_source_directory(. SOURCE)
当前目录所有.cpp .c文件给变量 SOURCE
ADD_SUBDIRECTORY( src ) 指明本项目包含一个子目录 src
TARGET_LINK_LIBRARIES( main Test ) 指明可执行文件 main 需要连接一个名为Test的链接库
子目录共享库
src 创建CMakeList.txt
AUX_SOURCE_DIRECTORY(. DIR_TEST1_SRCS)
ADD_LIBRARY ( Test ${DIR_TEST1_SRCS}) //编译为 目录下源文件为 Test共享库
编译
创建build目录
执行 cmake path
path是CMakeList.txt路径
make -j4
cmake编译模式
可以通过两种方式指定生成的Makefile的编译模式,一种是在cmake命令后显示指定编译模式,一种可以把编译的模式配置写在CMakeLists.txt中。
方式一:显示指定
mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make
或者
mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
方式二:在CMakeLists.txt中设置
SET(CMAKE_BUILD_TYPE "Debug”)
or
SET(CMAKE_BUILD_TYPE "Release")
9万+

被折叠的 条评论
为什么被折叠?



