Compiling and using Linux static libraries

Preface:

The nature and working mode of library files in both Linux and Windows are the same, except that the corresponding file formats and file suffixes for libraries on different platforms are different. The library called in the program is a static library.
There are two purposes to use libraries in a project. One is to make the program simpler without maintaining too many source files in the project. The other is to keep the source code confidential. After all, not everyone wants to open source their own programs.

Static library:

Static libraries on Linux are generated by the program ar.

stay Linux Medium static library to lib Prefix with.a As a suffix, the name of the library in the middle can be specified by itself, that is: libxxx.a
 stay Windows Medium static libraries are typically lib Prefix with lib As a suffix, the name of the library in the middle needs to be specified by yourself, that is: libxxx.lib

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

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

  1. The source file needs to be assembled to get it. o file, parameter-c is required.
# Do the following to generate binary by default. o File
# -c parameter position not required
 gcc source file(*.c) -c	
  1. What you'll get. o Package to get a static library.
 ar rcs Name of static library(libxxx.a) Raw material(*.o)
  1. Publish static libraries
# Publish static libraries
	1. Provide header file **.h
	2. Provides a static library of authoring libxxx.a

Examples of static library production:

There are the following source files in a directory to implement a simple calculator

# Directory structure add. C div.c mult. C sub.c -> algorithm source file, function declaration in header file head.h
# main.c is a test program for the interface, making libraries without main.c counts in
.
├── add.c
├── div.c
├── include
│   └── head.h
├── main.c
├── mult.c
└── sub.c

The source code is as follows:

Addition calculation source file add.c:

Addition calculation source file 
#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 calculates the source file mult.c:

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

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

The source file div.c for subtraction calculation:

#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 = 20;
    int b = 12;
    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;
}

Static library generation:
Step 1: Add the source file. C, div.c, mult. C, sub.c assemble to get the binary target file add.o, div.o, mult.o, sub.o

# 1. Generation. o
$ gcc add.c div.c mult.c sub.c -c
sub.c:2:18: fatal error: head.h: No such file or directory
compilation terminated.

# Prompt header file not found, add parameter-I to restart header file path
$ gcc add.c div.c mult.c sub.c -c -I ./include/

# Check to see if the target file has been generated
$ tree
.
├── add.c
├── add.o            # Target File
├── div.c
├── div.o            # Target File
├── include
│   └── head.h
├── main.c
├── mult.c
├── mult.o           # Target File
├── sub.c
└── sub.o            # Target File

Step 2: Package the generated target file with the ar tool to generate a static library

# 2. The target file that will be generated. o Packaged as a static library
$ ar rcs libcalc.a a.o b.o c.o    # a.o b.o c.o can be written in the same directory as *. O

# View files in directory
$ tree
.
├── add.c
├── add.o
├── div.c
├── div.o
├── include
│   └── `head.h  ===> Publish with static library
├── `libcalc.a   ===> Generated static library
├── main.c
├── mult.c
├── mult.o
├── sub.c
└── sub.o

Step 3: The static library libcalc that will be generated. A and the header file corresponding to the library. H Publish it to the user at the same time.

# 3. Publish static libraries
	1. head.h    => Function declaration
	2. libcalc.a => Function Definition(Binary Format)

Use of static libraries

Once we have an available static library, we need to put it in a directory, write test code based on the resulting header file, and call the functions in the static library.

# 1. Get the published static library first
	`head.h` and `libcalc.a`
	
# 2. Place the static library, header file, and test program in a directory ready for testing
.
├── head.h          # Function declaration
├── libcalc.a       # Function Definition (Binary Format)
└── main.c          # Function Test

Compile the test program to get the executable.

# 3. Compile the test program main.c
$ gcc main.c -o app
/tmp/ccR7Fk49.o: In function `main':
main.c:(.text+0x38): undefined reference to `add'
main.c:(.text+0x58): undefined reference to `subtract'
main.c:(.text+0x78): undefined reference to `multiply'
main.c:(.text+0x98): undefined reference to `divide'
collect2: error: ld returned 1 exit status
  1. Correct Compiler
# 4. Specify library information at compile time
	-L: Specify the header directory where the library is located(Relative or absolute path)
	-l: Specify the name of the library, Head Pinch(lib)Tail removal(.a) ==> calc
# -L-l, there can be spaces between parameters and parameter values or -L./ -lcalc
$ gcc main.c -o app -L ./ -l calc

# Looking at the catalog information, we found that the executable has been generated
$ tree
.
├── app   		# Generated executable
├── head.h
├── libcalc.a
└── main.c

Keywords: Linux Operation & Maintenance bash

Added by decessus on Tue, 04 Jan 2022 21:14:44 +0200