Use the GCC compiler to generate dynamic and static link libraries. The gcc compiler
Reprinted please mark: http://www.cnblogs.com/winifred-tang94/
1. compilation process
Gcc-fPIC-c xxx. c
-FPIC indicates the target code that notifies the gcc compiler to generate a location independent. The link is not copied.
2. Link Process
Gcc-shared-o libxxx. so xxx. o
After compiling the link, you can generate a dynamic link library with the extension of. so.
Eg.
You can also use gcc-fPIC-shared-o libhello. so hello. c
For example:
// Hello. h
// Hello. c
// Main. c
Mr Cheng Dynamic Link Library
Then compile the link main. c.
However, if you put-lhello behind the file name, there is no problem and a main execution file is generated:
The following problems occur when running the execution file:
The reason may be that the path of the dynamic library is not known when the program runs, so it is not found.
Solution:
Copy the dynamic link library to the system shared directory, or create a connection for the dynamic link library under the system shared directory (both hard connection and symbolic connection can be used as common symbolic connections ).
Ldconfig is used to share the dynamic link library with the system.
After the dynamic link library is shared by the system, run main. The result is as follows:
Use the GCC compiler to generate a static Link Library
An example shows how to generate a static Link Library in the gcc compiler.
// Hello. h
// Hello. c
// Main. c
First, the gcc-c hello. c file generates hello. o
Then, use hello. o to generate a static Link Library.
Ar-rc-o libhello. a hello. o
Alternatively, use ar rcs-o libhello. a hello. o
Then use the static link library to compile the link main. c