Compile the static library and connect to the static library, compile the static library connection
Introduction: The following example describes how to compile a simple sum static library libplus. a in linux and then use it.
$ mkdir 1;cd 1$ pwd/home/nbz/1$ cat >plus.cint plus(int i0, int i1){return i0 + i1;}$ cat >plus.hint plus(int i0, int i1);$ gcc -c -fPIC -o plus.o plus.c$ cat >main.c#include "plus.h"int main(){return plus(2, 3);}$ lsmain.c plus.c plus.h plus.o$ ar -r libplus.a plus.oar: creating libplus.a$ rm plus.o$ lslibplus.a main.c plus.c plus.h$ gcc -L. -o main main.c -lplus$ lslibplus.a main main.c plus.c plus.h$ ./main$ echo $?5$$ rm main$ lslibplus.a main.c plus.c plus.h$ gcc -o main main.c libplus.a$ lslibplus.a main main.c plus.c plus.h$ ./main$ echo $?5$
In Linux, how does one call multiple static library files when using g ++ to compile and connect programs?
In the compilation command line, place the used static library file behind the source file. For example:
Gcc-L/usr/lib myprop. c libtest. a libX11.a libpthread. a-o myprop
-L/usr/lib specifies the path for searching the library file. By default, the compiler searches for the specified library file in the current directory.
When I compile A static library, I rely on B static library. The program I compile depends on A static library and B static library. In this way, does B static library have duplicates?
The static library is copied to the program at the link stage of the program.
You use the word dependency.
Program compilation usually requires preprocessing, compilation, compilation, and linking.
In the link step, the static library will be copied.
Because the static library is copied to the program in the Link stage of the program, it does not matter when the program is running.
How many links are there?
Then, it will be copied in a few minutes.