error while loading shared libraries

2024-01-02 21:04:01

调试log4cplus示例时,运行时找不到库

log4/log4cpp$ g++ main.cpp -o test -llog4cplus -L/usr/local/lib
log4/log4cpp$ ./test 
./test: error while loading shared libraries: liblog4cplus-2.0.so.3: cannot open shared object file: No such file or directory

首先确保库存在

log4/log4cpp$ whereis liblog4cplus-2.0.so.3
liblog4cplus-2.0.so: /usr/local/lib/liblog4cplus-2.0.so.3
方法一:在终端设置库链接路径

缺点:只能在此终端有效,开另一个终端LD_LIBRARY_PATH又恢复原来的值

log4/log4cpp$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
log4/log4cpp$ echo $LD_LIBRARY_PATH
/usr/local/lib:
log4/log4cpp$ ./test 
WARN - Hello, World!
方法二:修改/etc/ld.so.conf文件

将/usr/local/lib添加在etc/ld.so.conf 后面

优点:后续的终端都有效

log4/log4cpp$ ./test 
./test: error while loading shared libraries: liblog4cplus-2.0.so.3: cannot open shared object file: No such file or directory
log4/log4cpp$ sudo vi /etc/ld.so.conf
修改为 include /etc/ld.so.conf.d/*.conf /usr/local/lib
log4/log4cpp$ sudo ldconfig
log4/log4cpp$ ./test 
WARN - Hello, World!
方法三:在QtCreator工程文件中修改

添加下面语句

LIBS    += -llog4cplus -L/usr/local/lib/

再ctrl+B 编译,F5运行?

文章来源:https://blog.csdn.net/ylxwk/article/details/135347734
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。