问题描述
作者将一个库放入自定义的路径下面,现设其为 /home/username/testdir/libtest.so ,通过将test加入target_link_libraries后,如下
SET(ARG_DEPENDENCIES test)
target_link_libraries(XXXXXX ${ARG_DEPENDENCIES})
编译通过,但是运行时报错:
error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
原因分析
可能是可执行文件找不到这个路径下的库
解决方法
作者尝试在CMakeLists.txt中添加了如下语句
set(SELF_LIBRARIES "-Wl,-rpath=/home/username/testdir")
-Wl,-rpath的含义可参考文档
https://www.cnblogs.com/homejim/p/8004883.html
再将变量SELF_LIBRARIES 加入到target_link_libraries,这样可执行文件执行时就会到这个目录下去找库文件,如下
target_link_libraries(XXXXXX ${SELF_LIBRARIES} ${ARG_DEPENDENCIES})