CMake - Make a list of paths absolute.

安稳与你 提交于 2019-12-11 07:31:45

问题


I have a variable:

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

Which comes from:

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}
       )

Because of the issue discussed in This Question, I need all of these paths to be relative.

I have tried this:

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()

But when I use target_link_libraries(${name} ${external_libraries_rel}) I get an Undefined Reference Error for all of the functions related to the library I am trying to link. Indicating that the library has not actually been linked.

Is my makeLibPathsAbsolute() function correct?


回答1:


By default, all variables set in the function are not visible outside. (In other words, variable's definition is scoped to the function).

For make variable visible for the function's caller, use PARENT_SCOPE option of the set() command.

E.g. you may "publish" external_libraries_rel list by appending this line to the end of the function:

set(external_libraries_rel ${external_libraries_rel} PARENT_SCOPE)


来源:https://stackoverflow.com/questions/46041606/cmake-make-a-list-of-paths-absolute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!