cmake 在安装库之前链接到该库

f4t66c6m  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(163)

构建时如何将链接器指向临时路径?

上下文:链接到尚未安装但将来会安装的库

...

set(LIBS_TO_BE_INSTALLED)
   libA.so
   libB.so
) 

set(FUTURE_LIB_PATH
    /usr/lib/future/path
) # Libraries are found here if installed

set(TEMPORARY_LIB_PATH
   /temporary/path
) # Libraries are found here before installing

add_library(${PROJECT_NAME} SHARED $SRCS $HDRS)
link_directories(AFTER $FUTURE_LIB_PATH) #this works if libs are installed
target_link_libraries(${PROJECT_NAME} ${LIBS_TO_BE_INSTALLED})

...

ldd输出应该是什么样子:

$ ldd ${PROJECT_NAME}.so
    ...
    libA.so.X => /usr/lib/future/path/libA.so.X
    libB.so.Y => /usr/lib/future/path/libB.so.Y
    ...

但是,当尝试使用临时路径时,它不起作用:

link_directories(AFTER $TEMPORARY_LIB_PATH)

失败,出现错误:

[100%] Linking CXX shared library ${PROJCET_NAME}.so
/usr/bin/ld: cannot find -llibA: No such file or directory
/usr/bin/ld: cannot find -llibB: No such file or directory
wgmfuz8q

wgmfuz8q1#

找到了答案

set(CMAKE_SKIP_RPATH true)
target_link_directories(${PROJECT_NAME} PUBLIC ${TEMPORARY_LIB_PATH})

ldd output before future libraries are installed:

$ ldd ${PROJECT_NAME}.so
    ...
    libA.so.X => not found
    libB.so.Y => not found
    ...

相关问题