Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cmake 字符串中的变量引用不通过“计算”;“设置”;_Cmake - Fatal编程技术网

Cmake 字符串中的变量引用不通过“计算”;“设置”;

Cmake 字符串中的变量引用不通过“计算”;“设置”;,cmake,Cmake,我想要和你一样的 不同的是,我的“值”条目包含对“键”(预期变量)的引用 例如,BASEPATH变量: BASEPATH:=/home/SomePath LIB_BASE?=$(BASEPATH)/lib/$(LIBREV) ACCELMOD_BASE=$(BASEPATH)/SomeOtherPath 我修改了我的CMakeLists.txt脚本,将每行的[Key/value]对提取为两个变量(见下面的代码),并以这样的对结束(注意,该值仍然包含对某个变量名称的引用,该名称通常在文件开头定

我想要和你一样的

不同的是,我的“值”条目包含对“键”(预期变量)的引用

例如,BASEPATH变量:

BASEPATH:=/home/SomePath
LIB_BASE?=$(BASEPATH)/lib/$(LIBREV)
ACCELMOD_BASE=$(BASEPATH)/SomeOtherPath
我修改了我的CMakeLists.txt脚本,将每行的[Key/value]对提取为两个变量(见下面的代码),并以这样的对结束(注意,该值仍然包含对某个变量名称的引用,该名称通常在文件开头定义):

我编写的代码如下:

# Part2
file(STRINGS revisions.mk ConfigContents)
foreach(NameAndValue ${ConfigContents})
    # Strip leading spaces
    string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
    #remove commented lines
    string(FIND ${NameAndValue} "#" commentIndex)
    if(${commentIndex} EQUAL 0)
            continue()
    endif()

    # Find variable name
    string(REGEX MATCH "^[^\\?:=]+" Name ${NameAndValue})
    # Find the value
    string(REGEX REPLACE "^(.+)=(.+)$" "\\2" Value ${NameAndValue})
    # Replace () with {} to denote a cmake's variable reference
    string(REGEX REPLACE "\\(([^)]+)\\)" "{\\1}" Value ${Value})

    # Set the variable
    set(${Name} ${Value})
    message("> Value of " ${Name} " : " ${${Name}})
endforeach()
我希望当我将键(名称)定义为变量(使用set命令)并将其值设置为键的对应值时,字符串中的引用将替换为引用变量的当前值

但事实并非如此。 e、 g.对于给定输入,循环结束前的消息命令返回:

>Value of BASEPATH: /home/SomePath
>Value of LIB_BASE : ${BASEPATH}/lib/${LIBREV}
>Value of ACCELMOD_BASE: $(BASEPATH)/SomeOtherPath
即使已经定义了BASEPATH

为了验证我的预期,我编写了以下简单代码来模拟循环中的行为:

set(BASE 123)
set(RIGHT '${BASE}/SomePath/AA')
set(LEFT_STR "LEFT")
set(${LEFT_STR} ${RIGHT})
message(">" ${LEFT} "<>" ${${LEFT_STR}})
set(基123)
集合(右“${BASE}/SomePath/AA”)
设置(左箭头“左”)
集合(${LEFT_STR}${RIGHT})
消息(“>”${LEFT}”“${${LEFT\u STR}}})
在本例中,${BASE}引用被正确替换并

'123/SomePath/AA'<>'123/SomePath/AA'
'123/SomePath/AA''123/SomePath/AA'
按预期返回


我可能出了什么问题?

cmake
中没有
eval
。常见且容易出错的方法是创建脚本,然后将其包括在内:

file(STRINGS revisions.mk ConfigContents)

set(varlist "")
foreach(NameAndValue ${ConfigContents})
    # Strip leading spaces
    string(REGEX REPLACE "^[ ]+" "" NameAndValue ${NameAndValue})
    #remove commented lines
    string(FIND ${NameAndValue} "#" commentIndex)
    if(${commentIndex} EQUAL 0)
            continue()
    endif()

    # Find variable name
    string(REGEX MATCH "^[^\\?:=]+" Name ${NameAndValue})
    # Find the value
    string(REGEX REPLACE "^(.+)=(.+)$" "\\2" Value ${NameAndValue})
    # Replace () with {} to denote a cmake's variable reference
    string(REGEX REPLACE "\\(([^)]+)\\)" "{\\1}" Value ${Value})

    # Set the variable
    set(${Name} ${Value})
    list(APPEND varlist ${Name})
endforeach()

set(script "")
foreach(i IN LISTS varlist)
        string(APPEND script "set(${i} \"${${i}}\")\n")
endforeach()
file(WRITE script2.cmake ${script})
include(script2.cmake)

foreach(i IN LISTS varlist)
        message(STATUS ${i}=${${i}})
endforeach()
这将创建包含以下内容的脚本
script2.cmake

set(BASEPATH "/home/SomePath")
set(LIB_BASE "${BASEPATH}/lib/${LIBREV}")
set(ACCELMOD_BASE "${BASEPATH}/SomeOtherPath")

然后
包括它。包含这样的脚本将重新计算表达式,从而解析引用。

您正在将make移植到cmake吗?您可以
make-f revisions.mk-f这是一个很大的阻力,但它可以工作!。谢谢你的回复。
set(BASEPATH "/home/SomePath")
set(LIB_BASE "${BASEPATH}/lib/${LIBREV}")
set(ACCELMOD_BASE "${BASEPATH}/SomeOtherPath")