Linux | Red Hat Certified | IT Technology | Operations Engineer
👇1000-person technical exchange QQ group, note [public account] for faster approval
Soft and Hard Links
Difference between Soft and Hard Links:
Soft Link: It is an independent file with its own inode and inode number.
Hard Link: It is not an independent file; it shares the same inode with the target file. A hard link simply creates a mapping relationship between the specified file name and inode number in the directory specified by Linux!
We can create soft and hard links for a file using the following commands:
$ ln -s filename linkname # Create soft link
$ ln filename linkname # Create hard link
Understanding Hard Links:
As we can see, what really points to the file on the disk is not the file name, but the inode. In fact, in Linux, multiple file names can correspond to the same inode.
[root@localhost linux]# touch abc
[root@localhost linux]# ln abc def
[root@localhost linux]# ls -1i
abc def
263466 abc
263466 def
The linking status of abc and def is identical; they are called hard links pointing to the file. The kernel records this link count, and the hard link count for inode 263466 is 2.
When we delete a file, we perform two actions: 1. Delete the corresponding record in the directory, 2. Decrease the hard link count by 1. If it becomes 0, the corresponding disk space is released.
Hard Link Count:
ll
These numbers represent the hard link count.
The essence of a hard link is a counter in the file’s inode attributes. It indicates how many file names have established a mapping relationship with my inode. In simple terms, it has its own file name pointing to my inode (the file itself).
Understanding Soft Links:
We can create a soft link using the command below:
$ ln -s filename linkname # Create soft link
We can clearly see that their inodes are different.
We create a mytest.c file with the following content:
#include <stdio.h>
int main(void) {
printf("hello, soft link...\n");
printf("hello, soft link...\n");
printf("hello, soft link...\n");
printf("hello, soft link...\n");
printf("hello, soft link...\n");
printf("hello, soft link...\n");
return 0;
}
The program runs normally; here we can run it directly with ./mytest.exe in the parent directory’s parent directory.
However, if we want to run this program from outside, it can be cumbersome because its path is a bit deep:
It’s too troublesome, so we can create a soft link to free our hands:
$ ln -s ./lesson18/mytest.exe my.exe
Now we can run it directly with ./my.exe to free our hands.
This is very similar to creating shortcuts on the desktop in Windows.
Deleting Soft and Hard Links::
To delete, you can directly use rm, but we still recommend using the dedicated unlink command:
unlink filename
Dynamic and Static Libraries:
What are dynamic and static libraries?
Dynamic Library .so: The program links to the dynamic library code only at runtime, allowing multiple programs to share the library’s code.
Static Library .a: The program links the library code into the executable file at compile time. The program will no longer need the static library at runtime.
An executable file linked to a dynamic library only contains a table of entry addresses for the functions it uses, rather than the entire machine code of the external function’s target file.
Dynamic Linking and Static Linking:
Dynamic Linking:
Before the executable file starts running, the machine code of the external functions is copied from the dynamic library on disk into memory by the operating system. This process is called dynamic linking.
Dynamic libraries can be shared among multiple programs, so dynamic linking makes the executable file smaller and saves disk space. The operating system uses a virtual memory (VM) mechanism to allow a single copy of a dynamic library in physical memory to be shared by all processes that need it, saving memory and disk space.
Static Linking:
Static linking is more aggressive; it directly links all the binary code of the target interface into the original file at link time, which is why the files generated by static linking are so large (after all, the binary code is copied over).
However, these are relative; there are advantages and disadvantages:
If the dynamic library in the path is lost or damaged, the dynamically linked program will definitely encounter errors when trying to use it at the target location.
Static linking does not rely on the original library since the binary code is copied over at compile time, so even if the original library code is lost, it is not a problem.
Generating Dynamic and Static Libraries:
Generating a Static Library:
$ ar -rc [static library] [.o]
The library name starts with lib, and static libraries end with .a.
At this point, we have a static library. A static library is essentially a package of the previously compiled source files into .o files. When others use our library, they simply find the .o files in the library and include them in the executable program.
Generating a Dynamic Library:
Dynamic libraries are a bit more complex than static libraries:
gcc -fPIC -c myadd.c -o myadd.o
gcc -shared -o libmyadd.so myadd.o
The difference is that when creating .o files, we need to add gcc -fPIC to generate position-independent code.
Using Static and Dynamic Libraries:
Now, from the perspective of the user, let’s learn how to use static and dynamic libraries.
Using Dynamic Libraries:
-I specifies the path for header file search.
-L specifies the path for library file search.
-l specifies which library to link under the path specified by -L.
Example:
gcc mytest.c -o mytest -I lib-dyl/include/ -L lib-dyl/lib/ -l mymath
After forming the executable program, the required code has already been copied into my code, so it does not depend on your library at runtime. No runtime search is needed.
Why do dynamic libraries have this issue? Just find a way for the process to locate the dynamic library.
Error while loading shared libraries solution:
1. Copy the dynamic library to the system path /lib64 for installation.
2. Import the path to the dynamic library through environment variables — when the program runs, it will look for the required dynamic library path in the environment variable — LD_LIBRARY_PATH.
3. Configure the system configuration file.
No runtime search is needed.
Why do dynamic libraries have this issue? Just find a way for the process to locate the dynamic library.
Error while loading shared libraries solution:
1. Copy the dynamic library to the system path /lib64 for installation.
2. Import the path to the dynamic library through environment variables — when the program runs, it will look for the required dynamic library path in the environment variable — LD_LIBRARY_PATH.
3. Configure the system configuration file.
For course inquiries, add: HCIE666CCIE
↑ Or scan the QR code above ↑
If you have any technical points or content you want to see,
Feel free to leave a message below to let me know!