Dynamic library and static library in Linux

Soft Links vs Hard Links

soft link

When we are not running under the local path, the running target binary file generally needs to specify the program path.
insert image description here
However, this kind of running is extremely time-consuming for programs with more complicated paths. For easy operation, we can set the path of the executable program as a soft link. The soft link at this time can generally be regarded as a shortcut to the Windows executable program.

The command to create a soft link is as follows :

[yzh@VM-4-5-centos ~]$ ln -s /home/yzh/my.exe mysoft

insert image description here
Note :
When creating a soft link, the path of the executable program cannot be written wrong, otherwise an error will occur when running the program.

hard link

Hard link refers to the establishment of a mapping relationship between the file name and the target inode file in the specified directory, and generally defaults to "alias".

The command to create a hard link is as follows :

[yzh@VM-4-5-centos ~]$ ln /home/yzh/my.exe myhard

insert image description here

Purpose of hard links

We know that there is also a reference count in the inode, which represents the number of file names associated with the target file.

  • When we create a hard link, it means that one more file name is associated with the target file, and the reference count is +1.
  • When we delete a hard link, it means that one less file name is associated with the target file, and the reference count is -1.
  • When the reference count decreases to 0, it means that no file name is associated with the target file, which in turn means that the file is actually deleted.

Delete link relationship command :

[yzh@VM-4-5-centos ~]$ unlink myhard

insert image description here

Extended application

When we create an empty directory, why is the reference count of the file 2? Because the
insert image description here
directory name of itself and the " . " inside itself have a mapping relationship with the inode of the same file. Among them, " . " also represents the current directory (that is, the directory we are in). Another example: when we create a ceshi1 directory in the ceshi directory, enter the directory, use the ll -ail directory to view, and find that "..." in this directory is the same as the inode of the ceshi directory, indicating that "..." is also mapped to the ceshi directory Relationship. Among them, "…" also represents the upper level directory
insert image description here
.

insert image description here
Therefore, we can use the reference count of the target directory -2 (self-correspondence and the "." in the next-level directory) to determine the number of sub-directories in the directory (also represents the number of "..." in the sub-directory).

The difference between soft and hard links

When we look at the soft and hard links created, the difference is as follows :

  • The soft link has an independent inode, indicating that the soft link is an independent file.
  • The hard link does not have an independent inode, indicating that the hard link is not an independent file.
    insert image description here

Dynamic library and static library

For ease of understanding, we have created two source files and two header files, which are only used for related operations of dynamic and static libraries.

mymath.c

accumulated to the target value.

#include "mymath.h"                                                                                            
int addToTarget( int form,int to )
 {
    
    
           int sum = 0;
 
           for( int i = form; i <= to; ++i )
            {
    
    
              sum += i;
            }
            return sum;
 }

mymath.h

 #pragma once
 #incldue <stdio.h>
 extern int addToTarget( int from, int to );

myprint.c

Prints the given string and timestamp.

#include "myprint.h"
void Print( const char* str )
{
    
    
   print("%s[%d]\n",str,(int)time(NULL));
}

myprint.h

#pragma once
#include <time.h>
extern void Print( const char* str);

main.c

Call myprint and mymath related libraries.

#include "myprint.h"                                                                                                #include "mymath.h"
int main()
  {
    
    
    Print("hello world\n");
    int res = addToTarget(1,100);
    printf("%d\n",res);
    return 0; 
 }

static library

Generate static library

1. Generate binary files from all source files. (Not executable).
insert image description here

2. Use the rc command to package these binary files into a static library.

  • -r(replace): If the created target file has the same name as the static library file, replace the static library file with the same name with the new static library file.
  • -c(create): Create a static library.
    insert image description here
    3: Put the header files and the static library in the static library in the specified directory.
    insert image description here

Of course, in order to simplify the above operations, we can also use the Makefile to generate uniformly.
insert image description here
Use the make and makeoutput commands to agree to generate and package the static library.
insert image description here

Use of static libraries

Method 1 : Use the sudo cp command to copy the header files to: /usr/include and copy the static library files: /lib64. However, when using the gcc command to compile and link
insert image description here
into dozens of executable programs, you need to specify the library to be compiled. (Just remove the prefix and suffix from the library name) For example: libhello.a --> hello. Method 2: Directly specify the path of the header file and static library file, and let the compiler search in the specified path. The compilation command is
insert image description here
as follows
:

gcc main.c -I ./output/include/ -L ./output/lib -lhello
  • -I: Indicates to specify the header file search path.
  • -L: Indicates that the library file search path is specified
    insert image description here

dynamic library

Dynamic library generation

Method one :

The first step : First compile the source files into binary files (not executable).

gcc -fPIC -c mymath.c -o mymath.o -std=c99
gcc -fPIC -c myprint.c -o myprint.o -std=c99

insert image description here
Step 2 : Use the gcc -shared command to package all object files to generate a dynamic library

gcc -shared myprint.o mymath.o -o libhello.so

insert image description here

Step 3 : Unify the header files and static libraries in the specified directory

We choose to put the .h header file in the include under the output directory, and put the dynamic library .so file in the lib directory under the include directory.
insert image description here
Method 2: Use Makefile to package and generate uniformly.

We write all the commands for packaging and generating dynamic and static libraries into the Makefile, and use the Makefile command to generate dynamic and static libraries.
insert image description here
insert image description here

The use of dynamic libraries

Different from the use of dynamic libraries, the executable program generated by compiling and linking with gcc -I -L -l cannot be run directly. Reason: When we use -I -L to indicate the path of the dynamic library in gcc, we are talking to the gcc compiler. When we want to
insert image description here
run the loader, the executable program has been generated at this time, which has nothing to do with gcc. The static library does not need to tell the operating system the address of the library because the static library has already loaded the code into the memory when it is loaded, and there is no need to establish a mapping relationship with the operating system to find it.

Method 1 : Increase environment variables

Since LD_LIBRARY_PATH is the path to be searched when the process runs the dynamic library, we need to add the path of the dynamic library to the LD_LIBRARY_PATH environment variable.

Use export plus the absolute path of the dynamic library in the LD_LIBRARY_PATH environment variable, and use the echo command to check whether the environment variable is successfully added.

[yzh@VM-4-5-centos userlib]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/yzh/userlib/output.lib

insert image description here

Method 2 : Configure /etc/ld.so.conf,d/ file

Step 1: Create a file in the /etc/ld.so.conf, d/ directory (the suffix must be .conf)

[yzh@VM-4-5-centos userlib]$ sudo vim /etc/ld.so.conf.d/yzh.conf

insert image description here
Add the path of the dynamic library to this file.
insert image description here
Then use the sudo ldconfig command to update the directory file under /etc/ld.so.conf.d/.
insert image description here
Method 3 : Set up soft links

Set the path of the dynamic library as a soft link under /lib64/. It is best to indicate the absolute path of the dynamic library in setting the soft and hard links, otherwise the system will not find the dynamic library.

[yzh@VM-4-5-centos userlib]$ sudo ln -s /home/yzh/userlib/output/lib/libhello.so /lib64/libhello.so

insert image description here
At this point, we use ls /lib64 to check whether the soft link is established.
insert image description here

The difference between dynamic and static libraries

  • When the static library is used, the executable program is loaded into the memory and mapped to the code area of ​​the address space through the page table, and each process running the program will repeatedly load the program into the memory, which greatly wastes the memory space .

  • The dynamic library can be loaded in batches with the executable program. When the process runs to the dynamic library in the executable program, the process will load the dynamic library into the memory, and the memory is mapped to the shared area between the stack and heap in the address space through the page table. When the process executes the dynamic library, it will jump from the code area to the shared area for execution. When there are multiple processes running the dynamic library of the executable program, the dynamic library loaded into the memory at this time is shared by each process, so the dynamic library is also called a shared library.
    insert image description here

Guess you like

Origin blog.csdn.net/m0_63300413/article/details/131754814