Linux static links and dynamic links

Source: Internet
Author: User

    • Link

The generation of an executable target file needs to be precompiled (g++-E), compiled (g++-s), compiled (g++-c), and linked four steps. A link is the process of merging multiple relocatable target files into a single destination file. The link is done by the linker and the linker is going to complete two tasks:

1, the symbol (symbol table by the assembler constructs) parses. That is, associating symbolic references with symbolic definitions. where symbol definitions and symbol references may not be in the same destination file. and the linker has some rules for parsing global symbols for multiple definitions: it is not allowed to have more than one strong symbol with the same name (functions and initialized global variables), if there is a strong sign and multiple weak symbols with the same name, select a strong symbol, if there are multiple weak symbols randomly selected one.

2, reposition. This includes merging the corresponding sections of different relocatable target files into a single section, and repositioning symbol references so that references to symbols point to the correct run-time addresses.

    • Static links

1. Generate a static library

# g++-C a.cc b.cc

# g++ ar RCS LIBAB.A a.o b.o

# g++-static-o prog main.o./LIBAB.A

With this form, multiple target modules can be packaged into a single file, called a static library. When linking to a static library, only the target module referenced by the program is copied, but it is important to note that the order in which the files appear on the command line must be correct when the link forms the final executable file. That is, the symbol definition must appear after the symbol reference. So the library is generally placed at the end of the command line.

Static linking has the following disadvantages:

Multiple processes refer to the same target module, there will be multiple copies in memory, wasting memory

If the static library has an update, you need to relink.

    • Dynamic links

Dynamic links are divided into load-time links and run-time links.

At load time, the linker replicates some relocation and symbol table information during the link phase, while the text and data of the dynamic library are relocated to a memory segment at load time, when multiple processes use the same dynamic library, the memory-mapped method is used to share code and data, and only one copy is in memory.

A run-time link is a way to invoke a system function by displaying it.

Run-time Links

Linux static links and dynamic links

Related Article

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.