Build a test environment Linux static Link Library and dynamic link library difference and dynamic Library Creation

Source: Internet
Author: User

Http://hi.baidu.com/zhengshaohua1987/blog/item/c99262eefe1e04ebb2fb95ee.html

 

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 called static libaray (static libaray), which is usually named in the form 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

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.

5. creation and use of static databases:

1. Generate a static library: Library name libmylib.

Ar RCS libmylib. A mylib. o

2. Copy the static library to the/usr/lib/or/lib/directory.

CP libmylib. A/usr/lib/

3. Use of static databases

For example, the test file is test. C.

Gcc-0 test. C-lmylib

-L is the option, and mylib is the library name. Mylib is the intermediate part of libmylib. in Linux, it is agreed that all libraries start with Lib.

The static library ends with. A, and the dynamic library ends with. So. You do not need to include a prefix or suffix when compiling a program.

Note: The static library name must start with "lib". Otherwise, the compiler cannot find the library.

 

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.
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.

CPP Code
  1. So_test.h:
  2. Void test_a ();
  3. Void test_ B ();
  4. Void test_c ();
  5. Test_a.c:
  6. # Include "so_test.h"
  7. Void test_a ()
  8. {
  9. Printf ("this is in test_a... \ n ");
  10. }
  11. Test_ B .c:
  12. # Include "so_test.h"
  13. Void test_ B ()
  14. {
  15. Printf ("this is in test_ B... \ n ");
  16. }
  17. Test_a.c:
  18. # Include "so_test.h"
  19. Void test_c ()
  20. {
  21. Printf ("this is in test_c... \ n ");
  22. }
so_test.h: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.

CPP Code
  1. Test. C:
  2. # Include "so_test.h"
  3. Int main ()
  4. {
  5. Test_a ();
  6. Test_ B ();
  7. Test_c ();
  8. Return 0;
  9. }
test.c:#include "so_test.h"int main(){    test_a();    test_b();     test_c();    return 0;}

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

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

Test whether to dynamically connect. 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

-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.

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

-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.

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

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.

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.