Static Library and Dynamic Library series (1)

Source: Internet
Author: User

Recently a buddy asked me some questions about their company's code, he was getting a DLL from another company to the hospital, and then calling the DLL's interface to continue writing code for the hospital. The problem is, I can't!  So take this opportunity to do a good job of Lib and DLL. Try to understand some of the problems with Lib and DLLs under Windows. Come at 1.1 o ' know.

What is Lib and what is a DLL. Why Lib and DLL generation

Whether it is a static library or a dynamic library, both to share program code, the analogy is that you encapsulate a piece of code into a function, and other functions can call your encapsulated function without having to copy and paste a copy. This is called shared program code

The popular idea is to package the target files of some common functions together and provide the interface for the corresponding functions for the programmer to call. The programmer only needs to know how the interface is defined.

Note that both the dynamic library and the static library are not required when you call the header file. But there is something like a header file that lets you know the function information inside the library, the return value function name parameter, and so on. If you do not provide a header file, you must provide a library function description document, or a tool to help you see the function information inside the library. Only then can you call these functions in your code. In other words, sometimes people just give you one. So or. A or. dll or. lib You can do your job accordingly.

We know that from the middle of the program you wrote to the build executable, the F7 command was executed under VS, the Makefile was executed under Linux, or a G++/GCC xxx-o was written yourself yyy

So this process includes: C source program header file----pre-compile processing (CPP)----the compiler itself-----optimizer----the assembler----The executable file

1. Compile pre-processing : The precompiled program is basically the replacement of the source program. After this substitution, generate a no macro definition. No conditional compilation directives, no special symbols for output files.

(A.) Macro Definition Directives :

#define NUMBER INT---note no semicolon #undef number---> Cancel the substitution of the defined int

  (b.) Conditional compilation Directives :

#ifdef, #ifndef, #else, #elif, #endif---> Decide which code the compiler will handle by defining different macros. The precompiled program will filter out unnecessary code according to the relevant files.

(c.) Header file contains Directives :

#include <>--> library header files in the/usr/include directory

  (d.) Special characters :

The precompiled program can recognize some special symbols. For example, the line ID in the source program will be interpreted as the current row number. File is interpreted as the name of the current compiled C source program

2. Compiling

The job of precompiling the program at this stage is to translate it into equivalent intermediate code or assembly code by lexical analysis and parsing, after confirming that all instructions conform to the grammatical rules

3. Optimization

Optimizing processing is a difficult technique in a compiled system. It involves two aspects: the compilation technology itself (software optimization) \ Machine hardware environment (hardware-related optimizations)

Software optimization: Delete common expressions, loop optimization (out-of-code, strength weakening, transform loop control conditions, known amounts of merging), replication propagation, deletion of useless assignments

Hardware-related optimizations: How to make full use of the various variables stored in the machine's various hardware registers to reduce the number of accesses to memory. According to the characteristics of the machine hardware pointing instructions (such as pipelining, RISC,CISC,VLIW)

The instructions make some adjustments so that the target code is shorter and the execution is relatively efficient.

The optimized assembly code must be translated into the appropriate machine instructions by the assembler assembly, which may be executed by the machine

4. Compilation

Assembly process time is the process of translating assembly language code into target machine instructions . The machine instruction file is the machine language code also called the target file . In general, the target file consists of segments. There are usually at least two segments on the target file:

  Code Snippet: This paragraph contains mainly the instructions of the program. The paragraph is generally readable and executable, but is generally not writable

Data segment: a variety of global variables or static data to be used in the main storage program. The general data is readable and writable.

There are three main types of target files in the UNIX environment:

  A. relocatable file : Contains code and data for linking to other target files to create an executable or shared target file

  B. Shared target files : This file holds code and data that are appropriate for linking in both contexts: the first is that the linker can process it with other relocatable files and shared files to create another target file; The second is that the dynamic linker combines it with another executable file and other shared target files to create a process image

  c. executable file : It contains a file that can be executed by the OS to create a process . The assembler generates a target file that is actually the first type (relocatable file). For the latter two other processes are also needed to get the This is the work of the link program.

5. Links

The target file generated by the assembler is not immediately executed, and there may be many unresolved issues: for example, a function inside a a.cpp refers to a symbol (such as a variable or function) defined inside a b.cpp; For example, a function that calls a library file in a program. These problems need to be handled by the linker. And the way to do this is to connect the associated target file ( For example, the target file of the a.cpp above and the target file of the b.cpp. or a.cpp target file and the library's target file. In other words, the symbol referenced in one file is connected to the definition of the symbol in another file, Make all of these target files a unified whole that can be loaded into the operating system.

Depending on how the developer assigns the same library functions , link processing can be divided into two types:

    1. Static link

In this way, the code of the function is copied from the static link library in the location of the function to the final executable program . The process is that the code of the function is loaded into the executable process (which is also the process of the function code) when it is executed. Executable program and function code in a process) in the virtual address space (that is, copied into the executable program). A static link library is actually a collection of target files , each of which contains code for one or a set of related functions in the library .

    2. Dynamic linking

In this way, the code of the function is placed in a place called a dynamic link library or in a target file of a shared object . At this point the linker does its work in the final The name of the shared object and other minor and mandatory registration information are recorded in the executable program. The entire contents of the dynamic-link library are mapped to the virtual address space of the corresponding process at run time when it is executed. The dynamic link library will Find the appropriate function code based on the information that is logged in the executable program.

For function calls in executables, dynamic linking and static linking are all possible . Using dynamic links can make the final executable file shorter and save some memory if the shared object is used by more than one process (only one copy of the code for this shared object is stored in memory), because the function code is not put into the executable. Instead, it maps to a virtual address space where the executable file accesses the function code based on the clues it holds about the code of the function to the virtual address. But this This is not to say that dynamic links are better than static links , because in some cases the performance of static links is higher

6. executable file.

Article Learning Source http://blog.csdn.net/zhongguoren666/article/details/6672988

Lib/.a is a static library that is fully copied to the executable at the time of the link, with multiple redundant copies being used multiple times.

DLL/.so is a dynamic library, link time is not copied, the program is run by the system dynamically loaded into memory for the program to call. The system only loads once, so there is only one copy of the code in the memory, and then the code is shared by multiple programs that use it.


Dynamic Library

Static Library

In the program compile time will not be linked to the target code, but is loaded when the program runs, you need a dynamic library when the program is running

is linked to the target code when the program is compiled,

Static libraries are no longer required when the program runs

Next, learn how to build static and dynamic libraries under Linux

Static Library and Dynamic Library series (1)

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.