Generation and use of static library under Linux

1, Static library concept

1. The library is a precompiled object file    files), which can be linked into the program. The static library is a special archive with Suffix ". A"   File) storage.

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

3. The C standard library itself is stored as / usr/lib/libc.a, which contains functions specified in the ANS1/ISO standard, such as printf. libc.a is linked by default for every C program.

In Linux, the static library takes lib as the prefix and. a as the suffix. In the middle is the name of the library. You can specify it yourself, that is, libxxx.a
In Windows, static libraries generally use lib as the prefix and lib as the suffix. In the middle is the name of the library, which needs to be specified, that is, libxxx.li

2, Creation of static library

To generate a static library, you need to assemble the source file (use the parameter - c) to get the target file in binary format (. o format), and then package the target file through the ar tool to get the static library file (libxxx.a).

The specific steps to generate a static link library are as follows:

1. You need to assemble the source file to get the. o file. You need to use the parameter - c

# -c parameter position is not required
$ gcc source file(*.c) -c    

2. Package the obtained. o to obtain the static library

$ ar rcs Name of static library(libxxx.a) raw material(*.o)

3. Publish static library

# Publish static library
    1. Provide header file **.h
    2. Provide the static library produced libxxx.a

3, Static library example

one   Prepare test procedure

Add calculation source file add.c:

#include <stdio.h>
#include "head.h"

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


Subtraction calculation source file sub.c:

#include <stdio.h>
#include "head.h"

int subtract(int a, int b)
{
    return a-b;
}


Multiplication calculation source file mult.c:

#include <stdio.h>
#include "head.h"

int multiply(int a, int b)
{
    return a*b;
}


Source file for division calculation div.c:

#include <stdio.h>
#include "head.h"

double divide(int a, int b)
{
    return (double)a/b;
}


Header file head.h:

#ifndef _HEAD_H
#define _HEAD_H
// addition
int add(int a, int b);
// subtraction
int subtract(int a, int b);
// multiplication
int multiply(int a, int b);
// division
double divide(int a, int b);
#endif

Test file main.c:

#include <stdio.h>
#include "head.h"

int main()
{
    int a = 11;
    int b = 28;
    printf("a = %d, b = %d\n", a, b);
    printf("a + b = %d\n", add(a, b));
    printf("a - b = %d\n", subtract(a, b));
    printf("a * b = %d\n", multiply(a, b));
    printf("a / b = %f\n", divide(a, b));
    return 0;
}


1.2 generate static library

Step 1: assemble the source files add. C, div.c, mult. C and sub.c to obtain the binary object files add. O, div.o, mult. O and sub.o

# 1. Generate. o
$ gcc add.c div.c mult.c sub.c -c


# Prompt: if the header file cannot be found, add the parameter - I and restart the header file path
$ gcc add.c div.c mult.c sub.c -c -I ./include/

# Check whether the target file has been generated
$ ls

  Step 2: package the generated target file through ar tool to generate a static library

# 2. Package the generated target file. o into a static library
$ ar rcs libmytest.a add.o div.o mult.o sub.o    # add.o div.o mult.o sub.o can be written as *. O in the same directory

# View files in directory
$ ls

Step 3: publish the generated static library libcalc.a and the corresponding header file head.h

# 3. Publish static library
	1. head.h    => Function declaration
	2. libcalc.a => Function definition(Binary format)


Use of static libraries

Compile test program main.c
$ gcc main.c -o test
/tmp/ccF94vUK.o: In function'main'Medium:
main.c:(.text+0x3a): yes'add'Undefined reference
main.c:(.text+0x5c): yes'subtract'Undefined reference
main.c:(.text+0x7e): yes'multiply'Undefined reference
main.c:(.text+0xa0): yes'divide'Undefined reference
collect2: error: ld returned 1 exit status
Above error analysis:

The compiled source file contains header files head.h, The definition corresponding to the function declared in the header file (that is, the function body implementation) is in the static library. The program does not find the function implementation when compiling, so the prompt is yes'xxxxxx'Undefined reference.

J Solution: specify the path and name of the static library when compiling

-L: Specify the directory where the library is located (Relative or absolute path)
-l: Specify the name of the library. Pinch the head (lib) Tail removal (.a) The rest is the name of the required static library


#  Specify library information when compiling
	-L: Specify the directory where the library is located(Relative or absolute path)
	-l: Specifies the name of the library, Pinch head(lib)Tail removal(.a) ==> mytest
# -L -l, there can be spaces between parameters and parameter values, or there can be no - L./ -lmytest
$ gcc main.c -o test -L ./ -l mytest

# Check the directory information and check whether the executable program is generated
$ ls

# Execute the generated executable
$ ./test
a = 20, b = 12
a + b = 32
a - b = 8
a * b = 240
a / b = 1.666667

Tags: Linux Ubuntu

Posted on Tue, 05 Oct 2021 15:33:18 -0400 by tex1820