问题描述
我有一个变量:
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}
)
由于需要,我需要所有这些路径都是相对的.
Because of the issue discussed in This Question, I need all of these paths to be relative.
我已经尝试过了:
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})
时,对于与我要链接的库相关的所有功能,我都得到了未定义的引用错误.表示实际上尚未链接库.
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.
我的makeLibPathsAbsolute()
函数正确吗?
推荐答案
默认情况下,该函数中设置的所有变量在外部都不可见. (换句话说,变量的定义作用域作用于该函数).
By default, all variables set in the function are not visible outside. (In other words, variable's definition is scoped to the function).
要使该函数的调用者可见该变量,请使用PARENT_SCOPE 选项"rel =" nofollow noreferrer> set()命令.
For make variable visible for the function's caller, use PARENT_SCOPE option of the set() command.
例如您可以通过在函数末尾添加以下行来发布" external_libraries_rel
列表:
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)
这篇关于CMake-列出绝对路径.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!