Use of static and dynamic libraries in Linux

Source: Internet
Author: User

Use of static and dynamic libraries in Linux
1. Database Introduction

The c/c ++ project's source code file is generally composed. c /. cpp and. h file, maybe the function you write is intended for use by others, but you cannot provide the source code you write to others, and the library will solve this problem well. The-c compilation option of gcc can only compile the c/c ++ source file without linking, that is, generate the. o file .. O files and. c /. cpp files are actually source code files, but the former is in binary format (the human eye cannot identify the content) and the latter is in text format (the human eye can recognize its content ), the former and the latter can be correctly identified by computers. We can package these. o files to form a library file that cannot be identified by source code.

If the user cannot identify the code of the library file, how can he use the function in the library file? This requires header files. Therefore, the H file does not need to generate binary format and then submit it to the user. The user can correctly use the functions in the library file according to the function declaration in the. h file.

2. Static library 2.1 naming rules

In Linux, the static library name consists of the prefix, name, and suffix. Take libtest. a as an example:

(1) prefix (fixed): lib

(2) name (custom): test

(3) suffix (fixed):.

2.2 static Library Creation

(1) generate a. o file from the. c/. cpp file.

gcc -c a.c b.c

(2) package the. o file
Name of the static library of ar rcs. o file

ar rcs libtest.a a.o b.o

For example, package the. c file used to implement the four arithmetic operations into the libmycalc. a file:

//include/calc.h#ifndef __CALA_H__#define __CALA_H__int add(int a, int b);int sub(int a, int b);int mul(int a, int b);int p(int a, int b);#endif /* __CALA_H__ *///src/add.c#include "calc.h"int add(int a, int b){    return a + b;   }//src/p.c#include "calc.h"int p(int a, int b){    return a / b;}//src/mul.c#include "calc.h"int mul(int a, int b){    return a * b;}//src/sub.c#include "calc.h"int sub(int a, int b){    return a - b;   }

(-I specifies the path of the header file)

2.3 use of static databases
//main.c#include 
 
  #include "calc.h"int main(void){    printf("%d\n", add(4, 6));      return 0;}
 

(-L specifies the path of the database,-l specifies the name of the database (excluding the prefix and suffix ))

3. Dynamic library 3.1 naming rules

In Linux, the dynamic library name is still composed of the prefix, name, and suffix. Take libtest. so as an example (some dynamic library names also contain version information, such as libtest. so.10.0.0 ):

(1) prefix (fixed): lib

(2) name (custom): test

(3) suffix (fixed):. so

3.2 Create a dynamic library

(1) generate a. o file from the. c/. cpp file.

Gcc-c a. c B. c-fpic //-fpic can also be written as-fPIC

(2) Packaging

gcc -shared -o libtest.so a.o b.o

Take the previous Code as an example:

3.3 Use of dynamic libraries

The method is the same as that for static databases:

When running a. out, the system prompts that the libmycalc. so dynamic library cannot be found. This is because: when the system loads executable code, you need to know the name of the library on which the program depends, and the absolute path of the library. The former is specified through the gcc compilation command option, and the latter is requiredSystem Dynamic LoaderComplete. You can use the ldd command to view the libraries on which the elf executable program depends:

/Lib/ld-linux.so.2 is the system dynamic loader. The executable program in elf format is composedLd-linux.so *To search:

(1) DT_RPATH section of the elf File

(2) environment variable LD_LIBRARY_PATH

(3)/etc/ld. so. cache file list

(4)/lib/or/usr/lib directory

To let the loader find the dynamic library, we can:

(1) copy your own libmycalc. so to/lib/or/usr/lib.

(2) temporarily set the environment variable LD_LIBRARY_PATH

Export LD_LIBRARY_PATH = $ LD_LIBRARY_PATH: LIBRARY PATH

"$ LD_LIBRARY_PATH: library path" indicates adding the library path to the original value of LD_LIBRARY_PATH.

(3) permanently set the environment variable LD_LIBRARY_PATH

A. user level

In ~ /. Add the absolute path of the dynamic library search in the bashrc file.

After the configuration is complete, restart the terminal or run the following command:

Source ~ /. Bashrc or .~ /. Bashrc

B. System Level

Add the above path to/etc/profile. After configuration, restart the system or execute the command:

Source/etc/profile or./etc/profile

(4) Update the/etc/ld. so. cache file list.

Add the absolute path of the dynamic library to the configuration file/etc/ld. so. conf and run the following command:

sudo ldconfig -v

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.