Create and use dynamic libraries in Linux

Source: Internet
Author: User
Dynamic library generation and use in Linux

I. Basic concepts of dynamic libraries

1. The dynamic link library is the library loaded when the program runs. After the dynamic link library is correctly installed, all programs can use the dynamic library to run the program. The dynamic link library is a collection of target files, and the organization of the target files in the dynamic link library is formed in a special way. The addresses of functions and variables in the library are relative addresses, not absolute addresses. The actual addresses are formed when the dynamic library program is called for loading.

2. The name of the Dynamic Link Library includes the alias (soname), real name (realname), and link name (linker name ). An alias consists of a prefix Lib, a library name, and a suffix ". So. The real name is the real name of the dynamic link library. Generally, a minor version number is added to the alias base and the version is released. In addition, there is a link name, that is, the name of the library used for program Link.

3. During dynamic link library installation, files are always copied to a directory, and an alias is generated using a soft connection. When the library file is updated, only soft links are updated.

Ii. generate and use a dynamic library

1. Read an instance to learn how to generate a static database and use a static database.

Create a program file

1> the test folder contains three files: Main. C, Add. C, sub. C, and tiger. h.


2> content in the main. c file:

# Include <stdio. h>

# Include "tiger. h"

 

Int main (void)

{

Printf ("sum = % d \ n", add (5, 3 ));

Printf ("sub = % d \ n", sub (5, 3 ));

 

Return 0;

}

3>. Content in the tiger. h file:

# Ifndef _ tiger __

# DEFINE _ tiger __

 

Int add (int A, int B );

Int sub (int A, int B );

# Endif

4>. Add. c file content

Int add (int A, int B)

{

Return A + B;

}

5>. sub. c file content

Int sub (int A, int B)

{

Return A-B;

}

Dynamic library generation

1> first generate the target file, but then add the compiler option-FPIC and linker option-shared,

Gcc-FPIC-C add. c

Gcc-FPIC-c sub. c

Generate intermediate files Add. O and sub. o

2> Generate a dynamic library

Gcc-shared-O libtiger. So add. O sub. o

Generate the dynamic library libtiger. So, and libtiger. So is the target dynamic library we generated. We will use the dynamic library and Main. C program to generate executable programs later

Note:

The above two parts can also be merged in one step:

Gcc-FPIC-shared Add. c sub. C-o libtiger. So

2. Use Dynamic Link Library

When compiling a program, the dynamic link library is consistent with the static library. The "-l library name" method is used to link the library file when the executable file is generated.

1> run the following command:

Gcc-O main. C-L./-ltiger

2>-L specifies the path strength of the Dynamic Link Library. The-ldtiger link library function is Tiger. -Ltiger is the call rule for dynamic databases. In Linux, the dynamic library name is lib *. So, while the link represents the bit-L *, and * is the name of the library.

3> but the program will prompt the following error:

Error while loading shared libraries: libtiger. So: cannot open shared object file: no such file or direct

This is because the dynamic link library is not found when the program is running. The concept of linking a dynamic library to a program during compilation and running is different from that of using a dynamic link library. during runtime, the dynamic link library to be linked by the program must be in the system directory.

4> use the following methods to solve the problem:

A. in Linux, the most convenient solution is to copy libtiger. So to the absolute directory/lib (however, it is only available for Super Users, so sudo should be used ). The executable program can be generated.

B. The second method is to place the directory of the dynamically linked Library to the program search path. You can add the library path to the environment variable LD_LIBRARY_PATH to implement:

Export LD_LIBRARY_PATH = 'pwd': $ LD_LIBRARY_PATH

You can also generate executable programs after executing this command.

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.