gcc link shared library against symbolic link

感情迁移 提交于 2019-11-30 19:11:26

When you build the shared libraries, add "-Wl,-soname=libtoaster.so.1" to the gcc flags (assuming you are linking with gcc). This sets DT_SONAME in the library, and will force any application linked against that library to have the name of the library taken from the DT_SONAME, rather than from the name of the file.

[vps@manticore]~/cprog/toaster1$ gcc -c my_app.c
[vps@manticore]~/cprog/toaster1$ gcc -c toaster.c
[vps@manticore]~/cprog/toaster1$ gcc -o libtoaster_a.so -shared -Wl,-soname=libtoaster.so toaster.o
[vps@manticore]~/cprog/toaster1$ gcc -R$(pwd) -L. -ltoaster_a -o my_app my_app.o
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
my_app: can't load library 'libtoaster.so'
my_app: exit status 4
[vps@manticore]~/cprog/toaster1$ ln -s libtoaster_a.so libtoaster.so
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
    Start    End      Type Open Ref GrpRef Name
    1c000000 3c004000 exe  1    0   0      my_app
    05b1f000 25b23000 rlib 0    1   0      /home/vps/cprog/toaster1/libtoaster.so
    084f9000 28532000 rlib 0    1   0      /usr/lib/libc.so.51.0
    09e80000 09e80000 rtld 0    1   0      /usr/libexec/ld.so
[vps@manticore]~/cprog/toaster1$
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!