Hi translation of C language - static library and dynamic library (8)

1. When encrypting files, you need to first compile the source file into a target file.

gcc -c 目标文件.c

2. Create an archive containing multiple .o files in the library directory
(1) Enter the lib directory in MinGW. The one with the suffix .a is the archive.
(2) Use the ar command to create an archive

ar -rcs libhfsecurity.a encrypt.o checksum.o

-r means to update the .a archive if it exists
-c means not to display feedback when creating the archive.
-s tells ar to index at the beginning of the .a archive.
.a is the newly created .a archive
. .o file is the file saved in the .a archive
. 3. All .a file names are in the form of libXXX.a. This is the standard way to name archives. The archives are static libraries.
4. Create a library archive so that you can use it in other programs. After installing the archive to a standard directory, you can compile the code with the -l switch.

gcc test_code.c -lhfsecurity -o test_code

If you want to put the archive somewhere else, use the -L option

gcc test_code.c -L/my_lib -lhfsecurity -o test_code

5. The program is composed of different codes. First create the .o file and the .a archive, and then link them into an executable program. After the program is linked, it becomes a large piece of object code, and there is no way to separate the display code and sensor code. Because they are statically linked together when compiling the program.
6. The core of the dynamic library is a piece of object code; the dynamic library consists of some additional information, which the operating system needs to use to link the library to the program; the dynamic library is created by one or more .O files.
7. Dynamic library creates target file

gcc -I/includes -fPIC -c hfcal.c -o hfcal.o

-fPIC tells gcc to create position-independent code.
8. Position-independent code is code that can run no matter where the computer loads it in memory. It can be moved around in memory.
The Windows operating system uses a technology called memory mapping when loading dynamic libraries.
9. Dynamic libraries are called the same for each platform. The suffix names are different, but the creation method is the same.
Windows: dynamic link library, the suffix is ​​.dll.
Linux and Unix: shared object files, the suffix name is .so.
Mac: dynamic library, the suffix name is .dylib.

gcc -shared hfcal.o -o C:\libs\hfcal.dll(Windows上的MinGW)
gcc -shared hfcal.o -o /libs/libhfcal.dll.a(Windows上的cygwin)
gcc -shared hfcal.o -o /libs/libhfcal.so
gcc -shared hfcal.o -o /libs/libhfcal.dylib

The -shared option tells gcc to convert the .o object file into a dynamic library.
Note: Once the library is compiled under a certain name, the file name cannot be modified.
On some ancient Mac systems, there is no -shared option.
10. When the dynamic library is created, it can be used like a static library.
11. If you compile the program with MinGW, you need to determine the environment before running it.
Insert image description here

12. Compile the new version of dynamic library

gcc -c -fPJC hfcal_UK.c -o hfcal.o           //把源文件编译为目标文件
gcc -shared hfcal.o -o /usr/local/lib/libhfcal.so     //把目标文件转换为共享目标文件

13. When the dynamic library and header files are installed in the standard directory, there is no need to use the -l standard when compiling, and there is no need to set the LD_LIBRARY_PATH variable when running the code.
14. When I want to change the name of the dynamic library, I rename the file name, but the compiler cannot find it: the compiler
will save the library name in the file when compiling the dynamic library. If the file is renamed, the name in the file has not changed. If the name of the dynamic library is permanently modified, it must be recompiled.

Guess you like

Origin blog.csdn.net/weixin_46504000/article/details/129245319