Differences between dynamic and static libraries

Source: Internet
Author: User

The library is essentially a binary format of executable code that can be loaded into memory for execution. The database is divided into static and dynamic databases.

Differences between static and dynamic Databases

1. Static function library

The class library name is generally libxxx. a. The files compiled using the static function library are relatively large, because all the data in the function library will be integrated into the target code, and its advantages are obvious, that is, the compiled execution program does not need the support of external function libraries, because all the functions used have been compiled. However, this will also become another drawback, because if the static function library changes, your program must be re-compiled.

2. Dynamic function library

The class library name is generally libxxx. so; compared with the static function library, the dynamic function library is not compiled into the target code during compilation, and the corresponding function in the function library is called only when your program executes the relevant function, therefore, the executable files generated by the dynamic function library are relatively small. Because the function library is not integrated into your program, but dynamically applied for and called when the program is running, the corresponding library must be provided in the running environment of the program. Changes to the dynamic function library do not affect your program, so it is easier to upgrade the dynamic function library.
In Linux, there are several important directories for storing the corresponding function libraries, such as/lib/usr/lib.

Static library usage

Static library operating tools: GCC and Ar commands.

Write and use static databases

(1) design library source code pr1.c and pr2.c

[[Email protected] make_lib] # Cat pr1.c

Void print1 ()

{

Printf ("This is the first lib SRC! \ N ");

}

[[Email protected] make_lib] # Cat pr2.c

Void print2 ()

{

Printf ("this is the second SRC Lib! \ N ");

}

(2) Compile the. c file

[[Email protected] make_lib] $ CC-o-c pr1.c pr2.c

[[Email protected] make_lib] $ LS-l Pr *. o

-RW-r -- 1 bill 804 April 15 11:11 pr1.o

-RW-r -- 1 bill 804 April 15 11:11 pr2.o

(3) Link Static Databases

To correctly find the library file in the Compilation Program, the static library must be named according to the Lib [name]. A rule, as shown in the following example: [name] = Pr.

[[Email protected] make_lib] $ ar-CLR libpr. A pr1.o pr2.o

A-pr1.o

A-pr2.o

[[Email protected] make_lib] $ LS-L *.

-RW-r -- 1 bill 1822 April 15 11:12 libpr.

[[Email protected] make_lib] $ ar-T libpr.

Pr1.o

Pr2.o

(4) Call The Library Function Code main. c

[[Email protected] make_lib] $ cat main. c

Int main ()

{

Print1 ();

Print2 ();

Return 0;

}

(5) Compile link options

-L and-l parameters are placed at the end.-l loads the path of the library file, and-l indicates the name of the library file.

[[Email protected] make_lib] $ gcc-O main. C-L./-LPR

[[Email protected] make_lib] $ LS-l main *

-Rwxrwxr-x 1 bill 11805 11:17 main

-RW-r -- 1 bill 50 April 15 11:15 main. c

(6) execute the Target Program

[[Email protected] make_lib] $./main

This is the first lib SRC!

This is the second SRC Lib!

[[Email protected] make_lib] $

Dynamic library usage

Write a dynamic library

(1)Design library code

[[Email protected] make_lib] $ cat pr1.c

Int P = 2;

Void print (){

Printf ("This is the first dll src! \ N ");

}

[[Email protected] make_lib] $

(2)Generate dynamic library

[[Email protected] make_lib] $ gcc-o-FPIC-shared-o dl. So pr1.c

[[Email protected] make_lib] $ LS-L *. So

-Rwxrwxr-x 1 bill 6592 April 15 15:19 DL. So

[[Email protected] make_lib] $

Implicit call of a dynamic library

Specify the location and name of the dynamic library when compiling and calling the library function code. See the following example.

[[Email protected] make_lib] $ cat main. c

Int main ()

{

Print ();

Return 0;

}

[[Email protected] make_lib] $ gcc-O tdl main. C./dl. So

[[Email protected] make_lib] $./tdl

This is the first dll src!

[[Email protected] make_lib] $

When the active name of the dynamic library changes, the program will not be able to run normally. One of the benefits of replacing the static library is to update the library content at any time by updating the dynamic library.

Explicit call of a dynamic library

Explicit calling of the dynamic library requires the support of four functions,Function dlopen to open the dynamic library, function dlsym to obtain the base address of objects in the dynamic library, FunctionNumber of dlerrors to obtain the error information in the explicit dynamic library operation. The function doclose closes the dynamic library.

[[Email protected] make_lib] $ cat main. c

# Include <dlfcn. h>

Int main ()

{

Void * phandle;

Void (* pfunc) (); // pointer to the Function

Int * P;

Phandle = dlopen ("./d1.so", rtld_now); // open the dynamic library

If (! Phandle ){

Printf ("can't find d1.so \ n ");

Exit (1 );

}

Pfunc = (void (*) () dlsym (phandle, "print"); // obtain the library function print address

If (pfunc)

Pfunc ();

Else

Printf ("can't find function Print \ n ");

P = (int *) dlsym (phandle, "P"); // obtain the address of library Variable P

If (P)

Printf ("P = % d \ n", * P );

Else

Printf ("can't find int p \ n ");

Dlclose (phandle); // close the dynamic library

Return 0;

}

[[Email protected] make_lib] $Gcc-o tds main. C-LD1-L.

At this time, you cannot immediately./tDS because when using the dynamic function library, you will find the dynamic function library under the/usr/lib and/lib directories. At this time, the library we generated is not inside. There are several ways to make it run successfully at this time: the most direct and simplest way is to pull libstr_out.so to/usr/lib or/lib. There is also a way to export LD_LIBRARY_PATH = $ (PWD). You can also add the directory of the library we generated in the/etc/lD. So. conf file, and then/sbin/ldconfig. /Etc/lD. so. conf is a very important directory that stores the directories to be checked when the linker and loader search for shared libraries. It is read from/usr/lib by default, to run smoothly, we can also add the directory of our library to this file and execute/sbin/ldconfig. There is also a file to learn about/etc/lD. so. cache, which stores frequently-used dynamic function libraries and loads them into the memory first, because the memory access speed is much higher than the hard disk access speed, this increases the speed of software loading dynamic function libraries.

View library Dependencies

Run the LDD command to check which libraries the execution file depends on.

This command is used to determine the dynamic library contained in an executable binary file.
[[Email protected] root] # LDD [-VD] [filename]
Parameter description:
-- Version: prints the version number of LDD.
-V -- verbose prints all information, such as the version information of the symbol.
-D -- Data-relocs: re-deploy the execution symbol and report the missing target object (only applicable to the ELF format)
-R -- function-relocs re-deploys the target object and function, and reports the missing target object and function (applicable only to the ELF format)
-- Help usage information.

If the given library name in the command line contains '/', the libc5 version of this program uses it as the Library name; otherwise, it searches for the library in the standard location. Run a shared library under the current directory with the prefix "./"

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.