设置一个cmake变量(如果用户未更改) [英] set a cmake variable if it is not changed by the user

查看:105
本文介绍了设置一个cmake变量(如果用户未更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当用户未更改cmake变量时,我才能(重新)设置该变量?

How can i (re)set cmake variable only when it is not changed by the user?

我有以下变量:

set(DIR "testdir" CACHE PATH "main directory")
set(SUBDIR ${DIR}/subdir CACHE PATH "subdirectory")

在第一次运行时,变量被初始化为 testdir testdir / subdir

On the first run the variables are initialized to testdir and testdir/subdir.

当用户更改 DIR 并重新运行cmake而不更改 SUBDIR ,我想生成一个新的 SUBDIR 路径,而我想保留 SUBDIR 路径,当用户更改它时。

When the user changed DIR and reruns cmake without changing SUBDIR, i would like to generate a new SUBDIR path, while i want to keep the SUBDIR path, when the user changed it.

所以 SUBDIR DIR 并<<,则应基于 DIR 的新值将c>设置为新的默认值。 code> SUBDIR 从未更改过。

So SUBDIR should be set to the new default value based on the new value of DIR, if DIR was changed and SUBDIR was never changed before.

推荐答案

将我的评论变成答案

您可以使用 MODIFIED 缓存变量属性,但文档显示

You could use MODIFIED cache variable property, but the documentation says


不要设置或获取。

Do not set or get.

也许更好的方法是使用if语句检查修改:

Maybe a better approach would be to check the modification with if statements:

set(DIR "testdir" CACHE PATH "main directory")
if (NOT DEFINED SUBDIR OR SUBDIR MATCHES "/subdir$")
    set(SUBDIR "${DIR}/subdir" CACHE PATH "subdirectory" FORCE)
endif()

或者您只是不将 DIR 放在 SUBDIR 内,然后将详细信息放入描述:

Or you just don't put DIR inside SUBDIR and put the details into the description:

set(SUBDIR "subdir" CACHE PATH "subdirectory of main directory (see DIR)")

这篇关于设置一个cmake变量(如果用户未更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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