Dynamic libraries and static libraries for Linux

Source: Internet
Author: User

A library is essentially a binary format of executable code that can be loaded into memory for execution. Two kinds of library static library and dynamic library
Static Library: This type of library name is generally libxxx.a, XXX is the name of the library. A file compiled with a static library is larger because all the data for the entire library is integrated into the target code. The advantage is that after the mutation, the executing program does not require external function library support. Be sure that if the static function library changes, the program needs to be recompiled.
Dynamic Libraries: the names of such libraries are generally libxxx. M.n.so,xxx is the name of the library, M is the main version number of the library, and N is the secondary version number of the library. (The version number is not required.) Dynamic libraries are dynamically requested and loaded when the program is run, and the corresponding libraries must be available in the running environment of the program.
To use a static library, the connector finds the functions required by the program and then copies them to the execution file, because the copy is complete, so once the connection is successful, the static library is no longer needed. However, for dynamic libraries, this is not the case. The dynamic library leaves a tag inside the executor that indicates that the library must first be loaded when the program executes. Due to the space saving of the dynamic library, the default operation to connect under Linux is to connect the dynamic library first, that is, if both static and dynamic libraries exist and are not specifically specified, they will be connected to the dynamic library.

production of static libraries
Prepare two repositories of source files souce_one.c and SOUCE_TWO.C

$ cat souce_one.c#include <stdio.h>void print1(){    printf("souce_one\n");}$ cat souce_two.c#include <stdio.h>void print2(){    printf("souce_two\n");}

Compile into a static library:

$ gcc -c souce*.c$ ar -rsv libmytest.a souce*.o

Prepare the test program to test the generated static library:

$ cat test.cmain(){    print1();    print2();    return 0;}

To compile the link test program:

gcc test.c$ gcc test.c -L./ -lmytest$ ./a.out souce_onesouce_two## -L指定库文件的路径## -l使用的库的名字

Making of Dynamic library
The 3 files above are still used souce_one.c, SOUCE_TWO.C, test.c
Compiling a dynamic library

$ gcc -c souce_*.c$ gcc -shared -fPCI -o libtest.so souce_*.o## -shared :指定生成动态链接库## -fPIC:表示编译为位置独立的代码

Test the Dynamic Library

$ gcc Test. C-ltest#-L Specify Connection phase reference shared library$./A. out  #执行程序时会报错./A. out: Error while loading shared libraries:libtest. so: Cannot open Shared object File:no such fileordirectory$LDD./A. out # # Use LDD to view dependencies, find Libtest library not foundLinux-gate. so. 1= (0xb7783000) libtest. so= = Not Found libc. so. 6=/LIB/I386-LINUX-GNU/LIBC. so. 6(0xb75cb000)/lib/LD-linux. so. 2(0xb7784000)$CPLibtest. so/usr/lib/#将动态库拷贝到程序默认查找的目录$./A. outSouce_onesouce_two

Dynamic libraries and static libraries for Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.