首页 > 解决方案 > 如何将没有价值的参数传递给 CMake?

问题描述

在CMake中,我可以传递一个没有值的变量作为参数,并检查它是否提供?

cmake -DPAR1=123 -DPAR2

# CMakeLists.txt
if (PAR2)
    message("PAR2 detected")
else()
    message("PAR2 not detected")
endif()

使用此代码,我得到错误:

Parse error in command line argument: -DPAR2
Should be: VAR:type=value
CMake Error: No cmake script provided.
CMake Error: Problem processing arguments. Aborting.

标签: cmake

解决方案


-DCMake 命令行参数必须采用var=value. 但是,模拟布尔值的一种方法#ifdef是为您的案例传递一个有效值(可以是您想要的任何值)true

cmake -DPAR1=123 -DPAR2=True

并在这种情况下完全省略变量false

cmake -DPAR1=123

最后,在您的CMakeLists.txt文件中,更改您的 if 语句以用于DEFINED检查PAR2变量是否存在:

if (DEFINED PAR2)
    message("PAR2 detected")
else()
    message("PAR2 not detected")
endif()

推荐阅读