Differences between Linux static library and dynamic library and creation of dynamic library

Source: Internet
Author: User
Differences between Linux static library and dynamic library and creation (transfer) of dynamic library)
I. Introduction

Generally, the link to the function library is completed during the compilation period (compile time. All related object files and libraries are linked to form an executable file ). When the program is running, it has nothing to do with the function library, because all the required functions have been copied to their own doors. Therefore, these function libraries are
Libaray), usually in the format of "libxxx..

In fact, we can also postpone the loading of some library function links to the runtime ). This is the dynamic link library technology.

Ii. features and advantages of Dynamic Link Library

First, let's take a look at the benefits of postponing the library function to load during the running of the program:

1. resources can be shared between processes.

What is the concept? That is to say, when a program is running to call a Dynamic Linked Library function, the operating system first checks all the running programs, check whether the library function has been copied in the memory. If there is, share the copy; only no links are loaded. Although this mode brings some additional overhead of "Dynamic Links", it greatly saves the system's memory resources. C's standard library is a dynamic link library, that is, all running programs in the system share the code segment of the same C standard library.

2. upgrade some programs easily. You only need to upgrade the dynamic link library, instead of re-compiling the link to other original code to complete the upgrade of the entire program. Windows is a good example.

3. You can even sit down to the link and load it completely controlled by the programmer in the program code.

When writing a program, the programmer can specify when or when to load the dynamic link library function. You can have a relatively large piece of software, but each time you run it, only a small part of the program is loaded into the memory due to different operation requirements. All functions are based on the principle of "calling only when needed", which greatly saves system resources. For example, the current software can usually open several different types of files, and these read/write operations are usually implemented using dynamic link libraries. In a single run, only one type of file will be opened. So until the program knows the file type, it loads the corresponding Read and Write Functions, instead of loading all the Read and Write functions at the beginning, and then finds that they are not used in the whole program.

3. Create a dynamic link library

Due to the sharing feature of Dynamic Linked Library functions, they are not copied to executable files. During compilation, the compiler only performs checks such as function names. When the program is running, the called Dynamic Linked Library Function is placed somewhere in the memory, and all programs that call it will point to this code segment. Therefore, these codes must use relative addresses instead of absolute addresses. During compilation, we need to tell the compiler that these object files are used for dynamic link libraries, so we need to use position independent code (PIC )).

For the GCC compiler, you only need to add the-FPIC label, such:

Gcc-FPIC-C file1.c
Gcc-FPIC-C file2.c
Gcc-shared libxxx. So file1.o file2.o

Notice the last line. The-shared tag tells the compiler that this is to create a dynamic link library. This is very different from the creation of static link libraries. The latter uses AR commands. It is also noted that the name of the dynamic link library is in the form of "libxxx. So" and the suffix is ". So"

Iv. Use of Dynamic Link Library

When using a dynamic link library, you first need to let the compiler check some syntax and definitions during compilation.

This is basically the same as the practical use of static libraries. The-lpath and-LXXX labels are used. For example:

GCC file1.o file2.o-lpath-LXXX-O program.exe

The compiler first searches for the libxxx. So file in the path folder. If the file is not found, it searches for libxxx. A (static library ).

During the program running, you also need to tell the system where to find your dynamic link library file. In UNIX, the environment variable LD_LIBRARY_PATH is defined. You only need to assign the PATH value to this variable. CSH command:

Setenv LD_LIBRARY_PATH your/full/path/to/DLL

After everything is properly arranged, you can run the LDD command to check whether the connection is normal.

LDD program.exe

Dynamic Link Library *. So compilation and use --

Dynamic library *. so is often encountered in C and C ++ programming in Linux. Recently, I have found several articles on the website to introduce the compilation and link of dynamic libraries, after finally understanding this, I have never been quite familiar with it. Here, I will take a note and provide some help to other brothers who are worried about the dynamic Library Link Library.
1. Dynamic library Compilation

The following example shows how to generate a dynamic library. Here is a head file: so_test.h, three. c files: test_a.c, test_ B .c, and test_c.c. We compile these files into a dynamic library: libtest. So.

So_test.h:

# Include
# Include

Void test_a ();
Void test_ B ();
Void test_c ();

Test_a.c:

# Include "so_test.h"
Void test_a ()
{
Printf ("this is in test_a... \ n ");
}

Test_ B .c:
# Include "so_test.h"
Void test_ B ()
{
Printf ("this is in test_ B... \ n ");
}

Test_a.c:

# Include "so_test.h"
Void test_c ()
{
Printf ("this is in test_c... \ n ");
}

Compile these files into a dynamic library: libtest. So
$ GCC test_a.c test_ B .c test_c.c-FPIC-shared-O libtest. So

2. Dynamic library links

In 1, we have successfully generated a dynamic link library libtest. So. Next we use a program to call the functions in this library. The source file of the program is test. C.

Test. C:

# Include "so_test.h"
Int main ()
{
Test_a ();
Test_ B ();
Test_c ();
Return 0;

}

L link test. C to the dynamic library libtest. So to generate the execution file test:

$ GCC test. C-L.-ltest-o Test

L test whether the connection is dynamic. If libtest. So is listed, the connection is normal.

$ LDD Test

L execute test and you can see how it calls functions in the dynamic library.
3. Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file

L-FPIC: indicates the code compiled into a location independent, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.

L-L.: indicates that the database to be connected is in the current directory.

L-ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.

L LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.

L of course, if you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
4. Note

When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.

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.