Linux gcc compilation (dynamic library, static library), linuxgcc
1. linux library path:/lib,/usr/lib,/usr/local/lib
2. Compile a static library in linux
A. Compile the source file
Vi pr1.c
Void print1 (){
Printf ("static print1 () callback \ n ");
}
Vi pr2.c
Void print2 (){
Printf ("static print2 () callback \ n ");
}
Vi main. c
Int main (){
Print1 ();
Print2 ();
Return 0;
}
B. Compile the library file into obj
Cc-c pr1.c pr2.c
View obj files
Ls-l pr *. o
C. link the static library (Multiple File packaging process-r append mode)
Ar-r libpr. a pr1.o pr2.o
View packaging results
Ar-t libpr.
D. compile main, use static library,-L to specify the library directory,-l to specify the library file name, and the source file libpr. a needs to be converted by default, with the header removed (lib) and tail removed (including. later), for pr. multiple libraries and multiple file names can be written into multiple-L,-l, and-o files to specify the name of the compiled output file.
Gcc-o staticdll main. c-L./-lpr
E. Run./staticdll
3. Compile the dynamic library
A. Compile the source file
Vi pr1dll. c
Void print (){
Printf ("this is dll src \ n ");
}
Vi maindll. c
Int main (){
Print ();
Return 0;
}
B. Compile the dynamic library-fPIC specifies that the dll is not related to the location,-o output path,-shared specifies the Sharing Mode
Gcc-fPIC-shared-o dll. so pr1dll. c
C. Compile the main program
Gcc-o exedll maindll. c./dll. so
D. Run:./exedll
References:
Http://www.cnblogs.com/feisky/archive/2010/03/09/1681996.html