CMake - 制作绝对路径列表。

时间:2017-09-04 17:09:50

标签: path cmake shared-libraries

我有一个变量:

set(${PROJECT_NAME}_EXTERNAL_LIBRARIES
        ${PocoNetExternal_LIBRARIES}
)

来自:

set(EXTERNAL_NAME PocoNetExternal)
    set(${EXTERNAL_NAME}_LIBRARIES
        ${PROJECT_BINARY_DIR}/${EXTERNAL_NAME}/Foundation/${CMAKE_SHARED_LIBRARY_PREFIX}PocoFoundation${POCO_build_postfix}${CMAKE_STATIC_LIBRARY_SUFFIX}
        ${PROJECT_BINARY_DIR}/${EXTERNAL_NAME}/Util/${CMAKE_SHARED_LIBRARY_PREFIX}PocoUtil${POCO_build_postfix}${CMAKE_STATIC_LIBRARY_SUFFIX}
        ${PROJECT_BINARY_DIR}/${EXTERNAL_NAME}/Net/${CMAKE_SHARED_LIBRARY_PREFIX}PocoNet${POCO_build_postfix}${CMAKE_STATIC_LIBRARY_SUFFIX}
       )

由于This Question中讨论的问题,我需要所有这些路径都是相对的。

我试过这个:

function(makeLibPathsAbsolute)

    set(temp ${${PROJECT_NAME}_EXTERNAL_LIBRARIES})    #rename list
    set(external_libraries_rel)                        #make empty list

    list(LENGTH temp len1)                 #len1 is length of temp list
    math(EXPR len2 "${len1} - 1")           #len2 is len1 - 1

    foreach(val RANGE ${len2})                                 #for val = 0 to len2
        list(GET temp ${val} relPath)                         #relPath becomes the {val} entry of temp
        get_filename_component(absPath ${relPath} ABSOLUTE)   #make relPath Absolute and call it absPath
        list(APPEND external_libraries_rel ${absPath})        #Append this to the external_libraries_rel list
    endforeach()

endfunction()

但是当我使用target_link_libraries(${name} ${external_libraries_rel})时,我得到了与我试图链接的库相关的所有函数的未定义参考错误。表明图书馆实际上没有链接。

我的makeLibPathsAbsolute()功能是否正确?

1 个答案:

答案 0 :(得分:2)

默认情况下,函数中设置的所有变量在外部都不可见。 (换句话说,变量的定义范围到函数中。)

对于函数调用者的make变量可见,请使用set()命令的 PARENT_SCOPE 选项。

E.g。你可以"发布" external_libraries_rel列表,将此行附加到函数的末尾:

set(external_libraries_rel ${external_libraries_rel} PARENT_SCOPE)