Static libraries and dynamic libraries under Linux

Source: Internet
Author: User
Tags mul one table

Static Library

Let's talk about why we need a library.
When there is a lot of code we will use in the program such as (scanf,printf, etc.) These functions we need to use in the program frequently, so we will compile the code as a library file, when we need to use the direct link.

Defined:

? The program then 编译 links the code of the static library to the executable program, which no longer requires a static library when the code is run. (Simple to understand is to pack a bunch of .o files together, when needed to let our program link in)

How to generate and use:

?? Here we use subtraction to illustrate:

Create required files [[email protected] ku]# touch add.c add.h sub.c sub.h mul.c mul.h dev.c dev.h main.c [[email protected] ku]# lsadd.c add.h dev.c dev.h main.c mul.c mul.h sub.c sub.h[[email protected] ku]#//write all file codes//add.c#includ                                                                                                                       E "Add.h" int add (int x,int y) {return x+y;}                                                                                                                                       Add.h#ifndef __add_h__#define __add_h__int ADD (int x,int y); #endif//__add_h__//sub.c#include "sub.h" int s                                                                                                                  UB (int x,int y) {return x-y;} Sub.h#ifndef __sub_h__#define __sub_h__int SUB (int x,int y);                                                                             #endif//__sub_h__//mul.c#include "mul.h" int mul (int x,int y)                                     {return x*y;} Mul.h#ifndef __mul_h__#define __mul_h__int mul (int x,int y);                                                                                                                  #endif//__mul_h__//dev.c#include "dev.h" int dev (int x,int y) {return x/y;}                                                                                                                 Dev.h#ifndef __dev_h__ #define __DEV_H__INT DEV (int x,int y); #endif//__dev_h__//main.c#include<stdio.h> #include "add.h" #i    Nclude "Sub.h" #include "mul.h" #include "dev.h" int main () {int A, B;    scanf ("%d%d", &a,&b);    printf ("%d +%d =%d\n", A,b,add (A, b));    printf ("%d-%d =%d\n", A,b,sub (A, b));    printf ("%d *%d =%d\n", A,b,mul (A, b));                                                                                                printf ("%d/%d =%d\n", A,b,dev (A, b)); return 0;}  compiling source files [[email protected] ku]# lsadd.c add.h dev.c dev.h main.cMUL.C mul.h sub.c sub.h[[email protected] ku]# gcc-c *.c//Generate. o files for all. c files [[email protected] ku]# lsadd.c add . h add.o dev.c dev.h dev.o main.c main.o mul.c mul.h mul.o sub.c sub.h sub.o[[email protected] ku]# rm ma  IN.O-RF//delete redundant. o files [[email protected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o main.c mul.c mul.h MUL.O SUB.C sub.h sub.o[[email protected] ku]#//Generate static library [[email protected] ku]# AR-RC LIBMYCAL.A *.o[[email pro Tected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o libmycal.a main.c mul.c mul.h mul.o sub.c sub.h Sub.o[[ema   Il protected] ku]#//view static library [[email protected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o libmycal.a main.c  MUL.C mul.h MUL.O sub.c sub.h sub.o[[email protected] ku]# ar-tv libmycal.a rw-r--r--0/0 683 APR 26 20:46 2018 add.orw-r--r--0/0 683 April 20:46 2018 dev.orw-r--r--0/0 679 Apr 20:46 2018 mul.orw-r--r--0/0 687 Ap R-20:46 2018 Sub.o[[eMail protected] ku]#//link static library generate executable [[[email protected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o libmycal. A main.c mul.c mul.h mul.o sub.c sub.h sub.o[[email protected] ku]# gcc main.c-l.-lmycal[[email protect Ed] ku]# lsadd.c add.h add.o a.out dev.c dev.h dev.o libmycal.a main.c mul.c mul.h mul.o sub.c sub.h sub.o[[ Email protected] ku]#//Run results [[email protected] ku]# lsadd.c add.h add.o a.out dev.c dev.h dev.o libmycal. A main.c mul.c mul.h mul.o sub.c sub.h sub.o[[email protected] ku]#./a.out 8 38 + 3 = 118-3 = 58 * 3 = 248  /3 = 2[[email protected] ku]#

以上是整个静态库的生成及运用过程
Sum up on 3 steps:

? First compile the source file into the target file: gcc–c source file
? generate Static Library: Ar–rc Lib (library name). A target file
? Using the Static library: GCC main.c-l (path of the Library)-L (library name)

Advantages and disadvantages of static libraries

# # # Advantages:
? 1. Save SPACE: Linker will only copy the objects you use.
? 2. Easy to pack.
# # # Disadvantages:
1. If there are global variables in the static library, then using them in several modules will cause the global variables to have different values, which is a very serious problem.
? 2, Static library compile, will not be linked to check, so many static library problems, in the generation of static library phase check out.
3, several modules, referencing the same static library, if there is a module is not compiled to, will cause a huge difference caused by the problem.
? 4. Generating a large number of library file files will occupy space

Dynamic Library Definitions:

? The program is 运行 then linked to the code of the dynamic library, and multiple programs share code that uses the library.
? An executable that is linked to a dynamic library contains only one table of the function entry address that he uses, not the machine code of the destination file where the external function resides.

How to generate and use:

The case procedure is the same as above, just write the procedure

[[email protected] ku]# lsadd.c add.h dev.c dev.h main.c mul.c mul.h sub.c sub.h[[email protected] ku]# Gcc-c-fpic *.c//-fpic is compiled into a location-independent code, without this option, the compiled code is location-dependent, so dynamic loading is done through code copies to meet the needs of different processes, but not to achieve the purpose of real code segment sharing.   [[email protected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o main.c main.o mul.c mul.h mul.o sub.c sub.h  Sub.o[[email protected] ku]# rm main.o-rf[[email protected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o MAIN.C mul.c mul.h mul.o sub.c sub.h sub.o[[email protected] ku]# gcc-shared-o libmycal.so *.O[[EMAIL&NBSP;PR Otected] ku]# lsadd.c add.h add.o dev.c dev.h dev.o libmycal.so main.c mul.c mul.h mul.o sub.c sub.h sub.o[[e   Mail protected] ku]# gcc main.c-l.-lmycal//-l. Represents the library to be connected in the current directory [[email protected] ku]# lsadd.c add.h add.o a.out dev.c dev.h dev.o libmycal.so main.c mul.c m Ul.h MUL.O sub.c sub.h sub.o[[email protected] ku]#./a.out./a.out:error while loading GKFX LIBRAries:libmycal.so:cannot open Shared object file:no such file or directory[[email protected] ku]# CP libmycal.so/ lib///move the dynamic library to the system library file [[email protected] ku]# lsadd.c add.h add.o a.out dev.c dev.h dev.o libmycal.so main.c mu L.C mul.h MUL.O sub.c sub.h sub.o//operation results [[email protected] ku]#./a.out 8 68 + 6 = 148-6 = 28 * 6 = 488/6 = 1 The second method, change the library path [[email protected] ku]#./main./main:error while loading shared libraries:libmycal.so:cannot open sh Ared object File:no such file or directory[[email protected] ku]# vim/etc/ld.so.conf.d/mycal.conf//write the path of the library in the created files /write path refresh buffer [[[email protected] ku]# ldconfig//Run result [[email protected] ku]#./main 8 68 + 6 = 148-6 = 28 * 6 = 488/6 = 1
Dynamic Library pros and Cons: Advantages:

? 1. Shared Memory
? 2. Standalone upgrade components (plug-in installation, software update)
? 3. Dynamic loading can be displayed

Disadvantages:

? 1. When multiple applications in the system use a dynamic link library, but the required versions are different, the dynamic link libraries interfere with each other.
? 2. Performance overhead. Dynamic link library in order to "share code, but do not share the data", introduced a small cost, call the dynamic link library functions, need several indirect memory access in order to go to the function entrance, global data is also.

Static libraries and dynamic libraries under 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.