1、cmake给CMakeLists.txt传递参数

命令行cmake -DTARGET_CPU:STRING=x86
然后,我们就可以在CMakeLists.txt中使用他帮助我们实现一些逻辑:
if(TARGET_CPU STREQUAL "x86")
#do something
else()
#do something
endif()

2、shell给CMakeLists.txt传递参数

a.sh

export TYPES_RELEASE_A20="A20"

export PROJECT_BUILD_TYPE=$1

CMakeLists.txt
if($ENV{PROJECT_BUILD_TYPE} STREQUAL $ENV{TYPES_DEBUG_X86} OR $ENV{PROJECT_BUILD_TYPE} STREQUAL $ENV{TYPES_DEBUG_NDK28})

add_definitions(-DAAA_HOST_BBB)

endif()

3、add_definitions的用法

add_definitions的功能和C/C++中的#define是一样的

比如我有如下两个文件,一个源文件main.cpp,一个CMakeLists.txt
源文件main.cpp

#include <iostream>
int main()
{
#ifdef TEST_IT_CMAKE
    std::cout<<"in ifdef"<<std::endl;
#endif
    std::cout<<"not in ifdef"<<std::endl;
}

cmake文件CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(optiontest)

add_executable(optiontest main.cpp)
option(TEST_IT_CMAKE "test" ON)
message(${TEST_IT_CMAKE})
if(TEST_IT_CMAKE)
    message("itis" ${TEST_IT_CMAKE})
    add_definitions(-DTEST_IT_CMAKE)
endif()

通过option设置一个变量,并通过add_definitions将其转换为#define TEST_IT_CMAKE

当变量为ON时,该程序的输出是

in ifdef
not in ifdef
当变量为OFF时,该程序的输出是

not in ifdef

06-23 00:05