Linux GCC compiles dynamic class libraries (. So) and static class libraries (. a)
My compilation environment for Ubuntu desktop 16.04
One: Test code
The test has 3 files: ab.h,ab.c,test.c
//AB.hvoidhello ();//AB.C#include <stdio.h>voidHello () {printf ("Hello from AB.C \ n");}//test.c#include <stdio.h>#include"AB.h"voidMainvoid) {printf ("It is main\n"); Hello ();}
View Code
Using GCC, compile and run to display the results:
[Email protected]:~/desktop/demo$ gcc AB.C test.c
[Email protected]:~/desktop/demo$./a.out
It is main
Hello from AB.C
Second: GCC compiles the static class library. A
//compile point o file[Email protected]:~/desktop/demo$GCC-C AB.C//compile to AB.O file[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out test.c//package as. A file[Email protected]:~/desktop/demo$ar-CRV LIBAB.A AB.O a-AB.O//Compile test program, test LIBAB.A[Email protected]:~/desktop/demo$GCC-o testliba test.c libab.a//Test procedure Testliba[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out libab.a test.c Testliba//run the test and the output is consistent with the above[Email protected]:~/desktop/demo$./Testliba It is mainhello from AB.C [email protected]:~/desktop/demo$
Three: GCC compiles the dynamic class library. So
//compile AB.C as a dynamic class library libab.so[Email protected]:~/desktop/demo$GCC-shared-o libab.so-FPIC AB.C//View[Email protected]:~/desktop/demo$lsab.c AB.h ab.o a.out libab.a libab.so test.c Testliba//Compile the test program Testso and link the libab.so in the current directory[Email protected]:~/desktop/demo$GCC-O Testso Test.c-lab-L.//set the dynamic class library path[Email protected]:~/desktop/demo$ Export ld_library_path=.: $LD _library_path//run the test program with the results consistent with the above[Email protected]:~/desktop/demo$./Testso It is mainhello from AB.C [email protected]:~/desktop/demo$
External supply, only need to put AB.h and libab.a or libab.so can;
Reference: http://www.cnblogs.com/ymy124/archive/2012/04/13/2446434.html
Linux GCC compiles dynamic class libraries (. So) and static class libraries (. a)