Raspberry Pi 6: The generation and use of static and dynamic libraries for Linux libraries

The generation and use of static library and dynamic library of Linux library

1. What is a library?

A library is a binary form of executable code that can be loaded into memory and executed by the operating system. It is to convert the source code into binary format source code, which is equivalent to encrypting,Others can use the library, but cannot see the content in the library

2. How to use?

Users need to have both header files and libraries.

Header file (equivalent to the function of the manual, can know what this library can do)

The library produced (specific implementation, storing .c, .cpp)

3. Generation and use of static libraries

1) Naming rules:

The naming method of the static library file name is "libxxx.a", the library name is prefixed with "lib", the suffix is ​​".a", and "xxx" is the static library name.

2) Production steps:

Raw materials: source code .c or .cpp

Generate .o from .c file, gcc a.c b.c-c Generate .o file

Will ==.o== package
ar rcs static library name raw material
ar rcs libtest.a ao bo
Note :
ar: get the meaning of the instruction
rcs: generate static library, the end of the static file:.a

3) Use of the library (provide .h.a):Only need to provide API description in the header file

gcc test.c -I ./include -L./lib -lmycalc -o app

-I (uppercase i): specify the path of the header file

-L: Specify the path of the library

-l (lowercase L): specify the name of the library (remove lib and .a)

-o: Specify the name of the final application generated

Example

Insert picture description here
Stored in the include directory is the header file head.h for addition, subtraction, multiplication and division

#ifndef __HEAD_H_
#define __HEAD_H_
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);
#endif

The source code of addition, subtraction, multiplication and division is stored in the src directory:

Insert picture description here
Generate .o files from .c files
Because the head.h and .c files are not at the same level, they need to be pointed out by -I
Insert picture description here
Then package .o, ar rcs static library name raw material

Insert picture description here
Put the created library in the directory lib, or leave it alone, in fact, as long as you have this library, here, move it to the lib in the upper directory

Insert picture description here
Then send the include and lib to the user.

4) The user's use of the library:

Users can use the library to achieve their needs after getting the two directories above

Include header file

#include <stdio.h>
#include "head.h" 
int main(void)
{
    
        	
	int sum = add(2, 24);
    	printf("sum = %d\n", sum);
        return 0;
}

Insert picture description here

Realize the use of static libraries through the above operations

4. Generation and use of dynamic libraries

1) Naming rules:

The naming method of the dynamic library is similar to that of the static library, the prefix is ​​the same as "lib", and the suffix is ​​changed to ".so". So "libmytime.so"

2) Production steps:

To generate .o from the source file, you need to add a parameter fpic

gcc a.c b.c -c -fpic(fPIC)
Insert picture description here
打包,gcc -shared a.o b.o -o libxxx.so

Insert picture description here
Put the created library in the directory lib, or leave it alone, in fact, as long as you have this library, here, move it to the lib in the upper directory

3) Use of the library:

gcc main.c -I ./ -L ./ -l test -o app

Insert picture description here
It will prompt No such file or directory at this time, need to set environment variables

The dynamic library cannot be loaded:

Use environment variables
Temporary setting:
in the terminal:
export LD_LIBRARY_PATH=path of dynamic library: $LD_LIBRARY_PATH

User level:
~/.bashrc, after the configuration is complete, restart the terminal or source ~/.bashrc
System level:
/etc/profile, after the configuration is complete, restart the computer or source /etc/profile

Insert picture description here

How to specify the location of the dynamic library when referencing the dynamic library:
linux dynamic library (.so) search path (directory) setting method

For programs with dynamic libraries, you can specify when the program is running, find the library file export LD_LIBRARY_PATH="/home/pi/back/test" in the path specified by LD_LIBRARY_PATH

Reference: Libraries in Linux

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108607122