Static library and dynamic library in Linux

Source: Internet
Author: User
Tags mul

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 the 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#include "add.h" intAddintXintY) {returnX+y;}//add.h #ifndef __add_h__#define __ADD_H__intAddintXinty);#endif//__add_h__//SUB.C#include "sub.h"intSubintXintY) {returnX-y;}//sub.h#ifndef __sub_h__#define __SUB_H__intSubintXinty);#endif//__sub_h__//mul.c#include "mul.h"intMulintXintY) {returnX*y;}//mul.h#ifndef __mul_h__#define __MUL_H__intMulintXinty);#endif//__mul_h__//dev.c#include "dev.h"intDevintXintY) {returnx/y;}//dev.h#ifndef __dev_h__#define __DEV_H__intDevintXinty);#endif//__dev_h__//main.c#include <stdio.h>#include "add.h"#include "sub.h"#include "mul.h"#include "dev.h"intMain () {intb; 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;}//compile source files[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 *.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 MAIN.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[[emai L Protected] ku]#//Generate a static library[Email protected] ku]# AR-RC LIBMYCAL.A *.o[[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]#//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    683Apr -  -: $ 2018add.orw-r--r--0/0    683Apr -  -: $ 2018dev.orw-r--r--0/0    679Apr -  -: $ 2018mul.orw-r--r--0/0    687Apr -  -: $ 2018Sub.o[[email protected] ku]#//link static library build executable file[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 protected] ku]# lsadd.c add.h add.o a.out dev.c dev.h de V.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.out8 38+3= One8-3=58*3= -8/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:

? 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

[Root@localhost ku]#LSADD.C add.h dev.c dev.h main.c mul.c mul.h sub.c sub.h[root@localhost ku]#Gcc-c-fpic *.c///-fpic is compiled into location-independent code, without this option, the compiled code is location-dependent, so dynamic loading is a way of copying code to meet the needs of different processes, rather than achieving the purpose of true code snippet sharing. [Root@localhost 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[root@localhost ku]#RM main.o-rf[root@localhost 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[root@localhost ku]#Gcc-shared-o libmycal.so *.o[root@localhost 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[root@localhost ku]#GCC main.c-l.-lmycal//-l. Represents the library to be connected in the current directory[Root@localhost ku]#LSADD.C add.h add.o a.out dev.c dev.h dev.o libmycal.so main.c mul.c mul.h mul.o sub.c sub.h sub.o[root@localhost ku]#./a.out./a.out:error whileLoading shared Libraries:libmycal.so:cannot Open Shared object file:no such fileorDirectory[root@localhost ku]#CP libmycal.so/lib///Move the dynamic library to the system library file[Root@localhost ku]#LSADD.C add.h add.o a.out dev.c dev.h dev.o libmycal.so main.c mul.c mul.h mul.o sub.c sub.h sub.o//Run results[Root@localhost ku]#./a.out8 68+6= -8-6=28*6= -8/6=1///second method, change the library path[Root@localhost ku]#./main./main:error whileLoading shared Libraries:libmycal.so:cannot Open Shared object file:no such fileorDirectory[root@localhost ku]#Vim/etc/ld.so.conf.d/mycal.conf//write the path to the library in the created file//Flush the buffer after writing the path.[Root@localhost ku]#Ldconfig//Run results[Root@localhost ku]#./main8 68+6= -8-6=28*6= -8/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 library and dynamic library in 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.