Generation and use of dynamic libraries under Linux

Source: Internet
Author: User

I. Basic concepts of dynamic libraries

1. The dynamic link library is the library that is loaded when the program runs, and when the dynamic link library is installed correctly, all programs can use the dynamic library to run the program. A dynamic-link library is a collection of target files that are organized in a dynamic-link library in a special way. The address of a function and variable in a library is a relative address, not an absolute address, and its real address is formed when the program that invokes the dynamic library is loaded.

2. The name of the dynamic link library has an alias (Soname), a true name (Realname), and a link name (linker name). The alias consists of a prefix lib, then the name of the library, plus a suffix of ". So". Real name is the real name of the dynamic link library, generally always in the base of the alias plus a small version number, release version and so on. In addition, there is a link name, which is the name of the library used when the program is linked.

3. When the dynamic link library is installed, always copy files to a directory, and then use a soft connection to generate aliases, when the library files are updated, just update the soft link.

Two. Build and use dynamic libraries

1. Take a look at an example to learn how to build a static library yourself and use a static library

To create a program file

1> has three files under the test folder: MAIN.C, Add.c,sub.c,tiger.h

2> the contents of the Main.c file:

?
1234567891011 #include<stdio.h> #include”tiger.h” intmain(void){              printf(“sum =%d\n”,add(5,3));              printf(“sub= %d\n”,sub(5,3));                return 0;     }

Content in the 3>.tiger.h file:

?
1234567 #ifndef __TIGER__#define __TIGER__ int add(int a,int  b);int  sub(int  a,int b); #endif

Content in the 4>.add.c file

?
1234 intadd(int a, int b){      returna +b;}

Content in the 5>.SUB.C file

?
1234 intsub(int a, int b){       returna - b;}

Generation of dynamic libraries

1> first generates the target file, but at this point the compiler option-FPIC and linker options-shared,

Gcc-fpic-c ADD.C

Gcc-fpic-c SUB.C

Generate intermediate files ADD.O and SUB.O

2> second generation of dynamic libraries

Gcc-shared–o libtiger.so ADD.O SUB.O

Generating dynamic library libtiger.so,libtiger.so is the target dynamic library we generate. We use dynamic libraries and MAIN.C programs to generate executable programs later

Description

The above two parts can also be synthesized one step:

gcc-fpic-shared ADD.C Sub.c-o libtiger.so

2. Using the dynamic link library

When compiling a program, it is consistent to use a dynamic-link library and a static library, using the "-L Library Name" method to link the library file when the executable file is generated.

1> using the command:

Gcc-o main main.c-l./-ltiger

2>-L specifies the path of the dynamic link library,-ldtiger the link library function Tiger. -ltiger is a call rule for dynamic libraries. The dynamic library naming method under the Linux system is lib*.so, while the link indicates that bit-l*,* is the name of the library itself.

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 due to the fact that the program did not find a dynamic link library when it ran. The concept of linking a dynamic library to a runtime using a dynamic-link library at compile time is different, and at run time, the dynamic link library that the program links to needs to be in the system directory.

4> use the following methods to resolve this issue

A. The most convenient solution under Linux is to copy the libtiger.so to the absolute directory/lib (however, if the superuser can, so use sudo oh, pro). You can generate an executable program.

B. The second method is to place the directory of the dynamic-link library in the program search path, which can be added to the environment variable Ld_library_path:

Export ld_library_path= ' pwd ': $LD _library_path

You can also generate an executable program after you execute this command

Original link: http://blog.csdn.net/tigerjb/article/details/6992531

Generation and use of dynamic libraries under Linux

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.