Linux动态库、静态库加载基础

Linux动态库、静态库加载基础
静态库
       在linux环境中, 使用ar命令创建静态库文件.如下是命令的选项:
          d -----从指定的静态库文件中删除文件
          m -----把文件移动到指定的静态库文件中
          p -----把静态库文件中指定的文件输出到标准输出
          q -----快速地把文件追加到静态库文件中
          r -----把文件插入到静态库文件中
          t -----显示静态库文件中文件的列表
          x -----从静态库文件中提取文件
          还有多个修饰符修改以上基本选项,详细请man ar 以下列出三个:
          a -----把新的目标文件(*.o)添加到静态库文件中现有文件之后
          b -----***************************************之前
          v -----使用详细模式       ar 命令的命令行格式如下:       ar [-]{dmpqrtx}[abcfilNoPsSuvV] [membername] [count] archive files...      eg:         ar -crs hello.a hello.c
动态库
1.创建共享库
     gcc -c error.c               
     gcc -c errorlog.c
     gcc -shared -o libapue.so error.o errorlog.o
这样就创建了共享库!
2.编译共享库
    假设共享库位于当前目录(即跟程序文件相同的目录中)
gcc -o test -L. -lapue test.c
这样就编译出了不包含函数代码可执行文件了,但是但你运行时会发现linux动态加载器打不到libapue.so文件.
可以用ldd 命令查看可执行文件依赖什么共享库:
ldd test
如何才能让动态加载器发现库文件呢?有两种方法可以解决:
     LD_LIBRARY_PATH 环境变量  
     /etc/ld.so.conf文件
    1.环境变量
       export LD_LIBRARY_PATH="dir$LD_LIBRARY_PATH"
    2.修改/etc/ld.so.conf文件.位于/etc/ld.so.conf
一般应用程序的库文件不与系统库文件放在同一个目录下,一般把应用程序的共享库文件放在/usr/local/lib下,新建一个属于自己的目录apue,然后把刚才libapue.so复制过去就行了
同时在/etc/ld.so.conf中新增一行:
/usr/local/lib/apue
实例分析:
    test.c
         #include
         #include "print.h"
         
         int main(void){
             printf("Function : main/n");
             print();
             printf("out main/n");
   
             return 0;
         }
     
     print.c
         #include
         #include
         int print(void){
            
             printf("Function : print/n");
             printf("Hello world/n");
             printf("out print/n");
             return 0;
         }
       1 : 创建动态库:
          gcc -o libprint.so -fPIC -rdynamic -shared print.c
     2 : 创建可执行文件
          gcc -o test -I$(INC_PATH) -L$(LIB_PATH) -lprint test.c
  3: 如果出现下列错误
          ./test: error while loading shared libraries: libprint.so: cannot open   shared object file: No such file or directory
      解决方法:
       1: export LD_LIBRARY_PATH=DIR:$LD_LIBRARY_PATH
       2: 修改文件/etc/ld.so.config文件,在末行加上你的库文件目录,最后,ldconfig刷新ld.so.cache文件即可。可以用strings命令查看ld.so.cache是否刷新:strings /etc/ld.so.cache | grep print
     
4:   ldd命令查看动态连接库
            
            linux-gate.so.1 =>  (0xb7f0e000)
            libprint.so => not found
            libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7d82000)
            /lib/ld-linux.so.2 (0xb7ef4000)
5 : nm查看程序中有那些符号
      
049f18 d _DYNAMIC
08049ff4 d _GLOBAL_OFFSET_TABLE_
080485cc R _IO_stdin_used
         w _Jv_RegisterClasses
08049f08 d __CTOR_END__
08049f04 d __CTOR_LIST__
08049f10 D __DTOR_END__
08049f0c d __DTOR_LIST__
080485ec r __FRAME_END__
08049f14 d __JCR_END__
08049f14 d __JCR_LIST__
0804a018 A __bss_start
0804a010 D __data_start
08048580 t __do_global_ctors_aux
08048450 t __do_global_dtors_aux
0804a014 D __dso_handle
         w __gmon_start__
0804857a T __i686.get_pc_thunk.bx
08049f04 d __init_array_end
08049f04 d __init_array_start
08048510 T __libc_csu_fini
08048520 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
0804a018 A _edata
0804a020 A _end
080485ac T _fini
080485c8 R _fp_hw
0804839c T _init
08048420 T _start
0804a018 b completed.6625
0804a010 W data_start
0804a01c b dtor_idx.6627
080484b0 t frame_dummy
080484d4 T main
         U print
         U puts@@GLIBC_2.0
6: strip取出程序中符号
7: strings查看可执行文件中的文本信息
      
参考:http://www.kuqin.com/linux/20081103/25444.html
      http://hi.baidu.com/li_zhongnan/blog/item/1d9bf3c2e13a9f32e4dd3b4f.html

               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94960/showart_1973740.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值