Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are Linux static function library and dynamic function library?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is Linux static function library and dynamic function library". In daily operation, I believe that many people have doubts about what Linux static function library and dynamic function library are. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is Linux static function library and dynamic function library". Next, please follow the editor to study!

Static function library for static function library: when compiling a link, the functions needed by the program are copied from the static function library to the execution file. When the program is running, there is no need to link to external libraries.

1. A static function library is a collection of multiple object files * .o

2. You can use the ar command (archiver) to generate .a files

Gcc-c part_a.c gcc-c part_b.c ar-r libtest.a part_a.o part_b.o 3, to use the functions in the static function library, simply include the declaration header file of these functions and specify the name of the static function library at compile time, and the compiler will copy the function to the object file.

4. The naming convention of the generated function library is: lib+ function library name. For example, to generate a library of functions for test, name it libtest.a

Example:

(1) create three files: main.c, part_a.c and part_b.c

# include # include "part_a.h" int print_part_a (void) {printf ("part A\ n"); return 0;} # include # include "part_b.h" int print_part_b (void) {printf ("part B\ n"); return 0;} # include # include "part_a.h" # include "part_b.h" int main (void) {print_part_a () Print_part_b (); return 0;} (2), here, we create three directories: src, include, and lib, which store the source files, header files, and library files, respectively, and then the resulting target files are stored in the current directory.

(3) write Makefile

INCLUDE + =-I./include SRC + =. / src LIB + =. / lib LIB_FILE + = $(LIB) / libtest all: lib gcc $(INCLUDE)-o main $(SRC) / main.c-L$ (LIB)-ltest lib: $(SRC) / part_a.o $(SRC) / part_b.o ar-r $(LIB_FILE) .a $(FILE_A_OBJ) $(FILE_B_OBJ) $(SRC) / Part_a.o: $(SRC) / part_a.c gcc $(INCLUDE)-c $(SRC) / part_a.c $(SRC) / part_b.o: $(SRC) / part_b.c gcc $(INCLUDE)-c $(SRC) / part_b.c (4), Execute the. / main file The contents of the print_part_a () function and print_part_b () function will be printed successively.

Analysis: we use the commands objdump-S main and objdump-S libtest.a to disassemble and see that the functions of the static library have been copied in the executable main at this time.

Dynamic function library (not available for now. For dynamic function libraries: when compiling links, the functions needed by the program are not copied from the dynamic function library, but are loaded when the program is running.

4. Commands and related parameters used in this paper

Analysis of parameters related to gcc command:

1.-Idirname:-I (Note: big I) can specify the header file path as dirname (. / represents the current path.. / represents a higher-level directory) and add it to the list of program header files.

For example, if the directory where the header file is located is include, then the input parameter is:-I. / include

2.-c: (note: small c) only precompile and assemble to generate the object file. O

3.-Ldirname:-L adds the indicated directory dirname to the target list of the program library, that is, tells the link script to look in the directory specified by-L first, and then look in the default path of the system.

For example, if you specify the current directory, the input parameter is:-L.

4.-lname:-l (Note: small L) you can specify that the function library named libname.an is loaded when linked.

For example, if you need a function library named libtest.a, the input parameter is:-ltest

Analysis of parameters related to ar command:

1.-r: insert the file into the library file

2.-c: create a library file

3.-t: displays the files contained in the library file

4.-d: delete the member files specified in the library file

At this point, the study of "what is the Linux static function library and dynamic function library" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report