代码之家  ›  专栏  ›  技术社区  ›  Oscar

如何使用cmake显示和返回列表

  •  1
  • Oscar  · 技术社区  · 8 年前

    这是我第一次接触cmake,我有两个关于列表的问题:

    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 ${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…为什么?

    1 回复  |  直到 8 年前
        1
  •  4
  •   Tsyvarev    8 年前

    功能的参数规格

    • <列表>
    • <变量>

    意味着CMake期望 名称 ,而不是取消对该名称的引用( ${..} ).

    对的:

    list(APPEND every_class ${tmp_elem})
    
    list(LENGTH every_class nb_elem)
    
    set(tst ${every_class} PARENT_SCOPE)
    

    在CMake中,变量或列表的名称本身可能表示为另一个变量的差异。以下结构为 完全有效 :

    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'.