Comparison of dynamic function library and static function library in Linux

Source: Internet
Author: User

The library function not only improves the utilization of the code, but also shields the details of the implementation of the function, and provides a unified interface for different developers. From the perspective of implementation, library functions can be divided into dynamic function libraries and static function libraries. The same set of functions that can be encapsulated into static and dynamic libraries as needed. So what's the difference between generating a static library and a dynamic library? What are the requirements for static and dynamic libraries to implement functions? What are the effects of both on memory? Here's a discussion with these questions.

the difference between static and dynamic library generation methods
To simplify, the following is illustrated by the implementation of a library with only one function. The code for the library function is demo.c as follows:
/*******************************************************************/
int cal (int a, int b)
{
Return (a^ (a << B)) + (b << a);
}

The code in MAIN.C is as follows:
[email protected] mylib]# cat MAIN.C
#include <stdio.h>
#include "demo.h"

int main (void)
{
printf ("Call cal (7,8) =%08x\n", Cal (7,8));
}
[email protected] mylib]# cat demo.h
int cal (int, int);
If you need to generate a dynamic library, you need to use the following makefile:
Mylib: = libdemo.so
BIN: = Test
Cflag: =-wall-i. /-i./-dtest=1
Libflag: =-l./-rpath=/home/xqch/workspace/mylib./-ldemo

All: $ (mylib) $ (BIN)

$ (BIN): MAIN.O
GCC $ (cflag)-l/home/xqch/workspace/mylib-ldemo-o [email protected] $^

$ (mylib): demo.o
Ld-shared-fpic-rpath=/home/xqch/workspace/mylib-o [email protected] $^

%.o:%.c
GCC $ (cflag)-c-o [email protected] $<

. Phony:install Clean

Install
CP $ (Mylib)/usr/lib
CP $ (Mylib)/lib

Clean
RM-RF *.O $ (BIN) $ (mylib)
The process of generating a static library is as follows:
[email protected] mylib]$ cat Makefile
Mylib: = libdemo.a
BIN: = Main
Cflag: =-wall-i. /-i./-dtest

All: $ (mylib) $ (BIN)

$ (BIN): MAIN.O
GCC $ (cflag)-o [email protected] $^ $ (mylib)

$ (mylib): demo.o
AR-RCV [email protected] $^

%.o:%.c
GCC $ (cflag)-c-o [email protected] $<


. Phony:clean
Clean
RM-RF *.O $ (BIN) $ (mylib)

the difference between the binary code generated by the static library and the dynamic library
Based on the above two makefile we can compare their differences:
[Email protected] mylib]$ LS-ALRT main
-rwxrwxr-x. 1 xqch xqch 7696 Oct 21:21 Main
Dynamic
[Email protected] mylib]$ LS-ALRT test
-rwxrwxr-x. 1 xqch xqch 7602 Oct 19:32 test
You can see that the executable file generated by the static library is larger than the executable file that needs to be linked to the dynamic library. By disassembling, we can see that the difference is in the way that the library functions are called differently:
Static
08048724 <cal>:
8048724:55 Push%EBP
8048725:89 e5 mov%esp,%ebp
8048727:53 Push%EBX
8048728:83 EC $0x4,%esp Sub
804872B:A1 A0 mov 0x804a030,%eax
8048730:83 C0 Add $0x1,%eax
8048733:A3 A0 mov%eax,0x804a030
8048738:A1 3c A0 mov 0x804a03c,%eax
804873d:83 C0 Add $0x1,%eax
8048740:A3 3c A0 mov%eax,0x804a03c
8048745:A1 A0 mov 0x804a038,%eax
804874a:83 C0 Add $0x1,%eax
804874D:A3 A0 mov%eax,0x804a038
8048752:8b 0d A0 mov 0x804a038,%ecx

Dynamic
080485a0 <[email Protected]>:
80485a0:ff 2c A0 JMP *0x804a02c
80485a6:68-XX-$0x40
80485AB:E9 FF FF FF JMP 8048510 <_init+0x2c>

requirements for static libraries and dynamic libraries for code
The common denominator is that if you need support for multithreaded code, you can't have global variables and dynamic variables and static variables (inside and outside of the function) in your code. Because they end up in a. BSS or. Data segment, and the same data segment is shared between different threads of the process, and a thread calls to use those variable functions, the subsequent calling function sees the value being changed.

The impact of static and dynamic libraries on memory
Static programs occupy a lot of memory, dynamic program on the contrary. Because the dynamic program is connected at execution time, add the address of the called dynamic function to the. GOT.PLT (Process link table).

This article is from the "Store Chef" blog, so be sure to keep this source http://xiamachao.blog.51cto.com/10580956/1712108

Comparison of dynamic function library and static function 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.