Static library generation and use under Linux

  

A. Static library concept

1. A library is a collection of pre-compiled object files that can be linked into a program. The static library suffix is ​​a special archive file storage of ”.a”.

2. The standard system library can be found in the directories /usr/lib and /lib. For example, in a Unix-like system, the C-order sequence library is generally stored as the file /usr/lib/libm.a. The prototype of the function in this library is declared in the header file /usr/include/math.h.

The 3.C standard library itself 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. An example:

Call the sin function in a math library libm.a 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\ ” , y); return 0;} 


If you use gcc tiger.c directly, you will get an error:

undefined reference to ‘sin’;

1> The function sin() is not defined in the program nor in the default library & rsquo;libc.a’ unless specified, the compiler will not link ’libm.a’.

2> In order for the compiler to link sin() into the main program ‘test.c’, you need to provide the math library & rsquo;libm.a’.

3> How to use:

gcc tiger.c /usr/lib/libm.a -o tiger

Then you can compile it. To avoid specifying long paths on the command line, the compiler provides a quick option for the linked library "ld";-l”. So you can use the following method:

gcc tiger.c -lm –o tiger

Note: The option -lNAME uses the linker to try to link the library file libNAME.a in the system library directory. .


2. Generate and use static libraries

1. Static libraries are a collection of obj files, usually static libraries are suffixed with ”.a”. The static library is generated by the program ar.

2. The advantage of static library is that you can re-link the program without recompiling the library code. This method saves the compilation process time (when compiling large programs, it takes cost For a long time). Another advantage of static libraries is that developers can provide library files to the people they use, without the need for open source code, which is a common method used by library function providers.

3. Use an example to understand how to generate static libraries and use static libraries.

First generate static libraries

1> There are three files in the test folder: main .c , tiger.c, tiger.h;

a. The contents of the main.c file:

#include<stdio.h>#include”tiger.h” //Note Include this file, not just link the .a file to int main(void){ printf(“sum=%d\ ”,add(3,5)); return 0; } 

b.tiger What's in the .h file:

#ifndef __TIGER__#define __TIGER__int add(int a,int b);#endif

Content in the c.tiger.c file

int add (int a, int b){ return a+b;}

2> The most basic step in creating a static library is to generate the target file tiger.o

gcc -o tiger.o -c tiger.c

3> Then generate static library libadd.a:

ar -rcs libadd.a tiger.o

Secondly use static library

1> Gcc command with the library file is OK

gcc -o main main.c libadd.a

2> You can also use the command ”-l library name” OK, the library name does not contain the string libraries and library extensions.

gcc -o main main.c -ladd

After the above command is executed, the system returns:

Cannot find –ladd

Explanation: The above command will look for the add function library in the system default path Xi'an 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 system's default search path. We need to think of the path of the specified library function. For example, the library file and the current compiled file are in the same directory: gcc -o main main .c -L ./-ladd

The system can generate executable files normally.

Description:

When using the -l option, the destination name of the -o option should be before the library name of the -l link, otherwise gcc will think that -l is the target of the build and an error occurs.

Copyright © Windows knowledge All Rights Reserved