如何使用cmake显示和返回列表
这是我第一次使用cmake,我对列表有两个问题: 1如何显示列表 2如何在函数中返回列表 这是我的密码:如何使用cmake显示和返回列表,cmake,Cmake,这是我第一次使用cmake,我对列表有两个问题: 1如何显示列表 2如何在函数中返回列表 这是我的密码: function(GET_ALL_DIRS where SEP) message (STATUS "Let's search all directories in ${where}") file (GLOB TMP_LIST_DIR ${where}${SEP}*) foreach (tmp_elem ${TMP_LIST_DIR}) if (IS_DIRECTORY $
function(GET_ALL_DIRS where SEP)
message (STATUS "Let's search all directories in ${where}")
file (GLOB TMP_LIST_DIR ${where}${SEP}*)
foreach (tmp_elem ${TMP_LIST_DIR})
if (IS_DIRECTORY ${tmp_elem})
list (APPEND "${every_class}" ${tmp_elem})
message ("We add ${tmp_elem}")
endif()
endforeach()
list (LENGTH "${every_class}" nb_elem)
message ("in the list there is ${nb_elem} elements")
set(${tst} "${every_class}" PARENT_SCOPE)
endfunction()
GET_ALL_DIRS (includes ${SEP})
list (LENGTH "${tst}" nb_elem)
message ("after get_all_dirs there is ${nb_elem} elements")
在函数中我有正确的元素数,但在它之后我有0。。。为什么?函数的参数规格 意味着CMake需要一个名称,而不是该名称的取消引用${..} 正确:
list(APPEND every_class ${tmp_elem})
list(LENGTH every_class nb_elem)
set(tst ${every_class} PARENT_SCOPE)
在CMake中,变量或列表的名称本身可以表示为另一个变量的差异。以下结构完全有效:
此类动态名称在函数和宏中广泛使用。您可以使用消息${list}打印列表。返回就是返回包含列表的变量。实际问题是什么?无论何时看到或在函数的参数规范中,都应该使用名称,而不是通过取消引用${..}获得的值。正确:listAPPEND every_class${tmp_elem},listLENGTH every_class nb_elem,settst${every_class}PARENT_SCOPE,好吧,这正是我问题的答案,你太棒了。编辑:我无法将您的评论设置为答案:/谢谢。第一次不容易理解,但是谢谢
set(my_var_name "a")
set(${my_var_name} "some value") # Assign value to variable 'a'
set(name_suffix "b")
list(APPEND list_${name_suffix} "other value") # Appends to a list 'list_b'.