Specify soname when creating shared library


http://man7.org/conf/lca2006/shared_libraries/slide4b.html


The shared library soname (cont.)

Here's how to use a soname:

  1. Specify soname when creating shared library:
    $ gcc -fPIC -c -Wall -g mod1.c mod2.c mod3.c 
    $ gcc -shared -Wl,-soname,libbar.so -o libfoo.so \
               mod1.o mod2.o mod3.o
    
    -Wl,-soname,libbar.so instructs linker to mark the shared library libfoo.so with the sonamelibbar.so.
  2. Create executable:
    $ gcc -g -Wall -o prog prog.c libfoo.so
    
    Linker detects that libfoo.so contains the soname libbar.soand embeds the latter name inside the executable.
  3. Run the program:
    $ LD_LIBRARY_PATH=. ./prog
    ./prog: error in loading shared libraries: 
    libbar.so: cannot open shared object file: 
    No such file or directory
    
    Dynamic linker cannot find anything named libbar.so.
  4. Create a symbolic link from the soname to the real name of the library:
    $ ln -s libfoo.so libbar.so
    $ LD_LIBRARY_PATH=. ./prog
    Called mod1-x1
    Called mod2-x2
    
At run-time thislink can point to a version of the library which is different from the version against which linking was performed.


posted @   张同光  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示