1, the production of dynamic link library
The dynamic link library has no main function compared to a normal program. Generate. So dynamic link library files with-shared and-fpic compilation parameters. When the program calls the library function
Just link to this library.
1) write dynamic library code
Define header file reader.h
#ifndef Reader_h_ #define Reader_h_int open (char *name); int Close (char *name); #endif // Reader_h_
Writing function Body reader.c
#include <stdio.h>int open (char *name) { printf ("%s opened.\n ", name); return 0 ;} int Close (char *name) { printf ("%s closed.\n") , name); return 0 ;}
2) Generate Dynamic Library files
gcc -shared-fpic reader.c-o libreader.so
2, the use of dynamic library
First step: Write the test code TESTLIB.C
" Reader.h " int Main () { char readername["81Reader"; Open (readername); Close (readername); return 0 ;}
Step Two: Call the Dynamic library (-L indicates the path to the dynamic-link library)
gcc testlib.c-o testlib-l./-lreader
Step three: Test output
81Reader Opened.81reader closed.
The production and use of dynamic library under Linux