1. Function library
A function library is actually a set of functions that are well written to facilitate reuse by others. After the implementation of the encapsulation, the ultimate goal is to call others.
2, the form of the library
The forms of libraries are: Dynamic link libraries and static link libraries.
Advantages:
(1) The library files are compiled binary files, others do not see the source code, can be kept confidential; (2) not because of careless modification of the problem, easy to maintain.
Library learning under LINUX: Static link libraries:. A files
In fact, it is a multi-function, compile but do not link the generated. o file, use the AR tool to package. a files. During the compilation phase, when these libraries are called, the linker goes to. A's library file to take out the. o file of the called function to execute the program that is linked to. The process is to package the. o file of the library function with the file of the main function as an executable file, so this executable file will be very large. When loaded into memory, it can also occupy a lot of space, the life cycle is the entire program life cycle.
Disadvantage: When more than one executable file contains the same library file, such as a, a, a and a, the library contains the printf, then the memory will be at the same time there are two of the same library, how wasted memory ah.
Dynamic link libraries:. So files
The link of the dynamic link library is that the library files involved are not linked to the code in the compile phase, but rather a tag is created, and when the program is running, the corresponding library file is loaded into memory for use and released when it is not needed. So when using the dynamic link library, the executable file will be relatively small, avoiding the waste of space.
Advantages: The dynamic library is in the need to be carried out into memory, so solve the dynamic library update, deployment, release code above the trouble, the user only need to load a new dynamic library, do incremental update on it.
The compile link in gcc is used by default in the way of dynamic linking, so when you want to use static linking, you need to add:-static
GCC a.c-o A.-static
Windows:
Static libraries:. lib files
Dynamic library:. dll files
3. Make your own static link library
Make your own static library, the main thing is to generate your own. A files and. h files.
(1) Make your own library function raw materials
TEST.C is what we make library function material:
#include <stdio.h>int fun1 (int A,int b) { return a + b;} int fun2 (intint b) { return A * b;}
Defines how two functions are implemented.
(2) Generate header file
The previous step implements the way the test.c is implemented, so we must have a header file with the same name as the library material.
Test.h
int fun1 (int A,int b); int fun2 (int A,int b);
A header file is actually a function prototype that declares the implemented material. Remember is a prototype.
(3) Creating a library file
Makefile:
All : -O test.o- c-RC libtest.a TEST.O
The test.c is a. o file that is used here:-C is specified to compile only, but is not linked. Remember, c is lowercase.
Use the AR Packaging tool to generate LIBTEST.A. The name of the library here cannot be arbitrarily named, because the file is TEST.O file, so the name of the library can only be libtest.a, that is, the Lib + file name.
(4) Use your own static link library
The above steps have got the. h and. A files that we need.
Your own code:
#include <stdio.h>#include"test.h"intMainintargcChar*argv[]) { inti; I= FUN1 (1,2); printf ("i =%d\n", i); I= Fun2 (3,4); printf ("i =%d\n", i);}
How to compile:
GCC Testlib.c-o testlib-l/home/qxj511/python-ltest
-L: Specifies the path to the library file, where the absolute path is specified, and the current path can be represented by electricity.
-ltest: The name of the execution library. The name of the library we generated above is LIBTEST.A, and here we use the lowercase l to represent Lib, so we can use Libtest instead.
(5) NM command
The NM command allows you to view symbolic information from a dynamic library and a static library. 、
nm libtest.a Output information: TEST.O: 00000000 t fun10000000d t fun2
It can be seen that the library contains only a TEST.O (the general library file contains a large number of. o files), while the TEST.O contains only two functions.
4. Make your own dynamic link library
The material is made using the above test.c, the head file is also test.h, here are not modified.
(3) Generate a dynamic link library
Makefile:
GCC test.c-o test.o-c-o libtest.so test.o-shared
-fpic: The file that executes the generated. O is the location-independent code.
-share: To generate a dynamic-link library
This gives you the. h and. So library files.
(4) Use your own dynamic link library
GCC Testlib.c-o testlib-l/home/qxj511/python-ltest
Compile one-time success, but when the operation is indeed an error. The reason is: Dynamic link library is to implement dynamic loading, when the need to call the relevant library, to load the corresponding library, and the default load of the dynamic library path is the fixed path of the system, so the solution:
Method One: Put your own dynamic library into the directory of the system:/usr/lib/directory.
Method Two: Add Ld_library_path. Ld_library_path is the path of the system load, the operating system load fixed directory, the first to go back to Ld_library_path to find the corresponding library files, when not found to take a fixed directory:/usr/lib/under Search. 、
Export Ld_library_path= $LD _library_path:/home/qxj511/python
The path that the library looks for is added through the Export command.
(5) LDD command
The purpose of the LDD command is to see which shared libraries the files depend on, and to see if they can be found and can be parsed.
[Email protected]:~/python# ldd testlib linux-gate.so. 1 = (0xb76fe000) libtest.so (0xb76f9000) libc.so. 6 =/lib/i386-linux-gnu/libc.so. 6 (0xb753c000) /lib/ld-linux.so. 2 (0xb76ff000)
As can be seen, our executable file is dependent on four libraries. The above means that all four libraries can be found, pointing to the digital address. When these libraries are not found, they appear: N,
Not found.
Function libraries: static and dynamic libraries