Generate and use static libraries in Linux and generate static libraries in linux
Generate and use static libraries in Linux I. Static library concept
1. The Library is a collection of pre-compiled object files that can be linked to the program. Static libraries are suffixed with ". a" special archive file storage.
2. The standard system library can be found in/usr/lib and/lib. For example, in Unix-like systems, the C language database is generally stored as a file/usr/lib/libm.. The prototype Declaration of the function in this library is in the header file/usr/include/math. h.
3. The C standard library is stored as/usr/lib/libc. a, which contains functions specified by the ANS1/ISO standard, such as printf. For every C program, libc. a is linked by default.
4. Example:
Call the sin function in libm. a mathematical library in the tiger. c program.
#include<stdio.h>#include<math.h>int main(){ double x = 2.0; double y = sin(x); printf(“the result:%f\n”,y); return 0;}
If you use gcc tiger. c directly, the following error occurs:
Undefined reference to 'sin ';
1> the sin () function is not defined in this program and is not in the default library 'libc. A'. Unless specified, the compiler will not link 'libm. '.
2> to enable the compiler to link sin () to the main program 'test. C', a mathematical library 'libm. A' is required '..
3> usage:
Gcc tiger. c/usr/lib/libm. a-o tiger
Then you can compile it. To avoid specifying a long path in the command line, the compiler provides the quick option "-l" for the linked function library ". Therefore, you can use the following method:
Gcc tiger. c-lm-o tiger
Note: Option-lNAME uses the connector to try to link the library file libNAME. a In the system library directory.
Ii. generate and use static databases
1. The static library is a collection of obj files. Generally, the static library is suffixed with ".. The static library is generated by the program ar.
2. the advantage of the static library is that you can re-link the program without re-compiling the library code. This method saves the compilation process time (when compiling a large program, it takes a long time ). Another advantage of the static library is that developers can provide library files to users without opening source code, which is often used by library function providers.
3. Use an instance to learn how to generate and use a static database.
First generate a static library
1> the test folder contains three files: main. c, tiger. c, and tiger. h;
Content in the. main. c file:
# Include <stdio. h> # include "tiger. h "// note that include this file is not just chained. file a can be int main (void) {printf ("sum = % d \ n", add (3, 5); return 0 ;}
B. Content in the tiger. h file:
#ifndef __TIGER__#define __TIGER__int add(int a,int b);#endif
C. Content in the tiger. c file
int add(int a,int b){ return a+b;}
2> the basic step for creating a static database is to generate the target file tiger. o.
Gcc-o tiger. o-c tiger. c
3> Generate the static library libadd.:
Ar-rcs libadd. a tiger. o
Second, use the static library
1> use the gcc command to add the library file.
Gcc-o main. c libadd.
2> You can also run the "-l database name" command. The database name is a string that does not contain the library function library and extension.
Gcc-o main. c-ladd
After the preceding command is executed, the system returns:
Cannot find-ladd
Note: The above command will find the add function library in the default path of the system, and link it to the target program to be generated. The system prompts that the library file add is not found. This is because the add library function is not in the default search path of the system. We need to consider the path of the specified library function, for example: the library file is in the same directory as the current compilation file: gcc-o main. c-L. /-ladd
The system can generate executable files normally.
Note:
When the-l option is used, the objective name of the-o option must be before the library name of the-l link; otherwise, gcc considers-l as the generated destination and fails.
Original article address: generate and use static libraries in linux
Linux static library usage Problems
Gcc main. c static_lib.a-o main
Static library usage in linux
There should be no difference. It should be a problem with your code. The static library is finally linked to an executable file during the compilation process, basically no difference from calling your own program.