Use of dynamic and static libraries under embedded Linux

The following mainly introduces the dynamic library and static library in the embedded Linux environment, and how to use the dynamic library and static library when compiling.
1 Naming rules
1) The dynamic library format lib***.so
Insert picture description here
under Linux is 2) The static library format under Linux islib***.a
Insert picture description here

2 The difference between a dynamic library and a static library
1) During the compilation process, the
dynamic library only refers to the function interface when the program is compiled and linked, and the compiled program is relatively small; when the static library is compiled and linked, the functions used have been incorporated into the program and compiled The program that comes out is relatively large.
2)
If the deployment on the board is a dynamic library, the corresponding function in the library will be called only when the relevant function is executed. Therefore, when deploying on the board, the dynamic library needs to be placed in the lib directory of the board, otherwise it will be prompted when the program is executed" "Cannot find xxx dynamic library" and terminate the running program; the static library has been loaded into the executable program when compiling and linking, so it does not need to be deployed on the board.
3) The impact of library updates on the program When the
dynamic library needs to be updated, as long as the function interface used remains unchanged, the program that uses it does not need to be compiled again, but the updated dynamic library needs to be deployed to the board again (replace the original library ); When the static library is updated, the program that uses it needs to be recompiled.

3 How to use the library in the program compilation process
No matter whether dynamic or static library is used in the program , the method of use is the same. Need to specify the library function header file path when compiling, specify the path and library name of the library when linking.
1) Use -L to specify the path where the library is located

-L/库所在的路径 

2) Use -l to indicate the library to be linked (note l is a lowercase L)

-l库名	 

3) Use -I (note the initial letter of include is capitalized I)

-I/头文件路径 

Take the ssh static library as an example to introduce the use of the library in the program compilation process. Note that the dynamic library is used in the same way during the compilation process.
Suppose the program uses two static libraries, libssh.a and libopenbsd-compat.a (according to the naming rules, the library names of these two libraries are ssh and openbsd-compat respectively), and these two libraries are placed in the same level directory as the makefile In the ssh directory, then specify the location of library functions and library header files in the makefile. Part of the makefile is as follows

LIB = -lssh -lopenbsd-compat 
LDFLAGS = -L./ssh/lib/
INCLUDE = -I./ssh/include \
			-I./ssh/include/openbsd-compat 
$(PROJECT):$(OBJS)
        @ $(GCC) -o $@ $^ $(CFLAG) $(LDFLAGS) $(LIB)
%.o:%.c
        $(GCC) -c $(CFLAG) $(INCLUDE) -o $@ $<	

Note, because when we actually compile the library, especially when compiling the open source library, the compiled library has different versions, such as libssh.so.4.8.1, then in actual use, you should link libssh.so to libssh Use it after .so.4.8.1. Changed to libssh.so, because you can use -lssh to specify the library name, which is convenient to use. If you insist on using the name libssh.so.4.8.1, you can just specify the library directly when linking, such as -L./ssh/lib/libssh.so.4.8.1, so you don’t need to use it anymore. -lssh specifies the library, and it cannot be specified in this way, because this library is no longer called ssh. For library names in the format of libssh.so, if you don't need -lssh to specify, you can also directly use -L./ssh/lib/libssh.so to specify the library to be referenced.

Guess you like

Origin blog.csdn.net/weixin_47745230/article/details/108179878