cmake条件编译
CMake的条件编译基于if elseif endif。3.0版本具体语法如下
if(expression)
# then section.
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
elseif(expression2)
# elseif section.
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
else(expression)
# else section.
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endif(expression) ref: https://cmake.org/cmake/help/v3.0/command/if.html?highlight=#command:if
expression有多种表达方式。布尔比较,字符串比较,数值比较,复合表达式等。罗列一下:
if(<constant>)
True if the constant is , ON, YES, TRUE, Y, or a non-zero number. False if the constant is , OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND. Named boolean constants are case-insensitive. If the argument is not one of these constants, it is treated as a variable.
if(<variable>)
True if the variable is defined to a value that is not a false constant. False otherwise. (Note macro arguments are not variables.)
if(NOT <expression>)
True if the expression is not true.
if(<expr1> AND <expr2>)
True if both expressions would be considered true individually.
if(<expr1> OR <expr2>)
True if either expression would be considered true individually.
if(COMMAND command-name)
True if the given name is a command, macro or function that can be invoked.
if(POLICY policy-id)
True if the given name is an existing policy (of the form CMP<NNNN>).
if(TARGET target-name)
True if the given name is an existing logical target name such as those created by the add_executable(), add_library(), or add_custom_target() commands.
if(EXISTS path-to-file-or-directory)
True if the named file or directory exists. Behavior is well-defined only for full paths.
if(file1 IS_NEWER_THAN file2)
True if file1 is newer than file2 or if one of the two files doesn’t exist. Behavior is well-defined only for full paths. If the file time stamps are exactly the same, an IS_NEWER_THAN comparison returns true, so that any dependent build operations will occur in the event of a tie. This includes the case of passing the same file name for both file1 and file2.
if(IS_DIRECTORY path-to-directory)
True if the given name is a directory. Behavior is well-defined only for full paths.
if(IS_SYMLINK file-name)
True if the given name is a symbolic link. Behavior is well-defined only for full paths.
if(IS_ABSOLUTE path)
True if the given path is an absolute path.
if(<variable|string> MATCHES regex)
True if the given string or variable’s value matches the given regular expression.
if(<variable|string> LESS <variable|string>)
True if the given string or variable’s value is a valid number and less than that on the right.
if(<variable|string> GREATER <variable|string>)
True if the given string or variable’s value is a valid number and greater than that on the right.
if(<variable|string> EQUAL <variable|string>)
True if the given string or variable’s value is a valid number and equal to that on the right.
if(<variable|string> STRLESS <variable|string>)
True if the given string or variable’s value is lexicographically less than the string or variable on the right.
if(<variable|string> STRGREATER <variable|string>)
True if the given string or variable’s value is lexicographically greater than the string or variable on the right.
if(<variable|string> STREQUAL <variable|string>)
True if the given string or variable’s value is lexicographically equal to the string or variable on the right.
if(<variable|string> VERSION_LESS <variable|string>)
Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(<variable|string> VERSION_EQUAL <variable|string>)
Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(<variable|string> VERSION_GREATER <variable|string>)
Component-wise integer version number comparison (version format is major[.minor[.patch[.tweak]]]).
if(DEFINED <variable>)
True if the given variable is defined. It does not matter if the variable is true or false just if it has been set. (Note macro arguments are not variables.)
if((expression) AND (expression OR (expression)))
The expressions inside the parenthesis are evaluated first and then the remaining expression is evaluated as in the previous examples. Where there are nested parenthesis the innermost are evaluated as part of evaluating the expression that contains them.
对于if语法,比较常用的就是字符串比较和有没有定义这个变量的比较。
第一种,对于变量是否定义,可以做如下写法:
if(DEFINED var)
else()
endif() 或者
if(var)
else()
endif()
两种都可以验证这个变量有没有定义过,注意:仅仅代表定义过,比如你在CMake命令行中随便写了-Dvar=xxx,就表示定义过了,对里面的值没有做任何限制。
第二种,常用的用法就是字符串比较
if(${var} STREQUAL "ON")
elseif(${var} STREQUAL "OFF")
endif()
但是需要注意的是在这个时候,你的脚本已经假设你对于var已经有了 已被定义的默认要求 !如果没有定义,脚本会报错退出。那有没有解决方法给个默认值呢?有!
option(address "This is a default option for var" ON)
这样就对var设置了默认值,即使命令行没有定义var,脚本里面也有默认值ON。用户若想更改,就在命令行显示定义:
cmake -Dvar=OFF .
但是在脚本中,这个var是在option之后才会被认为定义,在此之前依然是未定义的!
最后贴一段稍微完整点的代码
option(PUBLIC "This is a default option for PUBLIC" OFF)
message(STATUS "build public platform switch: "${PUBLIC})
if(${PUBLIC} STREQUAL "ON")
message(STATUS "start to build public platform.")
elseif(${PUBLIC} STREQUAL "OFF")
add_definitions(-DPUBLIC_CLOUD_PLATFORM)
message(STATUS "start to build private platform.")
endif(${PUBLIC} STREQUAL "ON")
cmake条件编译的更多相关文章
- CMAKE的用法
一. 基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make install——尝试执行bootstrap失败 使用:cmake ...
- CMAKE的使用
CMAKE的使用 Version 1.0 2009-3-18 一. 基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make ins ...
- cmake的两个命令: option 和 configure_file
本节要讨论的是cmake的两个命令: option 和 configure_file option 选项,让你可以根据选项值进行条件编译. configure_file 配置文件,让你可以在代码文件中 ...
- [转] CMake
转载地址:https://www.cnblogs.com/lidabo/p/7359422.html cmake 简介 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装( ...
- cmake使用方法详解
cmake 简介 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性 ...
- cmake 使用
1.cmake 显示编译命令: 在顶层CMakeLists.txt里设置 set(CMAKE_VERBOSE_MAKEFILE ON) 或者 cmake . 再 m ...
- C语言中的条件编译
通常情况,我们想让程序选择性地执行,多会使用分支语句,比如if-else 或者switch-case 等.但有些时候,可能在程序的运行过程中,某个分支根本不会执行. 比如我们要写一个跨平台项目,要求项 ...
- CMake之CMakeLists.txt编写入门
自定义变量 主要有隐式定义和显式定义两种. 隐式定义的一个例子是PROJECT指令,它会隐式的定义< projectname >_BINARY_DIR和< projectname & ...
- 用 cmake 构建Qt工程(对比qmake进行学习)
cmake vs qmake qmake 是为 Qt 量身打造的,使用起来非常方便 cmake 使用上不如qmake简单直接,但复杂换来的是强大的功能 内置的 out-of source 构建.(目前 ...
随机推荐
- ubuntu16.04 源码安装Python3.7 (可以在此基础上安装Tensorflow) (确保Tensorflow计算框架与系统的彻底隔离)
Python3.7 源码下载: https://www.python.org/downloads/release/python-370/ 解压源码: tar -zxvf Python-3.7.0.tg ...
- JFrog Artifactory CE c&&c++ 包管理工具
JFrog Artifactory CE 支持conan 以及普通二进制c&&c++包管理 使用docker 进行环境的搭建测试 安装 docker run -d -p 8081:80 ...
- cocos2dx 3.0 +VS2013 环境搭建
1.需要javasdk,android sdk,ndk,python 2.各种环境变量配置如下: JAVA_HOME:C:\Program Files\Java\jdk1.7.0_67 Path:%J ...
- nyoj A+B Problem IV
A+B Problem IV 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 acmj最近发现在使用计算器计算高精度的大数加法时很不方便,于是他想着能不能写个程序把这 ...
- 阿里云 搭建Git服务器
1. 服务端安装git, ssh, 客户端安装git sudo apt-get install git sudo apt-get install ssh 2. 服务单创建用户server sudo a ...
- 【转】java中创建对象的方法
有4种显式地创建对象的方式: 1.用); System.out.println("call default constructor"); } public Customer(Str ...
- Hibernate中get()和load()的区别
Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别. 1. get() 使用get()来根据ID进行单条查询: User user=se ...
- Unity容器声明周期管理
Having said that, here is a solution that you can use with the Unity container: Create some custom a ...
- WCF传输大数据 --断点续传(upload、download)
using System; using System.IO; using System.Runtime.Serialization; using System.ServiceModel; namesp ...
- MS-TEST 批处理执行测试时的资源文件目录问题
What: 使用MS-TEST的 批处理执行它的测试 DLL 时,如: MSTest /testcontainer:C:\David\ADAccountGIT\AdAccountMSTest\ADA ...