Reprint please indicate: http://www.cnblogs.com/winifred-tang94/
1. Compilation process
Gcc–fpic–c xxx.c
Where-fpic is the target code that notifies the GCC compiler to produce a location independent. The link does not go through a copy.
2. Link process
Gcc–shared–o libxxx.so XXX.O
A compiled link allows you to generate a dynamic-link library with a. so extension.
Eg.
You can also gcc–fpic–shared-o libhello.so hello.c
As an example:
Hello.h
hello.c
Main.c
Mr. Cheng dynamic Link Library
Then compile the link main.c
But putting the-lhello behind the file name is no problem, and a main execution file is generated:
The following issues occurred while running the execution file:
The reason may be that the program runs without knowing the path to which the dynamic library is located, so it is not found naturally.
Workaround:
Copy the dynamic link library to the system share directory, or make a connection to the dynamic link library under the System share directory (both hard and symbolic connections can be used, often symbolic connections).
The Ldconfig is intended to allow the dynamic link library to be shared with the system.
After the dynamic link library is shared by the system, run main and the results are as follows:
Using the GCC compiler to generate a static link library
Use an example to illustrate how to generate a static link library in the GCC compiler
Hello.h
hello.c
Main.c
First gcc–c hello.c file generation hello.o
Then use HELLO.O to generate a static link library, using
Ar–rc–o LIBHELLO.A hello.o
Or use AR rcs–o libhello.a hello.o
Then use the static link library to compile the link main.c
Using the GCC compiler to generate dynamic-link libraries and static-link libraries