C language function library is a set of many commonly used functions, the use of library function when writing C language program, can improve program running efficiency and improve programming quality, use methods such as #include and #include.
Depending on how the library function is used, function library loading time difference, the function library is divided into static function library and dynamic function library, the specific difference is: C language Program If you use the function of static function library, then the entire function library code and C language program together compiled into executable code, the volume of the program will expand If you use a function of a dynamic function library, the C language program will only compile the executable code with the function library file name and function name (do not compile the function code), run to find the function library file and function body, the volume of the program is basically unchanged.
The simple generalization is that the static function library is "space-changing Time" increase the volume of the program, reduce the running time, if the static function library changes, the entire program must be recompiled, because the function library is integrated into the final executable code, the dynamic function library is "Time for space", increase the running time, reduce the volume of the program, If a dynamic function library changes, the program does not need to be recompiled because the function library is not consolidated into the final executable code.
The static function library in Linux is represented as "libxxx.a", and in Windows, the suffix named ". Lib", and the dynamic function library in Linux is represented as "libxxx.so", and the suffix in Windows is ". dll". The contents of the function library include: (1) function name, (2) Function object code (binary), (3) Relocation information (link required), etc.
1 The production and use of the static function library
The process of creating a static function library can be described in the following diagram, including
(1) Write functions of. c files (e.g. ADD.C, SUB.C, MUL.C, and DIV.C)
(2) Write Makefile, then make, realize the function of compiling and archiving storage
Compilation of functions: Use GCC–C to compile only the unlinked functions. c files, generating the target files (for example, ADD.O, SUB.O, MUL.O, and DIV.O) of the functions respectively.
Archived storage of functions: Use AR-RC libxxx.a $ (objects) to archive the target files into the library.
(3) Write header files (such as ku.h), declare all functions in the static function library, the purpose is to KUMAIN.C function #include header file, you can call the corresponding function, so, complete the production of function library.
1.1 Making examples of static function libraries
The example is to create a static function library LIBSTATIC.A, which includes add,sub,Mul, and div functions, and then in kumain.c function to implement the subtraction of two integers, the structure of the entire file is
(1) Write a function of the. c File
Write 4 function Files add.c, SUB.C, MUL.C, and DIV.C
ADD.C
float Add (int a, int b)
{return
(a+b);
}
SUB.C
Float Sub (int a, int b)
{return
(a-b);
}
MUL.C
float mul (int a, int b)
{return
(a*b);
}
Div.c
Float Div (int a, int b)
{return
(A/a);
(2) Writing header files
Ku.h
float Add (int a, int b);
float Sub (int a, int b);
float Mul (int a, int b);
float Div (int a, int b);
(3) Writing makefile
### Makefile for static func lib
objects = add.o sub.o mul.o div.o libstatic.a
: $ (objects)
AR-RC LIBSTATIC.A $ (objects)
add.o:add.c
gcc-c add.c
sub.o:sub.c
gcc-c
sub.c mul.o:mul.c gcc-c C15/>DIV.O:DIV.C
gcc-c div.c clean
:
RM libstatic.a $ (objects)
(4) Compile. c files using make, generate. o files, archive. o Files to function library LIBSTATIC.A, complete the production of static function library.
1.2 Use of static function libraries
(1) Writing kumain.c, calling Add, Sub, mul, and DIV functions in LIBSTATIC.A
Kumain.c
#include <stdio.h>
#include "ku.h"
int main (void)
{
int a,b;
A = ten;
b = 3;
printf ("a =%D.\NB =%d.\n", a,b);
printf ("Static a+b =%f.\n", add (a,b));
printf ("Static a-b =%f.\n", sub (a,b));
printf ("Static a*b =%f.\n", Mul (A,b));
printf ("static A/b =%f.\n", div (a,b));
return 0;
}
(2) Compile kumain.c file using gcc kumain.c–o kumain.o–l./ku2–lstatic, run./KUMAIN.O to view the results of the run, success.
1.3 Use NM To view symbolic information in KUMAIN.O
The NM command lists the symbol information in the. o file, the. a file and the. So file, such as the value of the symbol, the type of symbol, and the name of the symbol. A symbol usually refers to a defined function, a global variable, and so on.
Use nm LIBSTATIC.A to view symbolic information and get
Use the NM KUMAIN.O >label.text to view symbolic information in KUMAIN.O and get
080484f9 T add 0804a020 b __bss_start 0804a020 b completed.6591 0804a018 D __data_start 0804a0 W Data_start t deregister_tm_clones 0804853c t div 080483e0 t __do_global_dtors_aux 08049f0c t __do_global_dtors_aux_fi Ni_array_entry 0804a01c D __dso_handle 08049f14 d _dynamic 0804a020 d _edata 0804a024 B _end 080485c4 T _fini 080485d8 R _
FP_HW t frame_dummy 08049f08 t __frame_dummy_init_array_entry 080487b8 R __frame_end__ 0804a000 d _GLOBAL_OFFSET_TABLE_
W __gmon_start__ 080482cc t _init 08049f0c t __init_array_end 08049f08 t __init_array_start 080485dc R _io_stdin_used W _itm_deregistertmclonetable W _itm_registertmclonetable 08049f10 d __jcr_end__ 08049f10 D __JCR_LIST__ W _
Jv_registerclasses 080485c0 t __libc_csu_fini t __libc_csu_init U __libc_start_main@ @GLIBC_2.0 0804842d t main t Mul U printf@ @GLIBC_2.0 080483a0 t register_tm_clones t _start 0804850f t Sub 0804a020 D __tmc_end__ t __x86.get_pc_thunk . BX
Introduction to 1.4 NM command
NM [option (s)] [file (s)]
Useful options:
-A prints the object file name before each symbol information;
-C output Demangle symbol name;
-D print dynamic symbol;
-L Print out the source file and line number using the debug information in the object file;
-N is sorted by address/symbol value;
-U prints out those undefined symbols;
Common Types of symbols
A the value of the symbol will not change in future links;
B the symbol is placed in the BSS section, usually those uninitialized global variables;
D the symbol is placed in ordinary data segments, usually those that have already been initialized, the global variables;
T the symbol is placed in the code snippet, usually the global non-static function;
U the symbol is not defined and needs to be linked in from other object files;
W is not explicitly specified as a weak link symbol, and it is used in other object files with links, otherwise the default value specified by a system is used.