In-depth discussion of Linux static library and dynamic library (read it) _c language

Source: Internet
Author: User
A library is essentially a binary format for executable code that can be loaded into memory for execution. The library is divided into two kinds, static library and dynamic library.
the difference between a static library and a dynamic library
1. Static function library
The names of such libraries are generally libxxx.a;Compiled using a static function library large file Size--space, because all of the data in the library is integrated into the target code, his advantage is obvious, that is, the compiled execution program does not need external function library support, because all the functions used are already compiled. Of course it will be his fault, because if the static function library changes, then your program must be recompiled.
2. Dynamic Function Library
The names of such libraries are generally libxxx.so, and in contrast to the static function libraries, the dynamic function libraries are compiled and has not been compiledIn the target code, your program executes to the relevant function to call the corresponding function in the library, so the dynamic function library produces the executable file is relatively small。 Because the library is not integrated into your program, but the program is running Dynamic request and call--time, the corresponding library must be provided in the running environment of the program. The change of dynamic function library does not affect your program, so the dynamic function library Upgrade/UpdateMore convenient.

Second, Static library

(a) Brief introduction
/OPT/HISI-LINUX/X86-ARM/GCC-3.4.3-UCLIBC-0.9.28/USR/BIN/ARM-HISMALL-LINUX-GCC \
MAIN.C src/*-i./include-l./lib-lmpi-o Main
/OPT/HISI-LINUX/X86-ARM/GCC-3.4.3-UCLIBC-0.9.28/USR/BIN/ARM-HISMALL-LINUX-GCC for cross-compilation tool chains
\ for line break, the next line and when the behavior of the same row, ' \ ' after no space
MAIN.C main function
src/* as source file
-I rear connector file
-L Follow library file path path
-L followed by the library file name, full name LIBMPI.A
. A is a static library

(ii) Preparation and use of static libraries
(1) Design Library source code pr1.c, pr2.c and MAIN.C
Copy code code as follows:

[Bill@billstone make_lib]$ Cat pr1.c
#include <stdio.h>
void Print1 (void)
{
printf ("This is the" "the");
}
[Bill@billstone make_lib]$ Cat pr2.c
#include <stdio.h>
void Print2 (void)
{
printf ("This is the second src lib!\n");
}
[Bill@billstone make_lib]$ Cat Main.c
int main (void)
{
Print1 ();
Print2 ();
return 0;
}

(2) compiling pr1.c, pr2.c files
Copy Code code as follows:

[Bill@billstone make_lib]$ Gcc-o-C pr1.c pr2.c
[Bill@billstone make_lib]$ ls-l pr*.o
-rw-rw-r--1 Bill 804 April 11:11 PR1.O
-rw-rw-r--1 Bill 804 April 11:11 PR2.O

(3) Link Static library
In order to correctly locate the library file in the compiler, the static library must follow the lib[name].aThe rule name, as in the following example [NAME]=PR.
ar parameter meaning:
R: Inserts a module (replace) in the library. Replaces a module with the same name when the inserted module name already exists in the library.
S: Writes a target file index to the library, or updates an existing target file index.
V: This option is used to display additional information to perform the action options.
T: Displays the library's module table list. Typically, only the module name is displayed.
[Bill@billstone make_lib]$ ar-rsv libpr.a pr1.o pr2.o
a-pr1.o
a-pr2.o
[Bill@billstone make_lib]$ ar-t LIBPR.A
pr1.o
pr2.o
(4) Compile Link Options
The-L and-l parameters are placed in the back. Where,-l loads the library file path,-L indicates the name of the library file.
[Bill@billstone make_lib]$ gcc-o main main.c-l./-LPR//Build Main
(5) Implementation of the target program
[Bill@billstone make_lib]$./main
This is the src!
This is the second SRC lib!

Iii. Dynamic libraries (implicit invocation)
(1) Design library code
Copy Code code as follows:

[Bill@billstone make_lib]$ cat pr1.c
#include <stdio.h>
int p = 2;
void print () {
printf ("%p:%d\n", &p, p);
printf ("This is the the the" "");
}

(2) Generate dynamic library xxx.so
Copy Code code as follows:

[Bill@billstone make_lib]$ gcc-o-fpic-shared-o xxx.so pr1.c
[Bill@billstone make_lib]$ ls-l *.so
-rwxrwxr-x 1 Bill 6592 April 15:19 xxx.so

(3) implicit invocation of dynamic libraries
Copy Code code as follows:

[Bill@billstone make_lib]$ Cat Main.c
int main ()
{
Print ();
return 0;
}
[Bill@billstone make_lib]$ gcc-o main main.c./xxx.so
[Bill@billstone make_lib]$ ./main
0x97b5d4:2
This is the src!

when the location of the dynamic library changes, the program will not function correctly; One of the benefits of dynamic libraries replacing static libraries is to upgrade the contents of the library at any time by updating the dynamic library.

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.