1. CMake传参并根据参数执行选择分支逻辑
1.2 CMake传参的必要性
假设有如下场景,需要在某种情况下,确定要生成静态库或者动态库其中某个,当然通过修改CMakeLists.txt的方式可以完成,但最简单的方式还是通过用户传参的方式决定编译静态库还是动态库,有如下CMakeLists.txt
#主CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(example)
# 设置USE_SHARED变量并添加描述,之后设置默认值,默认值为ON
option(USE_SHARED "BUILD SHARED" ON)
add_subdirectory(./xprint)
add_executable(example main.cpp)
target_include_directories(example PUBLIC ./xprint)
target_link_libraries(example xprint)
由于库文件和目标文件分别在不同的目录,使用主CMakeLists加子CMakeLists.txt的方式构建
# 子CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
通过访问USE_SHARED变量,来确定要不要生成动态库文件
if(USE_SHARED)
message(STATUS "需要编译动态库")
add_library(xprint-shared SHARED xprint.cpp xprint.h)
set_target_properties(xprint-shared PROPERTIES OUTPUT_NAME "xprint")
else()
message(STATUS "不需要编译动态库")
endif()
其余目录结构和目录相同,只修改了CMakeLists.txt,执行如下指令
cmake -D USE_SHARED=OFF ..
#对应如下输出
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- 不需要编译动态库 #构建结构发生变化,默认为ON,传参为OFF
-- Configuring done
-- Generating done
-- Build files have been written to: /home/detc/cMake/example/build
可以看到在传参时改变了USE_SHARED变量的值之后对应的子CMakeLists.txt中的构建结构也发生了变化,上述传参传入开关量的值,还可以传入字符串类型的值,通过比较字符串执行分支逻辑
1.3 传参在交叉编译时的应用
项目有时需要在
x86
下构建,有时又需要再aarch64
下构建,就需要来回切换编译器,因此使用传参方式更为简便
cmake_minimum_required(VERSION 3.15)
project(example)
option(USE_SHARED "BUILD SHARED" ON)
SET(BUILD_PLATFORM "x86" CACHE STRING "select build cpu type")
if(BUILD_PLATFORM STREQUAL arm)
message(STATUS "当前目标机器为嵌入式设备")
set(CMAKE_CXX_COMPILER xxxx) # xxx代表交叉编译工具链位置
set(CMAKE_C_COMPILER xxxx)
else()
message(STATUS "当前目标机器为非嵌入式设备")
set(CMAKE_CXX_COMPILER /usr/bin/g++) # xxx代表主机的gcc编译器
set(CMAKE_C_COMPILER /usr/bin/gcc)
endif()
add_subdirectory(./xprint)
add_executable(example main.cpp)
target_include_directories(example PUBLIC ./xprint)
target_link_libraries(example xprint)
2. 归纳总结
2.1 set的三种基本用法:
- 设置普通变量set(var valueList),其中valueList可以是空,或者列表,列表中间使用空格分开
- 设置缓存条目 set(var varValue CACHE type helpInfo),其中type最为重要,表示当前变量的类型,CMake支持的类型包含BOOL<ON,OFF>,FILEPATH
,STRING/STRINGS:文本或下拉框,INTERNAL:文本,只适用于内部- 设置环境变量 set(ENV{} []) 将环境变量设置为value
2.2 选择分支的基本用法
if(condition)
else()
endif()
# if中的condition一般必填
# else()和endif()括号中的condition一般是用来限定范围,可以填可以不填
2.3 关于option
option一般用作编译开关,基本用法如下
option(<variable> "<help_text>" [value])
与set不同,变量的值只能取ON或OFF,中间代表该变量的含义信息