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条件编译的更多相关文章

  1. CMAKE的用法

    一.      基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make install——尝试执行bootstrap失败 使用:cmake ...

  2. CMAKE的使用

    CMAKE的使用 Version 1.0 2009-3-18 一.      基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make ins ...

  3. cmake的两个命令: option 和 configure_file

    本节要讨论的是cmake的两个命令: option 和 configure_file option 选项,让你可以根据选项值进行条件编译. configure_file 配置文件,让你可以在代码文件中 ...

  4. [转] CMake

    转载地址:https://www.cnblogs.com/lidabo/p/7359422.html cmake 简介 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装( ...

  5. cmake使用方法详解

    cmake 简介 CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性 ...

  6. cmake 使用

    1.cmake 显示编译命令: 在顶层CMakeLists.txt里设置 set(CMAKE_VERBOSE_MAKEFILE ON) 或者  cmake .        再           m ...

  7. C语言中的条件编译

    通常情况,我们想让程序选择性地执行,多会使用分支语句,比如if-else 或者switch-case 等.但有些时候,可能在程序的运行过程中,某个分支根本不会执行. 比如我们要写一个跨平台项目,要求项 ...

  8. CMake之CMakeLists.txt编写入门

    自定义变量 主要有隐式定义和显式定义两种. 隐式定义的一个例子是PROJECT指令,它会隐式的定义< projectname >_BINARY_DIR和< projectname & ...

  9. 用 cmake 构建Qt工程(对比qmake进行学习)

    cmake vs qmake qmake 是为 Qt 量身打造的,使用起来非常方便 cmake 使用上不如qmake简单直接,但复杂换来的是强大的功能 内置的 out-of source 构建.(目前 ...

随机推荐

  1. spring注解事务使用总结

    在使用spring的注解事务的时候,需要考虑到事务的传播行为.遇到什么类型的异常时,事务才起作用.事务方法之间的嵌套调用时,怎么样才生效等等诸多问题.网上搜到很多的主要还是一堆理论文字描述,我这里给出 ...

  2. Libusb学习

    1.参考:http://www.cnblogs.com/Daniel-G/archive/2013/04/22/3036730.html https://baike.so.com/doc/506541 ...

  3. 关于CCRANDOM_0_1

    CCRANDOM_0_1的范围是[0,1)包括0但不包括1 CCRANDOM_0_1() * 1400.0f / 100.0f是0-13 另外每次随机都是相同的数,要随机下种子 srand((unsi ...

  4. Spring集成Mybatis(Dao方式开发)

    Spring整成Mybatis注意事项:  1. 关键jar包不能少 2.可以单独整理好Mybatis框架,测试无误再集成Spring 3.集成时,参数级别的细节可以选择忽略,但思路必须清晰 代码如下 ...

  5. oracle数据库启动时出现ORA-01157和ORA-01110问题

    sql>startup mount; sql>alter database open; RA-01157: 无法标识/锁定数据文件 10 - 请参阅 DBWR 跟踪文件ORA-01110: ...

  6. 建立SSH隧道从外网访问内网服务器

    http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examp ...

  7. ruby的代码风格

    http://stylesror.github.io/ 大部分同意,有小部分,不敢苟同.

  8. cocos命令行生成项目

    cocos命令行生成项目: cocos new GoodDay(项目名称) -p com.boleban.www(包名字) -l cpp(项目类型) -d D:\DevProject\cocos2dx ...

  9. C#自带缓存方案

    /// <summary> /// 获取数据缓存 /// </summary> /// <param name="CacheKey">键< ...

  10. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...