在我写的另一篇博客中讲解了静态库的使用:https://www.cnblogs.com/Suzkfly/p/14298950.html

本篇讲解动态库的使用,还是编写两个测试程序,main.c和hello.c,在main.c中调用hello.c中的函数。

hello.c

1 #include <stdio.h>
2 
3 void hello(void)
4 {
5     printf("Hello World\n");
6 }

main.c

extern void hello(void);

int main(int argc, const char *argv[])
{
    hello();

    return 0;
}

创建动态库

  同静态库一样,动态库也要遵循一定的命名规则,动态库的命名也是以“lib”开头,不同的是后缀为".so"。

  执行命令:gcc -fPIC -Wall -c hello.c

  生成hello.o,-fPIC表示编译与地址无关的代码,-Wall表示生成所有警告信息。

  执行命令:gcc -shared hello.o -o libhello.so

  生成libhello.so

使用动态库

  编译程序:gcc main.c -L. -lhello

  此时生成a.out文件,但是该a.out文件不能直接运行,运行则会报错:

  ./a.out: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory

  那是因为程序运行时找不到链接的库。那么如何让程序运行时能找到链接库呢,有三种方法:

  1、把库拷贝到/lib或/usr/lib目录下

    拷贝过后直接运行./a.out即可,此时a.out不需要与当前目录下生成的libhello.so文件在同一路径下,因为当前路径下的libhello.so只是在编译时有用,运行时加载的是/lib或/usr/lib目录下的库文件。

    如果在/lib或/usr/lib目录下已经有了需要加载的动态库,那么编译的时候就不需要使用-L指定库的路径了,可以直接用命令:gcc main.c -lhello  (这个感觉和使用线程是要加-lpthread是一样的)

      实际上,把静态库拷贝到/lib或/usr/lib目录下也能用gcc main.c -lhello编译,并且如果当静态库和动态库同时存在时,优先链接的是动态库,如果要强制链接静态库的话,需要在编译时加-static参数,如下

gcc main.c -static -lhello

  2、在LD_LIBRARY_PATH环境变量中加上库所在的路径

    具体方法为在执行./a.out之前加一句LD_LIBRARY_PATH=.

LD_LIBRARY_PATH=. ./a.out

  3、将库路径加到/etc/ld.so.conf.d路径下的某个.conf文件中,并执行ldconfig刷新

  可以将路径写入到现成的文件中,也可以新建一个以.conf命名的文件中,比如在/etc/ld.so.conf.d路径下新建一个hello.conf,在文件内直接写上库文件路径,保存退出后执行sudo ldconfig,之后便可运行a.out。

查看库中的符号

  有时候可能需要查看一个库中到底有哪些函数,nm命令可以打印出库中的涉及到的所有符号。库既可以是静态的也可以是动态的。比如将对刚刚生成的libhello.so使用nm命令:

linux@ubuntu:~/zkf/test/library$ nm libhello.so
00001f28 a _DYNAMIC
00001ff4 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
00001f18 d __CTOR_END__
00001f14 d __CTOR_LIST__
00001f20 d __DTOR_END__
00001f1c d __DTOR_LIST__
0000056c r __FRAME_END__
00001f24 d __JCR_END__
00001f24 d __JCR_LIST__
00002010 A __bss_start
         w __cxa_finalize@@GLIBC_2.1.3
00000490 t __do_global_ctors_aux
000003a0 t __do_global_dtors_aux
0000200c d __dso_handle
         w __gmon_start__
00000457 t __i686.get_pc_thunk.bx
00002010 A _edata
00002018 A _end
000004c8 T _fini
00000324 T _init
00002010 b completed.6159
00002014 b dtor_idx.6161
00000420 t frame_dummy
0000045c T hello
         U puts@@GLIBC_2.0
linux@ubuntu:~/zkf/test/library$ ldd ./a.out
checking sub-depends for 'libhello.so'
checking sub-depends for 'not found'
    libhello.so => libhello.so (0x00000000)
    libc.so.6 => not found (0x00000000)
    not a dynamic executable

  nm列出的符号有很多,常见的有三种:

  一种是在库中被调用,但没有在库中定义,说明需其他库的支持,用“U”表示,比如上面的puts。

  一种是库中定义的函数,用“T”表示,比如上面的hello。

  一种是所谓的“弱态”符号,它们虽然在库中被定义,但是可能被其他库中的同名函数覆盖,用“W”表示。

 

  ldd命令可以查看一个可执行程序依赖的动态库,比如:

linux@ubuntu:~/zkf/test/library$ ldd ./a.out 
checking sub-depends for 'libhello.so'
checking sub-depends for 'not found'
    libhello.so => libhello.so (0x00000000)
    libc.so.6 => not found (0x00000000)
    not a dynamic executable