In-depth discussion of Linux static library and dynamic library

Source: Internet
Author: User

2. Generate a dynamic library and use

A dynamic library (. So) file can be generated at compile time under Linux using the-shared parameter, as follows

A library is essentially a binary format of executable code that can be loaded into memory for execution. There are two kinds of library, 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; the file compiled by the static function library is larger-space , because all the data of the library is integrated into the target code, and his merits are obvious. That is, the compiled execution program does not require external library support because all the functions used are already compiled. This, of course, will also be a disadvantage, because if the static library changes, then your program must be recompiled.
2. Dynamic Function Library
The name of such a library is generally libxxx.so; relative to the static function library, the dynamic function library is not compiled into the target code when it is compiled, and your program executes to the relevant function to call the corresponding function in the library, so the dynamic function library produces less executables . Since the library is not integrated into your program, it is a dynamic application and call-time for the program to run, so you must provide the appropriate library in the running environment of the program. Dynamic function library changes do not affect your program, so the dynamic function library upgrade/update is more convenient.

Second, Static library

(a) Brief introduction

GCC  main.c src/*  -i./include-l./lib-lmpi-o main

MAIN.C main function
src/* as source file
-I back header file
-L follow-up to 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

The code is as follows:
[email protected] make_lib]$ cat pr1.c #include<stdio.h>voidPrint1 (void) {printf ("This is the first Lib src!\n"); } [[email protected] make_lib]$ cat pr2.c #include<stdio.h>voidPrint2 (void) {printf ("This is the second src lib!\n"); } [[email protected] make_lib]$ cat main.cintMainvoid) {print1 ();                 Print2 (); return 0; } 


(2) Compiling pr1.c, pr2.c files

The code is as follows:
[Email protected] make_lib]$ Gcc-o-l pr*-rw-rw-r--        1 Bill          Bill                    804     4 month  :-rw-rw-r--        1 Bill                    804    4 month  : One


(3) Link Static library
In order to correctly locate the library file in the compiler, the static library must be named according to the lib[name].a rules, as in the following example [NAME]=PR.
Ar parameter meaning:
R: Insert the module (replace) in the library. When the inserted module name already exists in the library, replace the module with the same name.
S: Writes a target file index into the library, or updates an existing target file index.
V: This option displays additional information for the option to perform the operation.
T: Displays a list of module tables for the library. Typically only the module name is displayed.

[email protected] make_lib]$ ar----


(4) Compile Link Options
The-L and-l parameters are placed behind. Where,-l loads the library file path,-l indicates the library file name.

[Email protected] make_lib]$ gcc-o main main.c-l./-lpr     //


(5) Execution of target procedures

[Email protected] make_lib]$./ is the first Lib src!   is the second src lib!


Third, dynamic library (implicit invocation)
(1) Design library code

The code is as follows:
<stdio.h>int2void  print () {                 printf ("%p:%d\n  ", &P, p);                printf ("This is the firstDLL src!\n"


(2) Generate dynamic library xxx.so

The code is as follows:
[Email protected] make_lib]$ gcc-o-fpic-shared-L *-rwxrwxr-x        1 Bill          Bill                  c6>6592    4 months  :


(3) Implicit invocation of dynamic libraries

The code is as follows:
int Main () {        print ();         return 0  -o main main.c./xxx.so [[email protected] make_lib]$. /0x97b5d4:2 This is the first Lib src!


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

Example:

The first thing to do is to prepare the header file for the function that we need to encapsulate into a library file and write the source file as follows:

1 // myAPI.h   2 int ADD (intint  b);   3 int Minus (intint b);
1 //MyAPI.cpp2#include"myAPI.h"  3   4 intADD (intAintb) {  5     returnA +b; 6 }  7   8 intMinus (intAintb) {  9     returnAb; Ten}

Next, prepare a main function source file for the test:

1 //main.cpp2#include"myAPI.h"  3#include <iostream>4   5 intMain () {6Std::cout <<"1 + 1 ="<< ADD (1,1) <<Std::endl; 7Std::cout <<"1-1 ="<< Minus (1,1) <<Std::endl; 8     return 0; 9}

Finally, compile our MyAPI.cpp file to generate the MYAPI.O target file

1. Generate a static library and use

Linux under the command to generate static Libraries AR processing MYAPI.O file generated static library files, the resulting library file should follow the specification, and Linux under the library file Plus "Lib" prefix. So for this example, a LIBMYAPI.A static library can be generated with one of the following commands:

AR crv LIBMYAPI.A myapi.o

The next step is to take advantage of the static library during the project compilation process, when the definition file for the library function is myAPI.cpp. The main.cpp compile command is as follows (note that the dependent static library file is placed after the dependency):

g++ main.cpp libmyapi.a-o Output

The executable output can be run after compiling, and LIBMYAPI.A is not needed at this time. Execute the command and output the result as follows:

2. Generate a dynamic library and use

A dynamic library (. So) file can be generated at compile time under Linux using the-shared parameter, as follows

g++-shared-fpic-o libmyapi.so MyAPI.cpp  

The generated dynamic library needs to be declared at compile time, and the runtime needs to be relied upon. Declare as follows

g++ main.cpp-l.-O output./libmyapi.so

"-L." The tag tells the g++ that the library might be in the current directory, and uses the "-LMYAPI" tag to tell the g++ driver to reference the shared function library libmyapi.so during the connection phase.

If you are prompted with the following error, move libmyapi.so to the/usr/lib directory:

./output

Transferred from: http://blog.csdn.net/u013256622/article/details/51811072

Http://www.jb51.net/article/37409.htm

In-depth discussion of Linux static libraries and dynamic libraries (GO)

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.