First, the static library file is packaged into multiple target files. In windows, the static library file suffix is. in linux, the static library file suffix is. a (a is short for archive, that is, document files ).
To put it bluntly, go to the topic.
addition ( a , result=a+ }
Multiplication. c
result=a* }
$ gcc - $ gcc -c multiplication.c
Among them, option-c enables gcc to compile only the source code into a. o file.
The current folder contains two. o files.
addition.c multiplication.c addition.o multiplication.o
$ ar cr libarith.a addition.o multiplication.o
Cr indicates create, and a static library file libarith. a is created.
Before using the library file, you must first create a header file header. h, which contains the declaration of functions implemented in the library file.
#include<stdio.h> addition( a, multiplication( a, b);
Then create a c program example. c to call the function.
#include result=addition(, printf( result=multiplition(, printf( }
Compile example. c
$ gcc example.c libarith.a -o example
The parameter after option-o specifies the executable file name generated by the compiler. If-o is not added, the default executable file name generated is a. out.
Run example
$./ addtion result : multiplication result :
multiplition.o
If you want to view which functions are included in the library file, you can use nm libarith. a to view which libraries need to be called by the executable file, you can use lld example to view them.
$ cp libarith.a file/ multiplition.o
Suppose you already have a target file subtraction. o
subtraction.o
If the subtraction. o file already exists in the library file, it overwrites the old one with a new one.
subtraction.o
Reference Source: http://www.thegeekstuff.com/2010/08/ar-command-examples/