删除变量的特定部分 [英] Remove specific part of variable
问题描述
我想从CMake变量中删除特定的库.
I want to remove specific libraries from a CMake variable.
假设 LIB
包含变量"A; B; C"的值,我知道使用 set
来添加另一个像这样的变量"D"的内容
Suppose LIB
contains the value of the variables "A;B;C",
I know use set
to add the comtent of another variable "D" like this
set(LIB ${LIB};D)
但是我试图从 LIB
中删除"C",如下所示:
However I tried to remove "C" from LIB
like following
unset(LIB C)
此代码不起作用.有人知道这样做的好方法吗?
This code does not work. Does anyone know good way to do this?
推荐答案
这不是 unset
的工作方式.在您的示例中, unset(LIB C)
取消设置变量 LIB
和 C
.它不会从 LIB
中删除 C
部分.
That's not how unset
works. In your example, unset(LIB C)
unsets both variables LIB
and C
. It does not remove the C
part from LIB
.
由于所有变量在CMake中都是内部字符串,因此应使用 string(REPLACE)
.就您而言
As all variables are internally strings in CMake, you should use string(REPLACE)
. In your case
string(REPLACE C "" LIBwithoutC LIB)
将所有出现的 C
替换为空字符串,并将结果存储在 LIBwithoutC
中.您可能需要对其进行微调,以删除多余的分号.
Replaces all occurrences of C
by an empty string and stores the result in LIBwithoutC
. You might want to fine-tune this to remove the extra semicolon.
这篇关于删除变量的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!