[转载] linux下创建和使用静态和动态库

本文介绍了Linux环境下两种主要的C/C++库类型:静态库(.a)和动态链接共享对象库(.so)。详细解释了如何创建这两种类型的库,包括编译选项、链接过程以及依赖性的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【转贴地址】http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

 

There are two Linux C/C++ library types which can be created:

   1. Static libraries (.a): Library of object code which is linked with, and becomes part of the application.
   2. Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
         1. Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.
         2. Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions.

Library naming conventions:
Libraries are typically names with the prefix "lib". This is true for all the C standard libraries. When linking, the command line reference to the library will not contain the library prefix or suffix.

Thus the following link command: gcc src-file.c -lm -lpthread
The libraries referenced in this example for inclusion during linking are the math library and the thread library. They are found in /usr/lib/libm.a and /usr/lib/libpthread.a.

Static Libraries: (.a)

How to generate a library:

    * Compile: cc -Wall -c ctest1.c ctest2.c
      Compiler options:
          o -Wall: include warnings. See man page for warnings specified.
    * Create library "libctest.a": ar -cvq libctest.a ctest1.o ctest2.o
    * List files in library: ar -t libctest.a
    * Linking with the library:
          o cc -o executable-name prog.c libctest.a
          o cc -o executable-name prog.c -L/path/to/library-directory -lctest
    * Example files:
          o ctest1.c

            void ctest1(int *i)
            {
               *i=5;
            }
                       

          o ctest2.c

            void ctest2(int *i)
            {
               *i=100;
            }
                       

          o prog.c

            #include <stdio.h>
            void ctest1(int *);
            void ctest2(int *);

            int main()
            {
               int x;
               ctest1(&x);
               printf("Valx=%d/n",x);

               return 0;
            }
                       

Historical note: After creating the library it was once necessary to run the command: ranlib ctest.a. This created a symbol table within the archive. Ranlib is now embedded into the "ar" command.

Note for MS/Windows developers: The Linux/Unix ".a" library is conceptually the same as the Visual C++ static ".lib" libraries.

Dynamically Linked "Shared Object" Libraries: (.so)

How to generate a shared object: (Dynamically linked object library file.) Note that this is a two step process.

   1. Create object code
   2. Create library
   3. Optional: create default version using a symbolic link.

Library creation example:

        gcc -Wall -fPIC -c *.c
        gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o //注意-Wl,-soname,libctest.so.1之间没有空格。
        mv libctest.so.1.0 /opt/lib
        ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
        ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
        
/*-Wl,-soname

-Wl 告诉编译器将后面的参数传递到连接器。而 -soname 指定了
共享库的 soname
 
这个soname的存在是为了兼容方便。
 
比如:
    有一个程序ap1,以及一个库libtest.so.1
    ap1启动的时候需要libtest.so.1
    如果链接的时候直接把libtest.so.1传给了ap1,那么将来库升级为libtest.so.2的时候,ap1仍然只能使用 libtest.so.1的代码,并不能得到升级的好处。而如果指定了soname为libtest.so,那么ap1启动的时候将查找的就是 libtest.so而不是其在被链接时实际使用的库libtest.so.1这个文件名。
    在开始时我们建立一个链接:ln -sf libtest.so.1 libtest.so
    而在库升级后,我们重新:ln -sf libtest.so.2 libtest.so即可,这样ap1不需要任何变动就能享受升级后的库的特性了。而libtest.so.1,libtest.so.2可以同时存在于系统内,不必非得把libtest.so.2的名字改成libtest.so.1*/    

    This creates the library libctest.so.1.0 and symbolic links to it.

    Compiler options:

        * -Wall: include warnings. See man page for warnings specified.
        * -fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries. Also see "-fpic".
        * -shared: Produce a shared object which can then be linked with other objects to form an executable.
        * -W1: Pass options to linker.
          In this example the options to be passed on to the linker are: "-soname libctest.so.1". The name passed with the "-o" option is passed to gcc.
        * Option -o: Output of operation. In this case the name of the shared object to be output will be "libctest.so.1.0"

    Library Links:

        * The link to /opt/lib/libctest.so allows the naming convention for the compile flag -lctest to work.
        * The link to /opt/lib/libctest.so.1 allows the run time binding to work. See dependency below.

Compile main program and link with shared object library:

    Compiling for runtime linking with a dynamically linked libctest.so.1.0:

        gcc -Wall -I/path/to/include-files -L/path/to/libraries prog.c -lctest -o prog
    Use:
        gcc -Wall -L/opt/lib prog.c -lctest -o prog
         

    Where the name of the library is libctest.so. (This is why you must create the symbolic links or you will get the error "/usr/bin/ld: cannot find -lctest".)
    The libraries will NOT be included in the executable but will be dynamically linked during runtime execution.

List Dependencies:

The shared library dependencies of the executable can be listed with the command: ldd name-of-executable

    Example: ldd prog

            libctest.so.1 => /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
            libc.so.6 => /lib64/tls/libc.so.6 (0x0000003aa4e00000)
            /lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)
       

Run Program:

    * Set path: export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH
    * Run: prog

Man Pages:

    * gcc - GNU C compiler
    * ld - The GNU Linker
    * ldd - List dependencies

Links:

    * LDP: Shared libraries

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值