Static and dynamic libraries in the Linux learning Note 7--linux

Source: Internet
Author: User
Tags naming convention

First, the compilation of the Static library

The compile process for the static library is as follows:

1. Compile into the target file

Here is an optional-static, called format: gcc-c-static code file name. C

2. Archive into a static library

A, archived tools are AR tools, using Ar-r can archive files into a static library, call format: ar-r static library files are archived files

For example: We have two C files, test1.c and test2.c, respectively.

First we compile it into the target file: Gcc-c-static test1.c

Gcc-c-static test2.c

Two files are generated, respectively, TEST1.O and TEST2.O

Then we archive them: Ar-r libdemo1.a test1.o test2.o, at this time the static library file is demo1.a. Note that Demo1 is a library name, preceded by Lib, is a naming convention.

B, use the NM tool to view the existing function symbol table, using the format: NM static library or dynamic library or target file or execute file

3. Use Static Library

Use format: GCC code file name. C L Static Library name-L Library Directory

For example: Here is a main.c file and the above libdemo1.a Static library, now we want to compile it, then:

GCC main.c-omain-ldemo1-l.

      

4. Specifications and conventions of static libraries

Library naming rules: Lib Library name. A//here refers to the library name directly behind Lib, as in the example above libdemo1.a

Library usage Rules:-L Library name//here refers to the direct addition of the library name after-l

The directory where the-l library is located//here refers to the directory where the library is located directly after-L, as in the current directory, or-L.

5. Use library to manage code advantages: Easy to organize code, reuse, protect code Copyright

6, Static library static meaning: the compiled program runs without relying on the library, the library as part of the program to compile the connection.

7, Static Library essence: is the target file collection (archive)

II. Compilation of dynamic libraries (called shared libraries in Linux)

1. The difference between a dynamic library and a static library

A, the dynamic library can be executed, the static library cannot execute.

B, the dynamic library is not part of the program, while the static library is copied to the program during the link phase of the program

2, the compilation of dynamic library

Use-c-fpic, where-fpic is optional

3, the Dynamic library connection

Using-shared

For example: There are two C files, respectively test3.c,test4.c

Compile them separately: gcc-c-fpic test3.c

Gcc-c-fpic test4.c

Generate TEST3.O and TEST4.O, then connect to them: gcc-shared-olibdemo2.so test3.o test4.o

4. Using Dynamic Library

Use format: GCC code-L Library name-L Dynamic Library location path

For example: There is a code file main2.c, to use the generated dynamic library Demo2, then:

GCC main2.c-ldemo2-l.-omain

5. Naming rules for dynamic libraries

LIB Library name. So

6, Dynamic Library loading

A. Finding A Dynamic Library
B. Loading dynamic libraries into memory
C. Mapping to the user's memory space

7. System-to-dynamic library lookup rules

A, find in/lib
B. Find in/usr/lib
C, find in the path specified by the environment variable Ld_library_path

8, the use of dynamic library tools

LDD See the dynamic library that the program needs to call, of course, LDD can only view the executable file

Readelf-h View the executor header.

Static and dynamic libraries in the Linux learning Note 7--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.