通过命令提示符将参数传递给CMAKE

时间:2012-10-15 13:47:33

标签: makefile cmake

我的项目有一个makefile,而且我正在传递一些参数,并且基于该参数我正在设置一些标志。我能做到这一点。但现在我想用cmake做同样的事情。我创建了cmakelist.txt但我不知道如何传递参数并检查cmakelist.txt中的参数值。

我的makefile示例:

ifeq "$(FAB)" "po"
      //setting some flags
else
      //setting some iother flag
endif

从命令提示符获取参数并根据该参数设置标志的方法是什么?

2 个答案:

答案 0 :(得分:60)

在CMakeLists.txt文件中,创建一个缓存变量,如下所示:

SET(FAB "po" CACHE STRING "Some user-specified option")

来源:http://cmake.org/cmake/help/v2.8.8/cmake.html#command:set

然后,使用GUI(ccmake或cmake-gui)设置缓存变量,或者在cmake命令行上指定变量的值:

cmake -DFAB:STRING=po

来源:http://cmake.org/cmake/help/v2.8.8/cmake.html#opt:-Dvar:typevalue

如果您的选项实际上是布尔值,则将缓存变量修改为布尔值。

答案 1 :(得分:0)

在Ubuntu 16.04上的CMake 3.13

此方法更加灵活,因为它不会将MY_VARIABLE约束为一种类型:

$ cat CMakeLists.txt 
message("MY_VARIABLE=${MY_VARIABLE}")
if( MY_VARIABLE ) 
    message("MY_VARIABLE evaluates to True")
endif()

$ mkdir build && cd build

$ cmake ..
MY_VARIABLE=
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

$ cmake .. -DMY_VARIABLE=True
MY_VARIABLE=True
MY_VARIABLE evaluates to True
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

$ cmake .. -DMY_VARIABLE=False
MY_VARIABLE=False
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

$ cmake .. -DMY_VARIABLE=1
MY_VARIABLE=1
MY_VARIABLE evaluates to True
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build

$ cmake .. -DMY_VARIABLE=0
MY_VARIABLE=0
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build