linux动态库、静态库生成和使用小实验

来源:互联网 发布:淘宝网冬季帽子 编辑:程序博客网 时间:2024/04/28 03:37

os:

-sh-2.05b$ uname -a
Linux localhost.localdomain 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

1. file list
/* libtest.c */
#include <libhello.h>

int main()
{
    sayhello();
    return 0;
}
/* libhello.c */
#include <stdio.h>
void sayhello()
{
    printf("hello from libhello/n");
    return;
}

/* libhello.h */
#ifndef _LIBHELLO_
#define _LIBHELLO_

void sayhello();

#endif

2. complie

gcc -o libtest.o -c libtest.c -I.
//static lib
gcc -o libhello.o -c libhello.c -I.

/** after ar, u can delete libhello.o, linker interests only in libhello.a **/
ar cqs libhello.a libhello.o
//dynamic lib
gcc -o libhello.so.1.0 -shared -Wl,-soname,libhello.so.1 libhello.o -L.
ln -s libhello.so.1.0 libhello.so.1
ln -s libhello.so.1 libhello.so

>ll
-rw-r--r--    1 borby    borby         946 Jul 17 01:20 libhello.a
-rw-r--r--    1 borby    borby          87 Jul 16 23:07 libhello.c
-rw-r--r--    1 borby    borby          64 Jul 16 23:07 libhello.h
-rw-r--r--    1 borby    borby         800 Jul 17 01:20 libhello.o
lrwxrwxrwx    1 borby    borby          13 Jul 17 01:22 libhello.so -> libhello.so.1
lrwxrwxrwx    1 borby    borby          15 Jul 17 01:22 libhello.so.1 -> libhello.so.1.0
-rwxr-xr-x    1 borby    borby        6552 Jul 17 01:22 libhello.so.1.0
-rw-r--r--    1 borby    borby          68 Jul 16 23:20 libtest.c
-rw-r--r--    1 borby    borby         712 Jul 17 01:20 libtest.o

3. link
1)static link:
    /**
      * 连接库的时候搜索符号表的顺序有关系, 把最底层的库放到最后面
      */
    /* error link */
    -sh-2.05b$ gcc -Wl,-Bstatic -lhello -o libtest.static libtest.o -L.
               libtest.o(.text+0x11): In function `main':
               : undefined reference to `sayhello'
               collect2: ld returned 1 exit status
    /* right link */
    -sh-2.05b$ gcc libtest.o -o libtest.static -Wl,-Bstatic -lhello -L.
   
    /** static linkage makes executable file largest **/
    -rwxr-xr-x    1 borby    borby      423437 Jul 17 01:30 libtest.static   
    /** and symbols are all T flag **/
  -sh-2.05b$ nm libtest.static | grep -w printf
  08048874 T printf
  -sh-2.05b$ nm libtest.static | grep -w sayhello
  080481ac T sayhello

  -sh-2.05b$ ldd libtest.static
        not a dynamic executable
  
2)dynamic link
    /* 参数循序无关系 */
    gcc -o libtest.dynamic -Wl,-Bdynamic -lhello libtest.o -L.

    /** executable file are smaller **/
  -rwxr-xr-x    1 borby    borby       11799 Jul 17 01:31 libtest.dynamic
    /**
        and symbols are different,
        means it'll find sayhello in another dynamic library ,
        it does not care about printf which accessed by libhello
     **/
  -sh-2.05b$ nm libtest.dynamic | grep -w sayhello
           U sayhello
  -sh-2.05b$ nm libtest.dynamic | grep -w printf
  -sh-2.05b$

  -sh-2.05b$ ldd libtest.dynamic
        libhello.so.1 => /lib/libhello.so.1 (0x40021000)
        libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)


default link
    gcc -o libtest.def.0 -lhello -L. libtest.o

    /** in default, linker finds libhello.so(ignores libhello.a ) to make dynamic linkage **/
  -rwxr-xr-x    1 borby    borby       11799 Jul 17 01:31 libtest.dynamic
  -rwxr-xr-x    1 borby    borby       11799 Jul 17 01:34 libtest.def.0
  -sh-2.05b$ diff libtest.def.0 libtest.dynamic   
  -sh-2.05b$
   
   
    mkdir slib; mv libhello.a slib
    /** 参数顺序问题 error link **/
    gcc -o libtest.def.1 -lhello -Lslib libtest.o
    /** right link **/
    gcc libtest.o -o libtest.def.1 -lhello -Lslib
   
    /** linker dynamic links libhello.a which is a static library **/
    -rwxr-xr-x    1 borby    borby       11668 Jul 17 01:36 libtest.def.1
    /** and symbols are as follows : sayhello are defined in executable file while printf
    -sh-2.05b$ nm libtest.def.1 | grep -w printf
         U printf@@GLIBC_2.0
    -sh-2.05b$ nm libtest.def.1 | grep -w sayhello
         08048344 T sayhello
   
    /** contrasted with libhello.a **/
  -sh-2.05b$ nm libhello.a | grep -w printf
           U printf
  -sh-2.05b$ nm libhello.a | grep -w sayhello
       00000000 T sayhello
   
   
    mkdir dlib; mv libhello.so* dlib
    gcc -o libtest.def.2 -lhello -Ldlib libtest.o   
    /** same as libtest.def.0 and libtest.dynamic **/
  -rwxr-xr-x    1 borby    borby       11799 Jul 17 01:40 libtest.def.2
  
  -sh-2.05b$ ldd libtest.def.0 libtest.def.1 libtest.def.2
libtest.def.0:
        libhello.so.1 => /lib/libhello.so.1 (0x40021000)
        libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
libtest.def.1:
        libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
libtest.def.2:
        libhello.so.1 => /lib/libhello.so.1 (0x40021000)
        libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)