gcc generates static libraries (. a) and dynamic libraries (. so)

This paper mainly describes how gcc generates static library (. a) and dynamic library (. so) to help us better carry out embedded programming. Because sometimes security is involved, static libraries or dynamic libraries may be provided for us to use.

System environment: Ubuntu Desktop 18.04

1, What are static and dynamic libraries

We usually need to make some public functions into function libraries for other programs. Function libraries are divided into static libraries and dynamic libraries.

The static library will be connected to the object code when the program is compiled. The static library is no longer needed when the program is running.

Dynamic libraries are not connected to the object code when the program is compiled, but are loaded when the program is running. In this way, we can dynamically change some functions of the program by changing the dynamic library.

Under Linux, the ar tool is used to compress the target files together, number and index them for easy search and retrieval.

2, gcc generates. a static library and. so dynamic library

1. Generate static library (. a)

1.1 edit and generate example programs hello.h, hello.c and main.c

hello.h

#ifndef HELLO_H / / if the source file is not defined, compile the following code
#define HELLO_H / / define macro
void hello(const char *name);
#endif/HELLO_ End of H / / ifndef

hello.c

#include<stdio.h>
void hello(const char *name)
{
	printf("Hello %s!\n",name);
}

main.c

#include "hello.h"
int main()
{
    hello("everyone");
    return 0;
}

1.2 compile hello.c into. o file

Both static and dynamic libraries are created from. o files. Therefore, we must compile the source code hello.c into an. o file through gcc and use the command under the Linux system terminal

gcc -c hello.c

To make sure we get the. o file, use the ls command

1.3 creating static libraries from. o files

The command specification of the static library file name is prefixed with lib, followed by the static library name with the extension. A. for example, if the static library name we will create is hello, the static library file name is libhello.a. To create a static library under Linux system, you need to use the ar command and enter the following command on the terminal

ar -crv libmyhello.a hello.o

Similarly, we can use the ls command to view the results.

1.4 using static libraries in programs

When the static library is finished, how to use its internal functions? You only need to include the prototype declaration of these common functions in the source program using these common functions, and then specify the static library name when generating the object file with gcc command.

Method 1:

Enter the following command at the terminal:

gcc -o hello main.c -L. -lmyhello

For custom libraries, main.c can also be placed between - L. and - lmyhello, but not behind them. Otherwise, it will prompt that myhello is not defined, but it is a system library, such as g++ -o main (-L/usr/lib) -lpthread main.cpp.

Method 2:

gcc main.c libmyhello.a -o hello

Method 3:

Mr. Cheng main.o

gcc -c main.c

Regenerate to executable:

gcc -o hello main.o libmyhello.a

1.5 verify the characteristics of static library

Next, when we delete the static library, run the executable file and find that the program is still running normally, indicating that the static library is not related to program execution. We use the rm command to delete the libmyhello.a file, and then execute the hello program.

2. Generate dynamic library (. so)

2.1 creating dynamic library files from. o files

The dynamic library file name naming specification is similar to the static library file name naming specification. The prefix lib is added to the dynamic library name, but its file extension is. So. For example, if the dynamic library we will create is named myhello, the file name of the dynamic library is libmyhello.so. Enter the following command on the terminal to get the dynamic library file libmyhello.so.

gcc -shared -fPIC -o libmyhello.so hello.o

2.2 using dynamic library in program

Using a dynamic library in a program is the same as using a static library. It also includes the declaration of these public functions in the source program using these functions, and then indicates the name of the dynamic library for compilation when generating the target file with the gcc command. Enter the following command at the terminal

gcc -o hello main.c -L. -lmyhello

Alternatively, you can use the command

gcc main.c libmyhello.so -o hello

At this time, no error will be reported (if libmyhello.so is not available), but an error will be prompted when running the program, because although the dynamic library of the current directory is used during connection, the library file will be found in the / usr/lib directory during runtime. You can copy the file libmyhello.so to the directory / usr/lib, so that there will be no errors.

Enter the following command on the terminal to move the libmyhello.so file to the / usr/lib directory

sudo mv libmyhello.so /usr/lib

You can see that the execution is successful.

If dynamic library and static library exist at the same time, we can find that we will give priority to using dynamic library through experiments.

3, Instance

1. Example 1

1.1 code

The code is as follows:

A1.c

#include<stdio.h>
void print1(int arg){
    printf("A1 print arg:%d\n",arg);
}

A2.c

#include<stdio.h>
void print2(char *arg){
	printf("A2 printf arg:%s\n",arg);
}

A.h

#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif

test.c

#include<stdlib.h>
#include "A.h"
int main(){
    print1(1);
    print2("test");
    exit(0);
}

1.2 generation and use of static library. a files

First, generate the. o file and enter the following command at the terminal

gcc -c A1.c A2.c

Next, generate the static library. a file, and enter the following command at the terminal

ar crv libafile.a A1.o A2.o

Finally, use the. a library file to create an executable program (PS: if this method is adopted, ensure that the generated. a file and. c file are saved in the same directory, that is, both in the current directory). Enter the following command at the terminal

gcc -o test test.c libafile.a
./test

1.3 generation and use of dynamic library. so file

The first is to generate the target file (. o). You must add "- fpic" (small mode, less code) to generate the. o file here, otherwise an error will be reported when generating the. so file. Enter the following command at the terminal

gcc -c -fpic A1.c A2.c

The next step is to generate the shared library (dynamic library). so file

gcc -shared *.o -o libsofile.so

Use the. so library file to create an executable program

gcc -o test test.c libsofile.so
./test

An error will be reported at this time. This is because the Linux system only looks for. So files in / lib and / usr/lib directories, so it is necessary to copy the corresponding. So files to the corresponding path. Enter the following command at the terminal

sudo cp libsofile.so /usr/lib

Then execute the test program to run successfully.

At the same time, it can be used directly

gcc -o test test.c -L. -lname

To use the corresponding library file, where

-50. : indicates that in the current directory, you can define the path by yourself, that is, use - Lpath.

-lname: name is the name of the corresponding library file (excluding lib), that is, if libafile.a is used, name is afile; To use libsofile.so, the name is sofile.

2. Example 2

2.1 code

sub1.c

float x2x(int x1,int x2)
{
    return (float)(x1*x2);
}

sub2.c

float x2y(int x1,int x2)
{
    return (float)(x1/x2);
}

sub.h

#ifndef SUB_H
#define SUB_H
float x2x(int x1,int x2);
float x2y(int x1,int x2);
#endif

main.c

#include<stdio.h>
#include "sub.h"
int main()
{
    int x1,x2;
    scanf("%d %d",&x1,&x2);
    printf("x1*x2=%f\n",x2x(x1,x2));
    printf("x1/x2=%f\n",x2y(x1,x2));
    return 0;
}

2.2 generation and use of static library. a files

Enter the following commands at the terminal in sequence

gcc -c sub1.c sub2.c
ar crv libsub.a sub1.o sub2.o
gcc -o main main.c libsub.a
./main
4 2

2.3 generation and use of dynamic library. so file

Enter the following commands at the terminal in sequence

gcc -shared -fpic -o libsub.so sub1.o sub2.o
sudo cp libsub.so /usr/lib
gcc -o main mian.c libsub.so
./main
4 2

summary

Through the above three examples, you can basically skillfully use gcc to generate static libraries and dynamic libraries, and learn relevant knowledge about static libraries and dynamic libraries. Similar to the previous experiment, Mr. Cheng creates an. o file, and then connects the. o file to the main program to generate an executable file. The difference is that there was only a single file before, and multiple files were connected in this experiment. When developing some projects in the future, if only static library or dynamic library is provided, we can use the knowledge of this experiment to call public functions and generate executable files.

Keywords: C Linux Ubuntu

Added by Peggy on Thu, 07 Oct 2021 19:53:42 +0300