cmake--编译类型

前言

cmake可以设设置多种编译选项。这些编译选项可以设置编译优化级别以及是否添加debug信息。

一, 目录结构

├── CMakeLists.txt
├── main.cpp

* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:main.cpp[] - The source file with main

二,cmake脚本

cmake_minimum_required(VERSION 3.5)

# 未设置编译类型的时候,设置cmake使用RelWithDebInfo编译类型,RelWithDebInfo可以简单描述为包含调试信息的release版本。
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message("Setting build type to 'RelWithDebInfo' as none was specified.")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
# 设置多种编译类型的选项
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()

project (build_type)

add_executable(cmake_examples_build_type main.cpp)

三,扩展分析

在cmake脚本中存在CMAKE_BUILD_TYPE变量, 为了不修改cmake脚本而能灵活切换不同编译类型,

可以在执行cmake时设置。

cmake执行包含两种方式,一种是采用cmake-gui,如下:

另外一种命令行方式,如下:

cmake .. -DCMAKE_BUILD_TYPE=Release

一般在构建目录中执行。

猜你喜欢

转载自www.cnblogs.com/svenzhang9527/p/10704066.html