转载自:https://www.jianshu.com/p/f0f71d36411a
cmake新增语法解释:
option (USE_MYMATH “Use provided math implementation” ON)指令
给变量赋值 ON,代表真;OFF关键字则代表 假
configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY])
对指定的输入文件中的内容按照指定的规则进行替换,替换完成后输出到output指定的输出路径
替换规则为:
1、在输入文件中,凡是以${VAR}格式或@VAR@格式出现的地方都将用CMake中对应变量的值进行替换,如上例中VAR变量的值为VAR_NEW,config.h中全部替换成了这个值
2、在输入文件中,类似于#cmakedefine VAR的定义语句将会被替换为#define VAR或者/* #undef VAR /,如上例中USE_MYMATH当设定为ON的时候,config.h变成了#define USE_MYMATH,设定为OFF时,变成了/ #undef USE_MYMATH */;同理,类似于#cmakedefine01 VAR的定义语句将会被替换为#define VAR 1或#define VAR 0。
demo11.cpp
#include <stdio.h>
// demoConfig.h文件是cmake生成的
#include "demoConfig.h"
#ifdef _INTX2_
#define CHOICE "in tx2"
#else
#define CHOICE "not in tx2"
#endif
int main(int argc, char* argv[]){
printf(CHOICE);
printf("%s version: %d.%d \n", argv[0], demo11_VERSION_MAJOR, demo11_VERSION_MINOR);
}
demo11Config.h.in
// the configured options and settings for demo11
#define demo11_VERSION_MAJOR @demo11_VERSION_MAJOR@
#define demo11_VERSION_MINOR @demo11_VERSION_MINOR@
#cmakedefine _INTX2_
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(demo11)
option(IN_TX2 "whether cmake in tx2" OFF)
# The version number
set(demo11_VERSION_MAJOR 1)
set(demo11_VERSION_MINOR 0)
if(IN_TX2)
set(_INTX2_ 1)
endif()
# configure a header file to pass some of the CMake settings to
# source code(demo11Config.h.in), the file demoConfig.h does't exists
configure_file(
"${PROJECT_SOURCE_DIR}/demo11Config.h.in"
"${PROJECT_BINARY_DIR}/demoConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find demoConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the exectuable
add_executable(demo11 demo11.cpp)