Linux static library and shared library

Source: Internet
Author: User
Linux static library and shared library classification: Linux applications 18: 014371 people read comments (1) collect reports

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 generatedIn Linux
The suffix of the static library is. A, which is generated in two steps.
Step 1. Generate a bunch of. O from source file compilation,Each. O 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.

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.