cmake:设置参数的默认值 [英] cmake: setting default values for arguments

查看:797
本文介绍了cmake:设置参数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UNIX Makefile如下:

My UNIX Makefile is as follows:

   param=0

   run:
       ./foo -r "fun($(param))"

如果我这样做 make run ,我得到。/foo-r fun(0)
make run param = 10 ,我得到。/foo -r fun(10)

So if I do make run, I get ./foo - r "fun(0)" and for make run param=10, I get ./foo -r "fun(10)".

现在我想使用cmake生成类似的Makefile。

Now I want to generate similar Makefile using cmake.

   add_custom_target(
    run 
    ./foo -r "\"fun($(param))\""
   )

如何在cmake配置文件中为 param 设置默认值?

How do I set the default value for param within cmake configuration file?

推荐答案

CMake中的概念有些不同。您可以定义带有默认值和文档字符串等的缓存变量(基本上是在同一构建目录中用于后续构建的变量,可由用户自定义)。然后,可以通过传递 -D name:type = value 选项进行更改或使用更友好的前端之一(例如 ccmake ,即CMake的curses用户界面。)

The concept in CMake is a bit different. You can define "cache variables" (basically variables that are remembered for subsequent builds in the same build dir, and can be customized by users) that come with default values and documentation strings and such. These can then be changed either by passing -D name:type=value options to cmake, or using one of the friendlier frontends (e.g. ccmake, the curses UI for CMake).

基于您的问题的示例:


SET(param 0 CACHE STRING "Test variable defaulting to '0'")
# ...
add_custom_target(run ./foo -r "\"fun(${param})\"")

您将在CMake详尽的文档中找到更多详细信息。

You'll find more details in the exhaustive docs for CMake.

PS。这是针对CMake内部的变量,特别是 CMakeLists.txt 本身的变量;据我所知,更改值的可能性不会保留到生成的Makefile中。我不确定一开始是否有可能,因为它可能与CMake支持的所有目标(例如Visual Studio项目以及其他不兼容)不兼容。无论如何,CMake似乎都不是为生成独立于CMake使用的构建文件而设计的。

PS. this is for variables inside CMake and specifically CMakeLists.txt itself; the possibility to change the value is not carried over into the generated Makefile as far as I can tell. I'm not sure that's possible in the first place because it probably wouldn't be compatiable with all of the targets supported by CMake (e.g. Visual Studio projects and what not). In any case, CMake doesn't seem to have been designed for generating build files used independently of CMake.

这篇关于cmake:设置参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆