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 构建.(目前 ...
随机推荐
- word 使用中 上标符号的实现
1. 首先在word 中打下一段话 如: 啦啦啦啦啦啦啦啦 然后加入你需要的上标 如 [2] 2. 选中你需要的上标,然后右击 3. 点击字体选项 出现下图: 4. 在 ...
- Loj 2005 相关分析
Loj 2005 相关分析 大力把式子拆开. \[ \begin{aligned} a &= \frac {\sum_{i=L}^{R} (x_i-\bar{x})(y_i-\bar{y})} ...
- BZOJ3270: 博物馆【概率DP】【高斯消元】
Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n间房间,并且满足可以从任何一 ...
- 事务的隔离级别以及oracle中的锁
事务的概念及特性 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 例如:在关系数据库中,一个事务可以是一条SQL语句,一组SQL语 ...
- Linux下sniffer实现(转)
转发网址:https://blog.csdn.net/eqiang8271/article/details/8489769 //Example 1. #include <stdio.h> ...
- Spring线程池ThreadPoolTaskExecutor配置及详情
Spring线程池ThreadPoolTaskExecutor配置及详情 1. ThreadPoolTaskExecutor配置 <!-- spring thread pool executor ...
- [C++ Primer] 第3章: 字符串, 向量和数组
标准库类型string string初始化 string s2(s1); string s2 = s1; string s3("value"); string s3 = " ...
- Think in java.chm 第14章 多线程
例子1引入线程概念通过得到当前线程方式循环主线程做某事 例子2演示了在主线程之外开启多个线程的基本方式 ( new一个extends Thread ) 例子3 ( task extends Threa ...
- RDLC报表系列一
1.报表项目搭建: 配置好后,单击Web服务URL:http://lg-20151517ryre/ReportServer 如果电脑系统打开的时候没有设置密码的话,此时打开有可能会出现需要登录名和密码 ...
- [C#][Log4Net] 配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...