5.9 静态库和动态库的使用

目录

程序库

库的概念

库的知识

静态库的特点

静态库的创建

 共享库

共享库的特点

共享库的创建

创建共享库 common      

加载共享库

如何找到共享库

笔记


程序库

库的概念

库是一个二进制文件,包含的代码可被程序调用

标准C库、数学库、线程库……

库有源码,可下载后编译;也可以直接安装二进制包

/lib   /usr/lib

库的知识

库是事先编译好的,可以复用的代码。

在OS上运行的程序基本上都要使用库。使用库可以提高开发效率。

Windows和Linux下库文件的格式不兼容

Linux下包含静态库和共享库

静态库的特点

编译(链接)时把静态库中相关代码复制到可执行文件中

程序中已包含代码,运行时不再需要静态库

程序运行时无需加载库,运行速度更快

占用更多磁盘和内存空间

静态库升级后,程序需要重新编译链接

静态库的创建

//确定库中函数的功能、接口 

//编写库源码hello.c
     #include  <stdio.h>
     void  hello(void) {
        printf(“hello  world\n”);
        return;
     }

//编译生成目标文件
     $ gcc  -c  hello.c  -Wall

创建静态库 hello

$ ar  -rsv  libhello.a  hello.o\

ar 参数:

c  禁止在创建库时产生的正常消息

r  如果指定的文件已经存在于库中,则替换它

s  无论 ar 命令是否修改了库内容都强制重新生成库符号表

v 将建立新库的详细的逐个文件的描述写至标准输出

q 将指定的文件添加到库的末尾

t  将库的目录写至标准输出

查看库中符号信息

     $nm  libhello.a

      hello.o:

      0000000 T  hello

                     U  puts

 nm:显示指定文件中的符号信息

   -a 显示所有符号    

//编写应用程序test.c
    #include  <stdio.h>
     void  hello(void);    
     int  main() {
        hello();
        return  0;
     }

//编译test.c 并链接静态库libhello.a
     $  gcc  -o  test  test.c  -L.  -lhello
     $ ./test
      hello  world

 共享库

共享库的特点

编译(链接)时仅记录用到哪个共享库中的哪个符号,不复制共享库中相关代码

程序不包含库中代码,尺寸小

多个程序可共享同一个库

程序运行时需要加载库 库升级方便,无需重新编译程序

使用更加广泛

共享库的创建

//确定库中函数的功能、接口 

//编写库源码hello.c  bye.c
     #include  <stdio.h>
     void  hello(void) {
        printf(“hello  world\n”);
        return;
     }

//编译生成目标文件
     $ gcc  -c  -fPIC  hello.c  bye.c  -Wall

创建共享库 common      

$ gcc  -shared  -o  libcommon.so.1  hello.o  bye.o

为共享库文件创建链接文件      

$ ln  -s  libcommon.so.1  libcommon.so        

符号链接文件命名规则      

lib<库名>.so

//编写应用程序test.c
    #include  <stdio.h>
    #include “common.h”  
     int  main() {
        hello();
        bye();
        return  0;
     }
//编译test.c 并链接共享库libcommon.so
     $  gcc  -o  test  test.c  -L.  -lcommon
     

加载共享库

//执行程序
   $ ./test
   ./test: error while loading shared libraries: libcommon.so
   cannot open shared object file : No such file or directory

//添加共享库的加载路径
   $  export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
   $ ./test
      hello  world
      bye!

如何找到共享库

为了让系统能找到要加载的共享库,有三种方法 :

把库拷贝到/usr/lib和/lib目录下

在LD_LIBRARY_PATH环境变量中添加库所在路径

添加/etc/ld.so.conf.d/*.conf文件,执行ldconfig刷新


笔记

打开目录

#include  <dirent.h>

 DIR  *opendir(const char *name);

 DIR *fdopendir(int fd);  使用文件描述符,要配合open函数使用

DIR是用来描述一个打开的目录文件的结构体类型

成功时返回目录流指针;出错时返回NULL

读取目录

#include  <dirent.h>

 struct  dirent *readdir(DIR *dirp);

struct dirent是用来描述目录流中一个目录项的结构体类型

包含成员char  d_name[256]   参考帮助文档

成功时返回目录流dirp中下一个目录项;

出错或到末尾时时返回NULL

关闭目录

closedir函数用来关闭一个目录文件:

 #include  <dirent.h>

 int closedir(DIR *dirp);

成功时返回0;出错时返回EOF

修改文件权限

chmod/fchmod函数用来修改文件的访问权限:

 #include  <sys/stat.h>

 int  chmod(const char *path, mode_t mode);

 int  fchmod(int fd, mode_t mode);

成功时返回0;出错时返回EOF

注意:在vmware和windows共享的文件夹下,有些权限不能改变。

获取文件属性

#include  <sys/stat.h>

 int  stat(const char *path, struct stat *buf);

 int  lstat(const char *path, struct stat *buf);

 int  fstat(int fd, struct stat *buf);

成功时返回0;出错时返回EOF

如果path是符号链接stat获取的是目标文件的属性;而lstat获取的是链接文件的属性

库的概念:

静态库

创建静态库步骤:

1 . 编写库文件代码,编译为.o 目标文件。

  1. ar 命令 创建  libxxxx.a 文件

   ar -rsv  libxxxx.a  xxxx.o

   注意:1 静态库名字要以lib开头,后缀名为.a

2 没有main函数的.c 文件不能生成可执行文件。

链接错误:

test.c:(.text+0x15):对‘hello’未定义的引用

collect2: error: ld returned 1 exit status

含义:表示hello函数在编译的源码内没有找到实现

解决:实现代码或者找到对应函数的库并且链接它。

链接静态库:

gcc -o 目标文件 源码.c  -L路径  -lxxxx

-L  表示库所在的路径

-l 后面跟库的名称

动态库的生成步骤:

  1. 生成位置无关代码的目标文件

 gcc  -c  -fPIC  xxx.c xxxx.c ....

  1. 生成动态库

   gcc  -shared -o libxxxx.so  xxx.o  xxx.o ....

  1. 编译可执行文件

gcc -o 目标文件 源码.c  -L路径  -lxxxx

执行动态库的可执行文件错误

./test: error while loading shared libraries: libmyheby.so: cannot open shared object file: No such file or directory

含义:可执行文件所使用的动态库找不到

解决办法:

找到动态库,添加到/usr/lib里面

或者使用export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:你的动态库目录

添加在~/.bashrc 文件里面

使用source ~/.bashrc 生效。

查看可执行文件使用的动态库:

ldd 命令 :   ldd 你的可执行文件

root@haas-virtual-machine:/mnt/hgfs/share/newIOP# ldd test

linux-vdso.so.1 =>  (0x00007fff6548d000)

libmyheby.so => /mnt/hgfs/share/newIOP/day5/libmyheby.so (0x00007f5c89521000)

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5c89144000)

/lib64/ld-linux-x86-64.so.2 (0x000055fe52211000)

root@haas-virtual-machine:/mnt/hgfs/share/newIOP/day5# ldd test

linux-vdso.so.1 =>  (0x00007ffcb652c000)

libmyheby.so => not found

libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbeeffaf000)

/lib64/ld-linux-x86-64.so.2 (0x0000561003c3b000)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值