GCC generates static library. a and dynamic library. so

catalogue

1, Static and dynamic libraries

  1.1 static library

1.2 dynamic library  

2, GCC generates static and dynamic libraries

2.1 preparation process

 ​

  2.2 use of static library

  2.3 use of dynamic library

  2.4 comparison between static library and dynamic library

  3, Usage examples of Library

1. Code:

2. Generate static library  

  3. Generate dynamic library

1, Static and dynamic libraries

  1.1 static library

  Static libraries are static link libraries (. lib under Windows,. a under Linux and Mac). It is called static because the static library will be copied directly to the target program when compiling, and this code will not be changed in the target program.
The benefits of static libraries are obvious. After compilation, the library files actually have no effect. The target program has no external dependencies and can run directly. Of course, its disadvantage is also obvious, that is, it will increase the volume of the target program.

1.2 dynamic library  

Dynamic library is dynamic link library (dll under Windows,. so under Linux,. dylib under Mac). In contrast to static libraries, dynamic libraries are not copied to the target program at compile time, and only references to dynamic libraries are stored in the target program. The dynamic library will not be loaded until the program runs.

2, GCC generates static and dynamic libraries

2.1 preparation process

Prepare three c language files, namely hello.h, hello.c and main.c. write three files using vim. At the same time, create a folder test1 to store the files. The code is as follows.

mkdir test1
cd test1

 

The three file codes are as follows:

hello.h

#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif//HELLO_H

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;
}

Then compile with gcc

gcc -c hello.c
ls

  2.2 use of static library

1. Create a static library

The naming convention of static library file name is to prefix lib and expand it to. A. Create a static library with the ar command.

ar -crv libmyhello.a hello.o

At the same time, enter ls to view the file  

As a result, the creation was successful.  

  2. Use static library in program

Enter code:

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

  Note: for custom static libraries, main.c can also be placed between - L. and - lmyhello, otherwise myhello is not defined.
-50. : indicates that the connected library is in the current directory

  3. Verify the characteristics of static library
When the static library is deleted, run the executable and find that the program is still running normally, indicating that the static library has no connection with program execution. At the same time, it also shows that the static library is connected to the code when the program is compiled.

  

  2.3 use of dynamic library

1. Create dynamic library

The dynamic library file naming specification takes lib as the prefix and the extended name is. so

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

shared: specifies to generate a dynamic link library, which cannot be omitted
-fPIC: refers to code compiled as location independent and cannot be omitted

  2. Use dynamic library in program

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

perhaps

gcc main.c libmyhello.so -o hello

Then when we run the hello file, we will find an error

Solution to the problem: copy libmyhello.so to the directory / usr/lib. Because the library file is found in / usr/lib at runtime.

sudo mv libmyhello.so /usr/lib

 

  2.4 comparison between static library and dynamic library

gcc compiles the. o file gcc -c hello.c
Create a static library ar -crv libmyhello.a hello.o
Create the dynamic library gcc -shared -fPIC -o libmyhello.so hello.o
Use the library to generate the executable file gcc -o hello main.c -L. -lmyhello
Executable. / hello  

  3, Usage examples of Library

1. Code:

sub1.c

float x2x(int a,int b)
{
	float c=0;
	c=a+b;
	return c;
}

sub2.c

float x2y(int a,int b)
{
	float c=0;
	c=a/b;
	return c;
}

 sub.h

#ifndef SUB_H
#define SUB_H
float x2x(int a,int b);
float x2y(int a,int b);
#endif

main.c

#include<stdio.h>
#include"sub.h"
void main()
{
	int a,b;
	scanf("%d",&a);
	scanf("%d",&b);
	printf("a+b=%.2f\n",x2x(a,b));
	printf("a/b=%.2f\n",x2y(a,b));
}

  Then compile gcc -c sub1.c sub2.c with gcc

2. Generate static library  

ar crv libsub.a sub1.o sub2.o

 

Then connect the file GCC - O main. C libsub. A and execute the file

  3. Generate dynamic library

gcc -shared -fPIC libsub.so sub1.o sub2.o
gcc -o main main.c libsub.so

  4. Comparison of file size between static library and dynamic library

Enter the size command to view the file sizes of static and dynamic libraries

  The results show that the files of the dynamic library are larger.

Tags: C++ Linux Windows

Posted on Thu, 07 Oct 2021 21:31:27 -0400 by jebster