cmake - 如何将没有值的参数传递给 CMake?

标签 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.

最佳答案

-D CMake 命令行参数必须采用 var=value 形式。但是,模拟 bool 值或 #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()

关于cmake - 如何将没有值的参数传递给 CMake?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57114900/

相关文章:

java - 如何使用 cmake 和 UseJava 模块添加 MANIFEST 文件?

linux - CMake:库链接要求

dll - 是否可以在同一个 CMakeLists.txt 中使用/MT 设置项目,而使用/MD 设置其他项目?

cmake - 我如何知道 CMakeLists.txt 中需要哪个最低 CMake 版本?

visual-studio-2012 - CMake 和 Visual Studio - 指定解决方案文件目录

makefile - 如何通过 Cmake 构建系统使用目录中的所有 *.c 文件?

linux - cmake set_target_properties INCLUDE_DIRECTORIES 在 linux 上被忽略

c++ - 使用包含OpenCV的cmake构建MacOS可执行文件进行分发

c++ - 如何从现有的 Makefile 创建 CMake 文件?

c++ - 如何使用 CMake 查找使用 VS 2017 编译的 Boost 文件系统库?