在编译出可执行文件后,运行此可执行文件。运行时,链接器会在LD_LIBRARY_PATH下搜索库文件。
例子:
源码见上篇博文。
编译命令如下:
gcc testso.c -fPIC -shared -o libtest.so
gcc main.c -o test -ltest -L./
运行test,出现如下错误。
./test: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
编写run.sh脚本。内容如下:
export LD_LIBRARY_PATH=./
./test
修改run.sh文件权限。
chmod 776 run.sh
运行脚本
./run.sh
结果为:
this is in test_a...
this is in test_b...
this is in test_c...
本文介绍了解决在运行程序时遇到的动态库加载失败的问题。通过设置环境变量LD_LIBRARY_PATH并调整运行脚本,成功加载了所需的动态库并使程序正常运行。
5734

被折叠的 条评论
为什么被折叠?



