Preface:
In the previous lesson, we introduced a simple introduction to GCC, including the GCC compilation steps: preprocessing, compilation, assembly, and link. In this lesson, you are welcome to join us in the Linux library.
Principle:
In Linux, there are two types of libraries: static library and shared library. The static library is. files ending with the suffix A are usually compiled to generate executable files at the specified static library location during the compilation link stage, therefore, the generated executable file does not require the participation of the static library during execution. The dynamic library sharing library is. files ending with the so suffix are usually used in the Code where the shared library is needed. In the compilation link stage, specify the path of the shared library to read the code from the memory table when executing executable files, only one program is shared in the memory. Therefore, the shared library must be involved. If the shared library cannot be found during the runtime, an error is returned. The default paths for both library systems are/usr/lib/,/lib, and/usr/local/lib. This also depends on the specific system version, it can also be divided into 32-bit and 64-bit paths. The following is a specific example.
Practice:
Follow these steps to create a file:
1> Create a calc. h file and enter the following code:
#ifndef CALC_H#define CALC_Hdouble Add(double a,double b);#endif
2> Create the Calc. c file and enter the following code:
#include <stdio.h>#include "calc.h"double Add(double a,double b){ return a + b; }
3> Create the calcmain. c file and enter the following code:
#include <stdio.h>#include "calc.h"int main(void){ double c = Add(2.0,3.0); printf("result:%f",c); return 0; }
4-1> Generate a static database and execute the command as follows:
Gcc-wall Calc. c-o Calc. O gcc-wall calcmain. c-o calcmain. oar RCS libcalc. A calc. O // use the AR command to generate the static library gcc-wall calcmain. O libcalc. a-O calc // generate the calc executable file,
4-2> generate dynamic shared library for execution. The command is as follows:
// Because calc has been generated before. O and calcmain. O 2 target files gcc-shared-FPIC Calc. o-O libcalc. so // use GCC to generate a shared library, where-shared: Shared Library;-FPIC: location-independent GCC-wall calcmain. o-O calc-L. -lcalc //-L.: Search for the current directory;-L: Use the external lib library.
Export LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATH // specify the temporary shared library search path, above-L. if the parameter is invalid after the experiment, I do not know why. By default, the path searched by the shared library will not search for the current path.
The above are the related steps of this practice. During the GCC compilation process, you can directly generate the target file with the source code C file and ignore it again! In this course, the knowledge points of the static library are relatively less relevant and currently the development is also less useful. For more information about the shared library, see the blog article here.