Creation and use of static function libraries and creation of static function libraries
The C language function library is a set of multiple groups of frequently-used functions that have been verified. When you compile a C Language Program, the library function can improve the program running efficiency and programming quality, use methods such as # include and # include.
The function library is divided into static and dynamic functions based on the differences in the loading time of the function library when using the library function. The specific difference is: if the C language program uses the function of the static function library, the code of the entire function library will be compiled into executable code together with the C language program, and the size of the program will expand. If the function of the dynamic function library is used, then, the C language program will only compile the code into executable code (without compiling the function code) together with the name and function name of the function library. during runtime, the library file and function body will be searched, and the size of the program will remain unchanged.
In short, the static function library increases the program volume and reduces the running time by changing the space. If the static function library changes, the entire program must be re-compiled, because the function library is integrated into the final executable code, the dynamic function library is "time-for-space", increasing the running time and reducing the program volume. If the dynamic function library changes, the program does not need to be re-compiled because the function library is not integrated into the final executable code.
In Linux, the static function library is represented as "libxxx. a ", windows suffix is ". in Linux, the dynamic function library is represented as "libxxx. so ", windows suffix is ". dll ". The function library contains the following content: (1) function name, (2) function target code (Binary), and (3) Relocation information (link required.
1. creation and use of static function libraries
The creation steps of the static function library can be described, including
(1) Compile the function. c file (for example, add. c, sub. c, mul. c, and div. c)
(2) Compile Makefile and make to compile and archive functions.
Function Compilation: Use gcc-c to compile functions without link. c file to generate the target file of the function (for example, add. o, sub. o, mul. o and div. o ).
Function archive: Use ar-rc libxxx. a $ (objects) to archive the target file.
(3) Compile the header file (for example, ku. h) declare all functions in the static function library for the purpose of kumain. after the c function # include header file, you can call the corresponding function. At this point, the function library is created.
1.1Example of creating a static function library
The content of the example is to create the static library libstatic. a. The library contains the add, sub, mul, and div functions, and then in the kumain. the c function references these four functions to add, subtract, multiply, and divide two integers. The structure of the entire file is
All code see: http://files.cnblogs.com/files/hfxin2001-eric-daddy/ku.rar
(1) Compile the function. c file
Compile four 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/b);
} |
|
|
(2) Compile the header file
// 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) Write 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 mul.c
div.o : div.c
gcc -c div.c
clean :
rm libstatic.a $(objects)
(4) use make to compile the. c file, generate the. o file, and archive the. o file to the libstatic. a function library to complete the creation of the static function library.
1.2 use of static function libraries
(1) Compile kumain. c and call the add, sub, mul, and div functions in libstatic..
// kumain.c
#include <stdio.h>
#include "ku.h"
int main (void)
{
int a,b;
a = 10;
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) Use gcc kumain. c-o kumain. o-L./ku2-lstatic to compile the kumain. c file and run./kumain. o to check the running result.
1.3 Use nm to view Symbol Information in kumain. o
The nm command lists the symbolic information in. o files,. a files, and. so files, such as the symbolic value, type, and name. Symbols usually refer to defined functions and global variables.
Use nm libstatic. a to view the symbol information.
Use nm kumain. o> label. text to view the Symbol Information in kumain. o.
080484f9 T add
0804a020 B __bss_start
0804a020 b completed.6591
0804a018 D __data_start
0804a018 W data_start
08048370 t deregister_tm_clones
0804853c T div
080483e0 t __do_global_dtors_aux
08049f0c t __do_global_dtors_aux_fini_array_entry
0804a01c D __dso_handle
08049f14 d _DYNAMIC
0804a020 D _edata
0804a024 B _end
080485c4 T _fini
080485d8 R _fp_hw
08048400 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
08048550 T __libc_csu_init
U __libc_start_main@@GLIBC_2.0
0804842d T main
08048527 T mul
U printf@@GLIBC_2.0
080483a0 t register_tm_clones
08048330 T _start
0804850f T sub
0804a020 D __TMC_END__
08048360 T __x86.get_pc_thunk.bx
Introduction to 1.4 nm commands
Nm [option (s)] [file (s)]
UsefulOptions:
- -A prints the object file name before each symbol information;
- -C: name of the demangle symbol;
- -D: Print dynamic symbols;
- -L use the debugging information in the object file to print the source file and row number;
- -N is sorted by address/Symbol value;
- -U prints undefined symbols;
CommonSymbol type
- A The value of this symbol will not be changed in future links;
- B. This symbol is placed in the BSS segment. It is usually the uninitialized global variables;
- D. Place the symbol in a common data segment, which is usually the initialized global variables;
- T this symbol is placed in the code segment, usually the global non-static functions;
- U is undefined and must be linked from other object files;
- W is not explicitly specified as a weak link symbol; other object files with the same link are used for its definition; otherwise, a default value specified by the system is used.