**Linux library concept and how to make static library and dynamic library

1. File-based programming case

For example, the project: obstacle avoidance can only be a car.
Network: Class A
Ultrasonic: Class B
Motor : Class C
Benefits: Sub-module programming idea
a. Clear division of labor (division of functional responsibilities), b. Convenient debugging, c. Concise
and file-based programming of the main program step:

1. Write a function called
hello.c

//hello.c
#include <stdio.h> //这里加一下头文件会更好,不加有的会有警告
void File1()
{
    
    
		printf("hello word\n");
}

2. Copy the .C file that calls the function, change the suffix to .h to clear the contents of the function, and leave the function header.
hello.h

//hello.h  这个头文件一定要加到main函数里面
void File1();

3. Put one below the main function (main program entry)

//main.c
#include <stdio.h>
#include "hello.h"//双引号是从当前路径找 默认是会去 /usr/include/里面的
int main()
{
    
    
	File1();
	return 0;
}

4. Compile the main function and the function together.

gcc main.c hello.c //会生成a.out 可执行文件

./a.out 即可运行

Second, the library (static library and dynamic library)

Shared library, static library, dynamic library related information reference source URL
Insert picture description here

1. Static library

Static function library: It is added to the target program before the program is executed (compiled);

Advantages:
1. Fast running,
2. The static library is packaged into the application and loaded fast.
3. The release program does not need to provide a static library, because it is already in the APP and is easy to transplant.

Disadvantages:
1. Big program
2. Trouble in updating, deploying, and publishing (as soon as the update is sent to the manufacturer)
3. Completely copy to the executable file when linking, do not use multiple redundant copies.

2. Dynamic library

Dynamic function library: is dynamically (temporarily) called by the target program during program execution;

Advantages:
1. The program is small
. 2. It is not copied when linking. The program is dynamically loaded into memory by the system when it is running for memory call. The system is only loaded once, and multiple programs can be shared, saving memory.
3. The program upgrade is simple, because there is no library source code in the APP. After the upgrade, as long as the library name remains the same, the function name and the parameters remain the same, it is only optimized to load successfully.

Disadvantages:
1. Running slower
2. Loading speed is slower than static library
3. Release program needs to provide dependent dynamic library.

Three, the production and use of the library

1. Static library production:

Static library (.a ending):
The naming format of the file name is libxxx.a
library name plus lib
suffix with .a
static library name xxx

Step 1: (Generate a file ending in .o)

When we compile the program, add a -c at the end

例如:gcc  hello.c  -c //这样就生成hello.o

Step 2: (Pack the hello.o file ar rcs lib+静态库名字+.a 原材料)

例如:ar rcs libhello.a  hello.o  
//hello.o就是刚才生成了那个。(静态库名字随意取,记得前面加lib)

In this way, we have generated a static library file (the one at the end of .a)

How to use the static library (.a):

gcc main.c -lhello -o staticLibrary
//gcc main.c libhello.a  -o staticLibrary 这样也行  
//这样就会生成 staticLibrary 可执行文件

/*
main.c:  是指main主函数
-lhello: 是我们生成的.a 文件砍头去尾(lib不要 .a也不要)前面加-l
-L:      是指告诉gcc编译器先从-L指定的路径去找静态库,
默认是从/usr/lib/ 或者  /usr/local/lib/ 去找。
./:      是 当前路径的意思
staticLibrary: 是我最后想生成的文件名(这里可随意起名字)
*/

./staticLibrary  //运行即可

2. Dynamic library production and use:

The production of dynamic library (generating .so ending file)


gcc -shared -fpic hello.c -o libhello.so
-shared 指定生成动态库
-fpic :fPIC选项作用于编译阶段,在生成目标文件时就得使用该选项,
以生成位置无关的代码。

Use of dynamic library

Compile first

gcc main.c -lhello -L ./ -o dynamicDepot
/*
main.c:  是指main主函数
-lhello: 是我们生成的.so 文件砍头去尾(lib不要 .so也不要)前面加-l
-L:      是指告诉gcc编译器先从-L指定的路径去找静态库,默认是从/usr/lib/ 
或者  /usr/local/lib/ 去找。
./:      是 当前路径的意思
dynamicDepot: 是我最后想生成的文件名(这里可随意起名字)
*/

How to run:
refer to the source URL

Method one (rarely used):
copy the file ending in .so to /usr/lib/ (you have to switch to super user and add sudo in front)

input the command:

sudo cp libhello.so /usr/lib/ 

run:

./dynamicDepot

Method two: (recommended)
1. Add instructions: export LD_LIBRARY_PATH=”路径”
for example:

export LD_LIBRARY_PATH=/home/CLC/pointsFile/STATIC_LIBRARY

The path can be viewed by entering the command pwd

But this is also a bit flawed, that is, you can't run it if you change a window

So we will create a file ending with .sh (script)

Let's first create a .sh file
vi start.sh(build script) and
write the following two

export LD_LIBRARY_PATH=/home/CLC/pointsFile/STATIC_LIBRARY

./dynamicDepot

Then we attach executable permissions to this script and
enter the commandchmod +x start.sh

So no matter if we change to a new window, we can run the script directly

Guess you like

Origin blog.csdn.net/weixin_47457689/article/details/109170176