Linux library concept and sub-file programming (interview focus)

introduceinsert image description here

related blog

https://www.cnblogs.com/sunsky303/p/7731911.html
https://blog.csdn.net/wk_bjut_edu_cn/article/details/81735956

library

a) Static library
The static function library is added to the target program before the program is executed (compiled);
advantages: fast running,
no need to provide a static library for the released program, because it is already in the app, and it is easy to transplant
Disadvantages: large

b) Dynamic library
The dynamic function library is dynamically (temporarily) called by the target program when the program is executed.
Disadvantage: Slow operation
Advantage: Small

library production

Production of static library:

(The naming method of the static library file is "libxxx.a", add "lib" before the library name, use ".a" as the suffix, and "xxx" is the static library name) Format xxxx.a a. gcc calcufuncs.c
-c
Generate xxx.o file
b. ar rcs libcalcufunc.a calcufuncs.o
xxx.o file generates xxx.a static library file

Production of dynamic library:

(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 becomes ".so")
Format: gcc -shared ao bo -o libxxx.so
Example: gcc -shared -fpic calcufuncs.c - o libcalc.so

-shared Specifies to generate the dynamic library
-fpic standard, the fPIC option acts on the compilation stage, and this option must be used when generating the object file to generate position-independent code.

library use

Static library:

gcc calculatorT.c -lcalcufunc -L ./ -o mainProStatic

-L: Specify the path of the library

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

-o: Specifies the name of the generated final application

-L tells the gcc compiler to find the static library from the path specified by -L. The default is to find it from /usr/lib /usr/local/lib and
use it in the current path./

Dynamic library:

gcc calculatorT.c -lcalc -L ./ -o mainProDy
libcalc.so
references the dynamic library, how to specify the location of the dynamic library
https://www.cnblogs.com/progamming/p/13043652.html
Programs with dynamic libraries
can Specify that when the program is running, find the library file in the path specified by LD_LIBRARY_PATH
export LD_LIBRARY_PATH="/home/pi/back/test"
At this time, it will prompt No such file or directory, and you need to set the environment variable

Use environment variables
Temporary setting:
In the terminal:
export LD_LIBRARY_PATH="path of dynamic library"

For example:
export LD_LIBRARY_PATH="/home/pi/mydir/test/dongtaiKu"

Then run the file (./mainProDong)

Another problem:
the environment variable at this time is only temporary, and the file cannot be executed in another window.
We can package the environment variable and the running file into a shell file (execute these two commands at the same time)
export LD_LIBRARY_PATH="The path of the dynamic library"
./mainProDong

The Share script needs to add an executable permission: chmod +x start.sh
and finally ./start

Note: The functions encapsulated by the library are functional functions. After the functional functions are encapsulated, they are transplanted into the programs they need.

Guess you like

Origin blog.csdn.net/weixin_48321071/article/details/124867482