背景: 链接到未安装但将来会安装的库
...
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
找到答案了
set(CMAKE_SKIP_RPATH true)
target_link_directories(${PROJECT_NAME} PUBLIC ${TEMPORARY_LIB_PATH})
ldd
输出之前安装未来的库:
$ ldd ${PROJECT_NAME}.so
...
libA.so.X => not found
libB.so.Y => not found
...