[Linux] generation and calling of static and dynamic libraries

Source: Internet
Author: User

1. Library: a binary form of executable code that can be loaded into memory for execution. The databases are divided into static and dynamic databases.

Ii. Differences between static and dynamic libraries

1) static library in Linux: The name is generally lib ***. A uses the static function library to compile a large file, 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. Of course, this will also become his shortcoming, because if the static function library changes, then your program must be re-compiled.

2) Linux dynamic 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.

Iii. Use of static databases

Operating tools: GCC and Ar commands

1) source code of the design library

Part1.c:

<Strong> void print1 () <br/> {<br/> printf ("This is the first lib SRC \ n "); <br/>}< br/> part2.c <br/> </strong><Strong> void print2 () <br/> {<br/> printf ("this is the secound lib SRC \ n"); <br/>}</strong>

2) Compile the. c file:

Gcc-o-c part1.c part2.c

Generate part1.o and part2.o files

3) link the static library

Ar-CLR libpart. A part1.c part2.c

-R replace existing or insert new file (s) into the archive

-S replace existing or insert new file (s) into the archive

-V replace existing or insert new file (s) into the archive

4) source code of the Call Library

Main. c

<Strong> int main () <br/>{< br/> print1 (); <br/> print2 (); </P> <p> return 0; <br/>}</strong>5) Compile and link the library

Gcc-O main. C-L./-LPart // do not forget-LPart

Explanation:-l the path followed by the library-l *** is after the lib and. A names of the static library are removed from LIB..

./Main // Execute

Output:

This is the first lib SRC!

This is the second SRC Lib!

Iv. Use of dynamic libraries

1) design the library code part. c

<Strong> # include <stdio. h> // Add <br/> int P = 8; <br/> void print1 () <br/>{< br/> printf ("This is the first so SRC \ n"); <br/>}</strong>2) generate a dynamic library

Gcc-o-FPIC-shared-o dl. So pr1.c

-FPIC:-F is followed by some compilation options. PIC is one of them, which indicates generating position independent code)

3) method of calling a function

<Strong> int main () <br/>{< br/> print1 (); <br/> return 0; <br/>}</strong>4) Call Compilation

Gcc-O main./dl. So

Run:./main

Output:

<Strong> This is the first so SRC </strong>Vi. explicit call of a dynamic library

Explicit calling of the dynamic library requires the support of four functions. The function dlopen opens the dynamic library, the function dlsym gets the base address of the objects in the dynamic library, and the function dlerror gets the error information in the explicit dynamic library operation, function doclose closes the dynamic library.

Call code: Main. c

<Strong> # include <dlfcn. h> <br/> # include <stdio. h> <br/> int main () <br/> {<br/> void * phandle; <br/> void (* pfunc )(); // pointer to the function <br/> int * P; <br/> phandle = dlopen (". /dl. so ", rtld_now); // open the dynamic library <br/> If (! Phandle) {<br/> printf ("can't find DL. so \ n "); <br/> // exit (1); <br/>}</P> <p> pfunc = (void (*)()) dlsym (phandle, "print1"); // obtain the library function print address <br/> If (pfunc) <br/> pfunc (); <br/> else <br/> printf ("can't find function Print \ n"); </P> <p> P = (int *) dlsym (phandle, "P"); // get the address of library Variable P </P> <p> If (p) <br/> printf ("P = % d \ n ", * P); <br/> else <br/> printf ("can't find int p \ n"); </P> <p> dlclose (phandle ); // close the dynamic library <br/> return 0; <br/>}</P> <p> </strong>Compilation call: gcc-O main. C-L./-LDL

Premise: you cannot do so now. /Main, because when using the dynamic function library, it will find the dynamic function library under the/usr/lib directory, and the library we generated is not in it. There are several ways to make it run successfully at this time: the most direct and simplest way is to pull DL. 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.

We use: Export LD_LIBRARY_PATH = $ (PWD). You can view it by ECHO $ LD_LIBRARY_PATH.

Run:./main

Output: This is the first so SRC
P = 8

7. 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.
[Root @ test 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)

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.