1, Generating static and dynamic libraries with gcc under Ubuntu
Function library is divided into static library and dynamic library.
Static library
When the program is compiled, it will be connected to the object code, and the existence of static library is not required when the program is running.
Dynamic library
When the program is compiled, it is not connected to the object code, but loaded when the program runs.
The difference between the two: the former is compiled and connected, and the latter is loaded by program running.
(1) Example (hello everyone)
1. Create a file
(1) test1. Create a folder
Create a folder with mkdir Command + file name, and enter the folder with cd + file name.
(2) . original code (three files in total)
Create a file, use the gedit Command + file name, and then paste the code in.
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; }
(3). gcc compiles the. o file
Compile command:
gcc -c hello.c
Check with ls command and you will find that a file with. o suffix is generated.
2. Creation and use of static library
(1) Create static library
Tools for creating static libraries: ar
Static library file naming specification: with lib as the prefix, it is a. A file
Compile command:
ar -crv libmyhello.a hello.o
(2) Using static libraries in programs
① Compile command:
gcc -o hello main.c -L. -lmyhello
For custom libraries, you can put main.c between - L. and - lmyhello, otherwise myhello is not defined.
-50. : indicates that the connected library is in the current directory
② Compile command:
gcc main.c libmyhello.a -o hello
③ Compile command:
gcc -c main.c//Mr. Cheng main.o gcc -o hello main.c libmyhello.a//Generate executable
3. Creation and use of dynamic library
(1) . create dynamic library
Tool for creating dynamic library: gcc
Dynamic library file naming specification: with lib as the prefix, it is a. so file
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) . execute dynamic library in program
gcc -o hello main.c -L. -lmyhello or gcc main.c libmyhello.so -o hello
Run the executable hello again, and an error will appear:
Solution: copy libmyhello.so to the directory / usr/lib,
sudo mv libmyhello.so /usr/lib
4. The dynamic library will be used preferentially
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
An error will be reported when executing the executable file. It can be seen that when the static library and dynamic library exist at the same time, the program will give priority to the dynamic library.
(2) Example 1 use library (A1, A2)
For detailed procedures, please refer to hello procedure
1. Original code
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 ```c #include<stdio.h> #include"A.h" int main() { print1(1); print2("test"); exit(0); }
2. Create file:
3. Use static library in program
gcc -c A1.c A2.c, generate. o file with. c file first
ar crv libfile.a A1.o A2.o, and then generate the static library
GCC - O test. C libfile. A, an error is reported here. It is found that the exit (0) in main.c is changed to return 0; that will do
After correction:
4. Use of dynamic library
gcc -shared -fPIC -o libfile.so A1.o A2.o
gcc -o test test.c libfile.so
sudo permission is required here
(3) Example 2 use library (addition, Division)
For detailed procedures, please refer to hello procedure
1. Original 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; printf("Please input the value of a:"); scanf("%d",&a); printf("Please input the value of b:"); scanf("%d",&b); printf("a+b=%.2f\n",x2x(a,b)); printf("a/b=%.2f\n",x2y(a,b)); }
2. File creation process:
3. Static library
gcc -c sub1.c sub2.c, generate. o file with. c file first
Then generate the static library, ar crv libsub.a sub1.o sub2.o
gcc -o main main.c libsub.a
4. Dynamic library
gcc -shared -fPIC -o libsub.so sub1.o sub2.o
gcc -o main main.c libsub.so
You need to move the libsub.so file to the usr/bin target directory, or an error may be reported
result:
2, Summary
Through the practice process of generating static library and dynamic library with gcc through three programs, I can skillfully generate static library and dynamic library. In the comparison of the two libraries, we can clearly see the difference between the two. Although some small problems were encountered in the process, they were solved quickly. As long as you practice more slowly, you will soon be able to master it. The executable file is obtained through the compilation link. Use the tool to compile the source code to get the. o file, and then link the. o file to get the executable file.
3, References
gcc generates static library. a and dynamic library. so
linux basic command -- text editing vim