Analyze static and dynamic libraries in Linux

Source: Internet
Author: User

1. What is a database?
A large number of libraries exist on windows and linux platforms.
Essentially, a library is a binary form of executable code that can be loaded into the memory for execution by the operating system.
Because windows and linux are essentially different, their binary libraries are incompatible.
This article only introduces libraries in linux.

2. Database types
There are two types of libraries in linux: static library and shared library (dynamic library ).
The difference between the two lies in that the code is loaded at different times.
The code of the static library has been loaded into the executable program during compilation, so the size is large.
The code of the shared library is loaded into the memory only when the executable program runs. It is only referenced in the compilation process, so the code size is small.

3. Significance of inventory
Libraries are existing, mature, reusable code written by others. You can use them, but remember to abide by the license agreement.
In reality, every program depends on many underlying libraries. It is impossible for everyone to start from scratch. Therefore, the existence of libraries is of extraordinary significance.
The benefit of the shared library is that if different applications call the same library, there is only one instance of the shared library in the memory.

4. How library files are generated in linux
The suffix of the static library is. a, which is generated in two steps.
Step 1. A bunch of. o files are generated by source file compilation. Each. o file contains the symbol table of this compilation unit.
Step 2. the ar command converts many. o files into. a static library.
The suffix of the dynamic library is. so, which is generated by gcc and specific parameter compilation.
For example:
$ Gcc-fPIC-c *. c $ gcc-shared-Wl,-soname, libfoo. so.1-o libfoo. so.1.0 *.

5. How are database files named? Are there any specifications?
In linux, the library files are generally stored in/usr/lib,
The static library name is generally libxxxx. a, where xxxx is the lib name
The name of the dynamic library is generally libxxxx. so. major. minor, xxxx is the lib name, major is the main version number, and minor is the minor version number.

6. How to know which libraries an executable program depends on
The ldd command allows you to view a shared library that executable programs depend on,
For example, # ldd/bin/lnlibc. so.6
=>/Lib/libc. so.6 (0 × 40021000)/lib/ld-linux.so.2
=>/Lib/ld-linux. so.2 (0 × 40000000)
The ln command depends on the libc library and ld-linux library.

7. How to locate shared library files during execution of executable programs
When the system loads executable code, it can know the name of the library it depends on, but it also needs to know the absolute path
In this case, the system dynamic loader (dynamic linker/loader) is required)
The executable program in the elf format is completed by the ld-linux.so *, which successively searches the DT_RPATH segment of the elf File-environment variable LD_LIBRARY_PATH-/etc/ld. so. cache file list-/lib/,/usr/lib directory find the library file and load it into the memory

8. How can the system find a new library?
If it is installed in/lib or/usr/lib, the ld can be found by default without any other operations.
If it is installed in another directory, add it to the/etc/ld. so. cache file. follow these steps:
1. Edit the/etc/ld. so. conf file and add the path to the directory where the file is stored.
2. Run ldconfig. This command will recreate the/etc/ld. so. cache file.

We usually make some common functions into function libraries for use by other programs. Function libraries can be divided into static libraries and dynamic libraries. The static library will be connected to the target code during program compilation. This static library is no longer needed when the program is running. The dynamic library is not connected to the target code during program compilation, but is loaded only when the program runs. Therefore, dynamic inventory is required when the program runs. This document provides examples to illustrate how to create static and dynamic libraries in Linux and how to use them. Before creating a function library, we first prepare the source program for example and compile the source program of the function library into a. o file.

Step 2: edit the program named hello. h, hello. c, and main. c;

Hello. h (see program 1) is the header file of the function library.

Hello. c (see Program 2) is the source program of the function library, which contains the public function hello, which will output "Hello XXX!" on the screen! ".

Main. c (see Program 3) is the main program of the Test Library file, and the public function hello is called in the main program.

Program 1: hello. h

# Ifndef HELLO_H
# Define HELLO_H

Void hello (const char * name );

# Endif // HELLO_H

Program 2: hello. c

# Include <stdio. h>

Void hello (const char * name)
{
Printf ("Hello % s! \ N ", name );
}

Program 3: main. c
# Include "hello. h"

Int main ()
{
Hello ("everyone ");
Return 0;
}

Step 2: Compile hello. c into a. o file;

Both static and dynamic libraries are created by the. o file. Therefore, we must first compile the source program hello. c into a. o file through gcc.

Enter the following command at the system prompt to get the hello. o file.

# Gcc-c hello. c

#

(Note 1: This article does not introduce the usage of each command and Its Parameter functions. If you want to learn more about them, see other documents .)

(Note 2: the first character "#" is a system prompt and does not need to be typed. The following is the same .)

Run the ls command to check whether the hello. o file exists.

# Ls

Hello. c hello. h hello. o main. c

#

(Note 3: the first character "#" is the system running result, which is the same as the following .)

  • Four pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • Next Page

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.