Dynamic library generation and use in Linux

Source: Internet
Author: User

Dynamic library generation and use in Linux
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.

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.

 

Original article address: dynamic library generation and use in linux


For linux dynamic database connection problems

During gcc compilation, when using dynamic library compilation, you can follow several methods
1. gcc test. c./libSDL2-2.so
2. gcc test. c-lSDL2-2
3. gcc test. c-L/home/test-lSDL2-2
Generally, the compilation parameters are written according to 2 or 3.
2 The meaning of writing is from the/lib or/usr/lib directory to find the library named SDL2-2, that is, find/lib/libSDL2-2.so or/usr/lib/libSDL2-2.so file for the link, of course, if there is no dynamic library, it will go to the static library. If there is no dynamic library, an error will be reported during compilation.
3. The meaning of this statement is that the-L parameter first searches for the library file to be linked from the specified directory, and then searches for the required library in the system folder.
1. /The libSDL2-2.so.0 file is linked to the final file, so execute readelf-. the path shown in the dynamic library section after exiting is. /libSDL2-2.so.0, and in the execution of the file will only find the libSDL2-2.so.0 file from the current directory, when the execution of the file is not in the directory of the file will appear not find the library file operation

When you perform the second operation. so library file and. the out file is in the same directory. It is also the ldd operation and. out,. when loading the dynamic library, the required library file is found in the current directory, and the execution is successful (The ldd command is essentially a script, set environment variables to run the dynamic library linker to output all the dynamic libraries to be linked ).
You can try to copy a. out to another directory and run it again. It will be the same as the first operation. The function. so file cannot be found.

The specific solution is to modify the compilation parameters. /libSDL2-2.so.0 change to-lSDL2-2 and copy the libSDL2-2.so.0 file to the/usr/lib directory, in addition, because the linker's cache file has not been modified, the dynamic library with the version number suffix may not be found ), you need to establish a file connection (ln-s libSDL2-2.so.0 libSDL2-2.so) under the/usr/lib directory or directly change the name to libSDL2-2.so
 
Can FORTRAN generate dynamic link libraries in LINUX? Such as so? If yes, what editor does it use?

Yes, for example, lintel fortran compiler.
Ifort-c-fpic test. f90ifort-shared-o test. so test. o

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.