gcc generates static library. a and dynamic library. so and their instance usage

Keywords: C Linux Ubuntu vim

1, Generating. a static library and. so dynamic library with gcc

1. Example 1 explanation of Hello program

① Create a directory and edit the generated sample programs hello.h, hello.c, and main.c

(1) Example program hello.h

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

(2) Example program hello.c

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

(3) Example program main.c

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

(4) Compile hello.c into an. o file

gcc -c hello.c


Tip: vim, nano, gedit, etc. are available for file editing

② gcc generates static libraries and uses

(1) Create static library
Static library file name specification format: lib is the prefix, followed by the static library name, and the extension is. A. Create a static library with the ar command.

ar -crv libmyhello.a hello.o

As shown in the figure:

(2) Using static libraries
Method 1: GCC - O Hello main. C - L - lmyhello

Tip: main.c can also be placed between - L. and - lmyhello, but not after them. Otherwise, myhello is not defined.

-50: Indicates that the connected library is in the current directory

As shown in the figure:
Method 2: gcc main.c libmyhello.a -o hello
As shown in the figure:

Method 3: Mr. Cheng main.c: gcc -c main.c
Regenerate into executable file: gcc -o hello main.o libmyhello.a
As shown in the figure:

(3) Check whether the common function is really connected to the target file hello
As shown in the figure:

It is found that after deleting the static library, the executable file is still executed, indicating that the public functions in the static library have been connected to the target file.

③ gcc generates dynamic libraries and uses

(1) Creating dynamic library files from. o files
Dynamic library file name specification format: lib prefix, followed by the dynamic library file name, and the file extension is. so. Create a dynamic library as a gcc command.

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

As shown in the figure:

Tip: - shared this option specifies to generate a dynamic connection Library (let the connector generate a T-type export symbol table, and sometimes a weak connection W-type export symbol). External programs cannot connect without this flag. Equivalent to an executable file.
-fPIC means that the compiled code is location independent. If this option is not used, the compiled code is location dependent. Therefore, during dynamic loading, the needs of different processes are met by code copying, but the purpose of real code segment sharing cannot be achieved.
(2) Using dynamic libraries

 gcc -o hello main.c -L. or gcc main.c libmyhello.so -o hello

Error in executing. / hello. Because the dynamic library file of the current directory is used during connection and the dynamic library file is found in / usr/lib during runtime, copy the file libmyhello.so to the directory / usr/lib.
As shown in the figure:

It is found that dynamic libraries are needed when the program is running.

④ When static library and dynamic library have the same name, the choice of gcc

(1) Delete all generated files and restore to the completion state of 3 subroutines.

(2) Create a static library file libmyhello.a and a dynamic library file libmyhello.so.

gcc -c hello.c
ar -ccr libmyhello.a hello.o 
gcc -shared -fPIC -o libmyhello.so hello.o


As shown in the figure:

Result: when the static library and the dynamic library have the same name, the gcc command will give priority to the dynamic library, and the dynamic libraries in / usr/lib and / lib directories will be disconnected by default.

⑤ Summary of static and dynamic libraries

Static library: the static library will be connected to the object code when the program is compiled, and the static library will no longer be needed when the program is running.
Dynamic library: the dynamic library is not connected to the object code when the program is compiled, but is loaded when the program is running. Therefore, the dynamic library is also required when the program is running.

2. Example 2

① Create program:
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");
return 0;
}


② Generation and use of static library. a files.

③ Generation and use of shared library. so file

3. Example 3

① Create program:

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;
	printf("Please enter a:");
	scanf("%d",&a);
	printf("Please enter b:");
	scanf("%d",&b);
	printf("a-b=%.2f\n",x2x(a,b));
	printf("a*b=%.2f\n",x2y(a,b));
}

As shown in the figure:

② Generate three. o files

gcc  -c sub1.c sub2.c main.c

③ Generate x2x and x2y target files into static libraries and use them

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

④ Generate x2x and x2y target files into dynamic libraries and use them

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

⑤ Compare the file size generated by static library and dynamic library

Static library size:

Dynamic library size:

Conclusion: the comparison shows that the static library file is smaller than the dynamic library file, and the difference between the generated executable files is small.

2, Experimental summary

This experiment is the second time to use gcc. Compared with the first time, I am much familiar with the operation of ubuntu. This experiment is mainly a verification process, so the operation is relatively simple, and I am familiar with the usage and differences between static library and dynamic library.

3, References

[1] Generation and use of static library. a and. so library files under Linux.
[2] Generate static and dynamic libraries with gcc.

Posted by outpost on Fri, 08 Oct 2021 03:49:23 -0700