How Linux generates static and dynamic libraries

Source: Internet
Author: User

1. Introduction

1. Libraries under Linux
The difference between static and shared libraries (dynamic libraries) is that the code is loaded at different times.
The code for the static library has been loaded into the executable program during compilation, so the volume is large.
The code for a shared library is loaded into memory when the executable is running, only a simple reference during compilation, so the code is small.

2, the significance of the existence of the library
Libraries are written by someone else's existing, mature, reusable code that you can use but remember to abide by the license agreement.
In reality, every program relies on many underlying libraries, and it is not possible for everyone's code to start from scratch, so the existence of a library is extraordinary.
The benefit of a shared library is that if different applications call the same library, then only one instance of the shared library is needed in memory.

2. Static Library

The suffix of the static library is. A, which is produced in two steps

1.由源文件编译生成一堆.o,每个.o2.ar命令将很多.o转换成.a,成为静态库动态库的后缀是.so,它由gcc加特定参数编译产生。具体方法参见后文实例。

A statically linked file in a gnu/linux system is actually a compressed package of multiple. o files. Suppose we have cool.h cool.c and some.c files to get the static link library LIBCOOL.A. First use the following command to get the corresponding object file COOL.O and SOME.O:

    gcc -c cool.c    gcc -c some.c

The object file generated in this way is called the PDC--the location-dependent code (position-dependence). Then use the following command to get the static link file Libcool.a:

    ar -r libcool.a cool.o some.o    ranlib libcool.a

Static link library LIBCOOL.A complies with Gnu/linux's static link library naming convention and must be "LIBYOUR_LIBRARY_NAME.A"

3. Dynamic Library

Dynamically linking files in Gnu/linux must be generated via the linker ld. Suppose we have hot.c other.c and so on to generate a dynamic-link library libhot.so. First use the following command to get the corresponding object file HOT.O and SOME.O

    -fPIC-c hot.c    -fPIC-c other.c

The parameter-fpic specifies that the generated object file is positional-independent (position-independence code), and only PIC can be used to generate a dynamic-link library. Then use the following command to get the dynamic library:

   ld -Bshared -o libhot.so hot.o other.o

Or you can use the compiler's LD wrapper:

    gcc -shared -o libhot.so hot.o other.o

You can also use the compiler to generate a dynamic library directly:

  gcc -fPIC -shared -o libhot.so hot.c other.c

Here the option-shared indicates that the type of the target file is a dynamic-link library, and the naming convention for the Dynamic library is "libyour_library_name.so"

4. PostScript

Question one: How to know which libraries an executable program depends on

The LDD command can view a shared library that is dependent on an executable program.
For example # ldd/bin/lnlibc.so.6
=/lib/libc.so.6 (0x40021000)/lib/ld-linux.so.2
=/lib/ld-linux.so.2 (0x40000000)
You can see that the LN command relies on the LIBC library and the Ld-linux library

Question two: How to locate the shared library file when executing the executable program
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.
System dynamic Loader is required at this time (Linker/loader)
For the ELF format executable program, is done by ld-linux.so*, it has searched elf file Dt_rpath Segment-environment variable Ld_library_path-/etc/ld.so.cache file list-/lib/,/usr/ The Lib directory is loaded into memory after locating the library file
such as: Export ld_library_path= ' pwd '
Add the current file directory as a shared directory

Question three: How to get the system to find him after installing a new library
If installed under/lib or/usr/lib, then LD can be found by default, no additional action is required.
If you are installing in a different directory, you need to add it to the/etc/ld.so.cache file by following the steps below
1. Edit the/etc/ld.so.conf file and add the path to the directory where the library files are located
2. Run Ldconfig and the command rebuilds the/etc/ld.so.cache file

Recommended Blog Address
Http://www.cppblog.com/deane/archive/2012/08/01/165216.html (http://www.cppblog.com/deane/archive/2012/08/01/165216.html)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How Linux generates static and dynamic libraries

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.