Soft Link Overview

A soft link is a common way to share files and directories in Linux. This way is similar to a shortcut in Windows. When a file or directory needs different paths, you can create soft links to share the file or directory. In this way, the system has only one source file or directory. In addition, the clever use of soft links can greatly increase the portability of applications.

Creating and deleting soft links

To create and delete a soft link, run the ln command to create a link file in the following format:

Ln -s [source file or directory] [destination file or directory]Copy the code

Create a soft link to /usr/local/www to/TMP:

$: ln -s /usr/local/www /tmp/www
$: ls /tmp/www -l
$: lrwxrwxrwx 1 fens     fens       14 9月  12 08:56 www -> /usr/local/www
Copy the code

Delete soft links:

rm /tmp/www 
Copy the code

Note: when deleting, if it is a directory, do not add “/” at the end, otherwise it will delete the directory pointing to!

Soft link use tips

Shared directory Files

For embedded Linux development, Linux virtual machines will install cross-compilers for various platforms. These compilers may use libraries that require header files to be placed in the path of the compiler, so that when the program is compiled, it will automatically find header files. Here is my virtual machine:

Drwxr-xr-x 12 1007 Users 4.0K July 18 2019 ARM-Himix200 - Linux drwxr-xr-x 9 root root 4.0K January 24 2017 ARM-hisiv300 - Linux Drwxr-xr-x 10 520 520 4.0k 5月 5 2019 ARM-Hisiv500-Linux drwxr-xR-x 10 520 520 4.0k 6月 9 10:30 ARM-hisiv600-LinuxCopy the code

The Boost library is also easy to use, since 85% of its modules only need to reference header files. Therefore, the library files are relatively large, which can be used in every compiler. Sharing files by creating soft links also reduces disk space consumption. Here is the soft link I established:

$ls arm-himix200-linux/target/usr/include/boost -lLRWXRWXRWX 1 root root 10 July 19, 2018 the arm - himix200 - Linux/target/usr/include/boost - > / opt/boost
$ls arm-hisiv300-linux/target/usr/include/boost -lLRWXRWXRWX 1 root root 10 on May 12, 2018 arm - hisiv300 - Linux/target/usr/include/boost - > / opt/boost
$ls arm-hisiv500-linux/target/usr/include/boost -lLRWXRWXRWX 1 root root 10 July 30, 2019 arm - hisiv500 - Linux/target/usr/include/boost - > / opt/boost
$ls arm-hisiv600-linux/target/usr/include/boost -lLRWXRWXRWX 1 root root 10 July 30, 2019 arm - hisiv600 - Linux/target/usr/include/boost - > / opt/boostCopy the code

Unify device file names to enhance application portability

On embedded Linux, the device name and GPIO number may be different. For example, on some platforms, the serial port device name is ttySxx, while on others, it is ttyAMAxx. The running indicator light of this product is GPIO51 control, the other product is GPIO32 and so on.

Therefore, the application of different platforms either add a compiler switch, macro definition to distinguish the device file name of different platforms, which will lead to bloated code, readable, different platforms need different compiler switch, easy to error.

Using soft links is a good way to solve this problem. You can create a dev directory (location of your own), which I like to put in the same directory as the application. Then use soft links to unify the device files with names such as:

Platform 1:

$ cd /usr/local/dev
$ ln -s /dev/ttyS1 ./com1
$ ln -s /dev/ttyS2 ./com2
$ ln -s /dev/ttyS3 ./com3
$ ln -s /sys/class/leds/led_orange/brightness ./runLed
$ ln -s /sys/class/gpio/gpio211/value ./powerLed
$ ls -l
#Ignore my wrong timeLRWXRWXRWX 1 fens fens 10 1 1 2000 com1 -> /dev/ttys1 LRWXRWXRWX 1 fens fens 10 1 1 2000 com2 -> /dev/ttys2 LRWXRWXRWX 1 fens holds fens holds 11 January 2000 com3 - > / dev/ttyS3 LRWXRWXRWX 1 fens holds fens holds 29 January 1, 2000 power - > / sys/class/gpio/gpio211 / value 1 fens holds LRWXRWXRWX fens holds 37 on January 1, 2000 runLed - > / sys/class/gpio/gpio212 / valueCopy the code

Platform 2:

$ cd /usr/local/dev
$ ln -s /dev/ttyAMA1 ./com1
$ ln -s /dev/ttyAMA2 ./com2
$ ln -s /dev/ttyAMA3 ./com3
$ ln -s /sys/class/leds/led_orange/brightness ./runLed
$ ln -s /sys/class/gpio/gpio211/value ./powerLed
$ ls -l
#Ignore my wrong timeLRWXRWXRWX 1 fens fens 10 1月 1 2000 com1 -> /dev/ttyama1 LRWXRWXRWX 1 fens fens 10 1月 1 2000 com2 -> /dev/ttyama2 LRWXRWXRWX 1 fens fens 11 1月 1 2000 com3 -> /dev/ttyama3 LRWXRWXRWX 1 fens fens 29 1月 1 2000 power -> / sys/class/gpio gpio41 / value LRWXRWXRWX 1 fens holds fens holds 37 on January 1, 2000 runLed - > / sys/class/gpio/gpio42 / valueCopy the code

In this way, you only need to add a script to create soft links, which can be called at the time of system startup. You can solve the problem of different device files on different platforms, and the application does not need to change, and the driver does not need to change.

In the practical application, there are more skills to explore, here is just to throw a brick to attract jade, there are better skills can also stay in the following message, exchange and study together!

Wechat official account: Fensnote

Welcome to: