Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497575
  • 博文数量: 59
  • 博客积分: 2968
  • 博客等级: 少校
  • 技术积分: 648
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 22:20
个人简介

IT圈泥瓦匠一枚,混迹过国产处理器圈,从事Linux BSP开发混一软件经理职务,现漂浮在云端从事OpenStack-Nova相关设计工作。

文章分类

全部博文(59)

文章存档

2014年(1)

2013年(2)

2012年(7)

2011年(26)

2010年(9)

2009年(14)

我的朋友

分类: LINUX

2009-06-23 00:06:22

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                <生成.o文件>
     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://hi.baidu.com/li_zhongnan/blog/item/1d9bf3c2e13a9f32e4dd3b4f.html



阅读(2636) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~