Introduction to Linux for embedded learning

The following content is collated from: [Beijing Xunwei] Embedded learning Linux introduction _bilibili _bilibili

1. Ubuntu

Ubuntu is one of the distributions of Linux, along with Radhat centos, Debian Opewrt and others

Ubuntu is divided into: Ubuntu without an interface (Ubuntu-core); Ubuntu with an interface (Ubuntu-Desktop).

Ubuntu components: Ubuntu-Core + third-party desktop

Ubuntu-core + gnome = Ubuntu

​ Ubuntu-core + kde = kubuntu

​ Ubuntu-core + lxde = lubuntu

1.1 Enabling user root

** Current user @ Host name: ~ ∗∗(**(∗∗(not root))

Open the root:

sudo passwd
Copy the code

And then set the password

When finished, enter:

su root
Copy the code

Enter the password to enter root mode (# for root user)

Exit the root user:

exit
Copy the code

1.2 Download using apt-get

Automatic software download, installation, configuration

Settings download source: Settings in software&updates

Update download source:

Sudo apt-get update// This command accesses each url in the source list, reads the software list, and saves it on the local computer.Copy the code

Installation software:

Sudo apt-get install // eg: apt-get install vimCopy the code

Software Update:

Sudo apt - get upgrade / / this command it will list and we use the apt - getupdate download software, comparing the software if it is found that the installed version is too low, will prompt update, if the software is the latest version, there would be no need to update.Copy the code

Software uninstallation:

Sudo apt-get remove < software name >//eg: apt-get remove vimCopy the code

1.3 Use of Vim editor

The VI editor is the original editor for Unix systems. It uses console graphical mode to simulate a text editing window, allowing you to view lines in a file, move around in a file, insert, edit, and replace text. Although it may be the most complex editor in the world, it has a wealth of features that have made it a mainstay tool for Unix system administrators for years. When the GNU project ported the VI editor to the open source world, they decided to make some improvements to it. Since it was no longer the original VI editor of old Unix, the developers renamed it ViImproved, or Vim

Vim is an enhanced version of VI

Open the Vim editor and type directly from the console:

Vi <filename> //eg: vi helloworld.c // If the open file does not exist, it will create a new file, if the file exists, then it will open directly.Copy the code

1.3.1 Three modes of Vim editor:

Enter the normal mode directly; Press I on the keyboard to enter edit mode (press Esc to exit edit mode). Command line mode, enter: to enter the command line mode (press Esc to exit the command line mode)

Press the arrow keys to move the cursor, and Vim also has its own commands for moving the cursor

K: move up J: Move down H: move left L: Move right Ctrl+F (PageDown) : scroll down Ctrl+B (PageUp) : scroll upCopy the code

1.3.2 Quick Location:

Gg: Move the cursor to the first line G: Move the cursor to the last line NGG: Move the cursor to the NTH lineCopy the code

1.. 3.3 Text Copy and Paste:

Copy a single line: double-click YY

Duplicate multiple lines:

  1. First locate the first line of the content to be copied, press V, then move the cursor to select the text, and press Y when the selection is complete. Move the cursor to where you want to paste and press P to paste the content.
  2. Locate the first line of the content to be copied and type NYy (eg: 3YY) to copy 3 lines

1.3.4 Deleting Text:

  1. Edit mode: Use the Delete key to delete data

  2. Common mode: Run the dd command to delete the entire line where the cursor is located

    Delete multiple lines using NDD

  3. Command line mode: use n1, n2d to delete rows in a specified range

1.3.5 Undo Operation:

In common mode, press U.

Reverse Undo: Press CTRL + R

1.3.6 Finding text

Command line mode, use “/ + find content “eg: / ABC

Press “n” to find the next one

1.3.7 Replacing Text

: %s /oldtext/newtext/g
Copy the code

1.3.8 Save the Settings and exit

: q! // Forcibly exit: wq // Save the configuration and exit: q // Exit without modificationCopy the code

1.3.9 Comparison of documents

Use:

vimdiff  fiel1 file2
Copy the code

1.4 Ubuntu Common Commands

1.4.1 Run the ls command to View folders

We use the ls command to view the file information

ls
Copy the code

View hidden file -a; Display all file information -l

ls-al
Copy the code

The meanings of the first column parameters:

D: / / directory file: / / normal file: p/l/manage file: / / link file b: / / block device file c: / / character device file s: / / socket fileCopy the code

File permissions:

R :// Read permission w:// write permission x:// executable permission -:// No permissionCopy the code

1.4.2 Switching directory Command CD

cd .. // return to the previous levelCopy the code

The PWD command is used to view the Current Directory

2. Linux

2.1 File Path

2.1.1 Absolute Path (complete Path) and Relative Path

Absolute path: Start from the root directory, which on Linux is /

Relative path: A path relative to the current location, usually starting with **./ **

2.1.2 Linux Home directory and root directory concepts

The root directory is the lowest directory in Linux and is represented by a /

cd /
Copy the code

Home directory: the path where the current user resides. In the /home directory, it is represented by ~

The home directory of user root is /root

2.2 Common Linux Commands

2.2.1 Creating a folder Command mkdir

Mkdir test// Create the test folderCopy the code

Create a multi-level directory: use the -p parameter

mkdir -p test/test1/test2
Copy the code

2.2.2 Deleting a folder Command rmdir

Rmdir foldername// Delete only empty foldersCopy the code

2.2.3 Deleting a File or Directory Run the rm command

Parameters:

-r: deletes all subdirectories under the directory recursively (r must be added to delete a directory) -f: deletes the directory forcibly. -I: prompts you to confirm the deletionCopy the code

2.2.4 Create a file using the touch command

touch filename
Copy the code

2.2.5 Refreshing the screen Run the clear command

Clear // Clear the screen, but save the historyCopy the code

2.2.6 Screen initialization Command reset

Reset // Clear the screen while clearing recordsCopy the code

2.2.7 Command cp to Copy a File or Directory

Cp old filename new filename cp old_filename new_filename // Copy the file cp -r old_foldername new_foldername // Copy the folder. Recursive copy cp filenale /path // copies the file to the new pathCopy the code

2.2.8 Changing the Name of a File (Directory) Mv Command

Mv old_filename new_filename // Change the filename. Mv old_foldername new_foldername // Change the foldernameCopy the code

** To move multiple files, use the wildcard ***

2.2.9 Compress and decompress the tar command

Tar [Parameter] Compressed file name File or directory to be compressedCopy the code
Parameters: -c: create a new zip package -x: unzip the package. -z: compress or unzip the package in gzip format, which is compressed if combined with C, and unzip if combined with -x. -j: bzip2 format, which is compressed if combined with C, and unzip if combined with -x. -v: displays the file being processed. -C: format -c path: decompresses the compressed file to a specified pathCopy the code
Gzip tar -xzf test.gzip tar -xzf test.gzip -c.. / newFolder // Unzip test.gzip into newFolderCopy the code

2.2.10 Run the ifconfig command to Check and Configure the network status

Ifconfig // View the current network information ifconfig NIC IP address // Set the IP address eg: ifconfig ens33 192.168.3.45 ifconfig NIC Down // Disable the NIC eg: Ifconfig ens33 Down IfConfig ENS33 up // Card card eg: ifconfig ens33 UpCopy the code

2.2.11 Run the cat command to view file Contents

Cat filename // View the file content cat -b filename // Add the -b parameter to display the line number of the file contentCopy the code

2.2.12 Run the reboot command to restart the system

Reboot // Restarts the systemCopy the code

2.2.13 Run the poweroff command

Poweroff / / off the phoneCopy the code

2.2.14 Viewing the IP address ipconfig of a Windows PC

Ipconfig // View the IP address of the Windows computerCopy the code

2.2.15 ping command

You can test whether the network of the machine and the target machine is connected, how fast, how stable.

Windows IP: 192.168.0.107

Ping 192.168.0.107 // Test the connection statusCopy the code

2.3 Linux Help Guide

Man man // The manual has 9 pages 1. Executable programs or shell commands 2. System calls 3. Library calls 4. Help for devices and special files, usually under/dev 5. Help with configuration files 6. Help with games 7. Help with miscellaneous 8. Help with system commands that superusers can execute 9. Kernel dependentCopy the code

Press Q to exit the manual

View command Description

Man 1 PWD // View the PWD instruction man -f PWD // Search for the command location if you do not know where the command isCopy the code

2.4 Linux Permission Management

Ubuntu is a multi-user operating system. We can create different accounts for different users, and each user has his own account to log in. The advantage is that we can manage each user well, and we can also control the access of each user to the system.

Ubuntu users fall into three categories:

  1. The root user
  2. The user is created for the first time
  3. Average users // developers rarely use it

All user information is stored in the /etc/passwd file

Using the ls -l command, you generally have three permissions for files: read permission R, write permission W, and execute permission x

The nine English characters are divided into three groups. Each group consists of three letters. Group 1: permissions of the file owner; Group 2: permissions of the user group to which the file owner belongs. Group 3: permissions of other users. Column 3: Topeet represents the file owner; Column 4: The group to which the topeet file owner belongs

Because our permissions for each file are represented by nine bits, in groups of three bits, we can combine them into eight different situations

Against 2.4.1 chmod command

Function: Change the permission of a file or folder

Chmod Parameter Permission file name //eg: chmod 777 test.cCopy the code

2.5 Concept of connection files in Linux

There are two types of connection files under Linux. One is a shortcut similar to Windows, which we call soft links, which can also be called symbolic links. The other is created through inode connections in the file system, similar to copying on Windows computers, but without creating new files. We call this a hard link. Hard links can also be referred to as physical connections.

2.5.1 Index Nodes

Inodes are also called inodes, and the kernel assigns one inode to each newly created file. Inodes are used to store file information. Each file occupies an inode, and the inode number is unique. The inode can be simply understood as a pointer, which always points to the specific storage location of the text. The system defines each file by inode, not by filename.

Run the ls -i command to view the inode number

ls -i
Copy the code

2.5.2 hard links

A hard link is a new link to an inode number. This link points to the inode, and the system does not reassign the inode. If two files have the same inode, the two files are identical. You can use the ln command to establish a hard link

Ln Source file Destination file // The common parameter -f is used to forcibly create a connection regardless of whether the destination file exists.Copy the code

Use the touch test1.c command to create file test1.c, and then use ln test1.c test2.c to create a hard link

touch test1.c
ln test1.c test2.c
Copy the code

Their inode number is 524652. Test1.c and test2.c are identical files. Test1.c and test2.c are identical files. Test1.c and test2.c are identical files.

Let’s create another hard connection. Ln test1.c test3.c -f: ln test1.c test3.c -f: ln test1.c test3.c -f: ln test1.c test3.c -f: ln test1.c test3.c -f: ln test1.c test3.c -f: ln test1.c test3.c -f

Advantages and disadvantages of hard links:

Advantages:

  1. Convenience, while similar to Windows replication, usually does not take up real space. Whether we modify test1.c or test2.c or test3.c, if we modify either, our files will be modified at the same time because their inode numbers are the same.
  2. Safe to prevent accidental deletion. If we delete either test1.c or test2.c or test3.c, we can still access the file through the rest of the connections, unless we delete both. We can make use of this feature to do the backup of files.

Disadvantages:

  1. A hard connection can only be created on the same file system (because different file systems have different management modes), and even some file systems do not have index numbers, so they are not indexed file systems. Even if it has an index number, the index number of the two file systems doesn’t have to have the same meaning. Even if it has the same index number, we connect several files, it has the same inode, but other files in different file systems might use that inode, and there could be conflicts. So we can only create hard connections in the same file system.
  2. You cannot create hard connections between directories (too complex to support right now). If hard links to our catalogue, then we need along with the connection of data to be connected all data directory to create hard links, if we are going to the root directory of the etc with hardwired to create a hard-wired directory, so it is not only our files to be created, the file below all file names to create a hard connection, This will create a large workload for the work environment, and is very complex, so it is not supported at present.

2.5.3 soft links

Similar to shortcuts on Windows. You can use the ln command to establish a soft link

Ln -s source file destination file // And the source file to use absolute paths) ln -s/home/topeet/test/test3. C test1. C / / to test3. C this file to create a soft linkCopy the code

Test1. c and test3.c have different inode numbers, so the two files are completely independent. In short, a soft link creates a new file. When accessing the linked file, the system will discover that it is a linked file, and then read the linked file to find the actual file to access.

Because it’s similar to the shortcut on Windows, we delete the source file test3.c, and test1.c cannot be opened.

2.6 Linux Directory Structure

The entire Linux file system starts with the ** “/” directory, and the root directory is the top layer. The concept of root and home directories was mentioned earlier. There are many directories below it, which are also called subdirectories, and there are more directories below the subdirectories. It forms a tree-like structure. You can think of it as an upside-down tree.

The structure is virtual, there are no restrictions, it is just a virtual concept. So in theory, the Linux directory structure can be arbitrary, which means I can go where I want to go, without any restrictions, but if all of us do that, and different people think differently, then we can easily get messed up.

In order to solve the problem of disunity among different developers, the hierarchical standard is researched, that is, the file hierarchical standard, short for FHS, full name Filesystem hierarch Standard. The FHS defines two layers of specification:

  1. What files should be stored in the/directory? For example, configuration files should be placed under /etc, and executable files should be placed under /bin or /sbin
  2. It is defined for the subdirectories of /usr and /var in Linux. For example, /usr/share should be a shared data file. FHS only gives you the topmost directory and the sublayers /usr and /share to store data, we can configure the other sublayers at will

2.6.1 Linux Root Directory

The FHS has some fairly detailed rules about the base directories of the Linux root file system, such as which folders to place which files. In the ubuntu root directory, the rules for each file are as follows:

2.7 Linux File System

The software that manages and stores file systems in an operating system is called a file system.

A Linux system must mount a file system, and if the system cannot mount it from the specified device, the system will fail.

Common types of Linux file systems are ext3, ext4, Proc file systems, and sysFS file systems.

  1. The ext3 file system evolved from ext2. It is fully compatible with the ext2 file system and is smaller and more reliable than ext2.
  2. Ext4 file system is improved on the basis of ext3, and the performance and reliability of ext4 file system is better than that of ext3, and the function is very rich, and ext4 is fully compatible with ext3, ext3 only supports 32000 subdirectories. But ext4 is better than 3 because it supports an unlimited number of subdirectories.
  3. The Proc file system is a special file system in Linux that actually exists only in memory, and it is a pseudo-file system. This file system is the mechanism by which the kernel and kernel modules send messages to processes.

2.7.1 Ubuntu File System

You can run the df command to view the file system type of Ubuntu. The df command displays the available disk space on the disk partition. Use df -t to display the type of the file system, as shown in the man manual.

Enter the ** “df -th” ** command to display in a more human-readable way:

2.8 The first Linux program HelloWorld

Those who have experience in SCM or ARM embedded development know that after we write a program, we need to compile the program. Then there are integrated environments such as KEil and IAR on SCM for us to choose. But once we enter the world of Linux, these common ones will disappear. Everything has to start again. On Linux, ubuntu, we compile using GCC

2.8.1 Basic use of GCC

GCC stands for GNU Compiler Collection. GCC supports a variety of computer architectures, such as X86, ARM, and MIPI. The ubuntu we use comes with GCC by default

GCC -v // Check the GCC versionCopy the code

Create the hello.c file and add the following content

vi hello.c
Copy the code
#include <stdio.h> int main(void) {printf(" Hello world! \ "); return 0; }Copy the code

Then we compile hello.c in the following format:

GCC option file //-o specifies the name of the generated file eg: GCC hello.c -o hello // If we don't use the -o parameter to specify the name of the generated file, just type GCC hello.c to compile the a.out file. The structure of a.out is the same as hello. // The -c argument tells GCC that compiling the source file will assemble, but not link. At this point, the target file is generated, or if no output file is specified, a.o file with the same name is generated. // When -c is followed by multiple source files, an. O file is generated for each source file, but -o cannot be used.Copy the code

2.8.2 file command

View the file type. Format:

File File name // eg:file helloCopy the code

Executable files compiled using GCC compiler are X86 and cannot run on ARM development board.

2.8.3 Compilation Process

Compiling from hello.c to hello or A.out goes through four steps: preprocessing, compilation, assembly, and linking.

Hello. I preprocessed C language code Hello. S assembly language file HelloCopy the code
  1. In the preprocessing phase, the compiler expands the header file or macro definition, or the conditional compilation option. We can use the -e parameter to get the preprocessed file.
GCC -e hello.c -o hello. I // Use GCC -e hello.c -o hello. I to get the preprocessed fileCopy the code
  1. To compile a file into assembly code. The -s argument compiles the hello. I file to a hello. S file
gcc -S hello.i -o hello.s
Copy the code
  1. Assembler, the compilation of assembly files into machine code. The -c argument compiles the hello.s file into the hello.o file
gcc -c hello.s -o hello.o
Copy the code
  1. Link. Compile the object file directly into an executable. Link is divided into static link and dynamic link, GCC is the default dynamic link, the generated program is small, but need to rely on the library. Static links: Using the -static parameter is static links, because the program contains the required library, so the volume is large.
GCC hello.o -o hello // dynamic link GCC hello.o -o hello-static // static linkCopy the code

2.9 Linux Environment Variables

2.9.1 Environment Variables

Environment variables are system default parameters. Linux is a multi-user operating system, so each user also has their own environment variables. For example, the command we learned before can be executed successfully no matter which path is entered, because the system has set the search path of the command in advance.

The common variable PATH determines which PATH to go to find our program or command, and we will change this variable frequently throughout the development process.

2.9.2 echo command

Function: Displays a paragraph of text on standard output

Echo $PATH //$indicates a referenceCopy the code

2.9.3 Modifying Environment Variables in Ubunu

Two common methods: (example: add /home/topeet/test to PATH variable)

  1. Use the command directly
Export variable = Value of the new variable :$variable export PATH=home/topeet/test/:$PATHCopy the code

With this method, the environment variable takes effect immediately, but only temporarily, when we reopen and close the terminal, it is gone, and only for the current user.

  1. Modify the.bashrc file to add our environment variables directly to the configuration file. Add the added environment variables to the bottom line of the configuration file.
Export variable = Value of the new variable :$variable export PATH=home/topeet/test/:$PATHCopy the code

Bashrc (source. Bashrc), or restart and close the terminal to take effect. Modifying this file is permanent, but only for the current user.

source .bashrc
Copy the code

2.10 Linux writes its first command

2.10.1 Concepts of Commands

A command is an executable program. For example, if we type ls -al, ls is the name of the executable program. -al is the argument to be passed in.

2.10.2 ps command

Displays process dynamics.Copy the code

When the shell receives our command, it will look for the environment variables and the default path according to the characters we input. As mentioned in the previous chapter, you can print the environment variables to see what they are, and then it will look for any program with the same name as the command we input.

2.10.3 Defining a Command of your own

Compile the hello.c file you wrote earlier

GCC hello.c -o hello. /hello // Run the hello fileCopy the code

We can now execute hello as an executable, but now we have an executable that can only run in /home/dlx/desktop, and we have to add the current directory, so if I go up to the directory, I can’t run hello with ** “./hello “**, Because the file hello does not exist in the upper-level directory, we cannot directly enter the command “hello”, which will prompt an error.

  1. We learned in the last chapter about environment variables, and the PATH variable in the environment variable determines which paths our programs or commands are going to go to, so we can add the executable PATH to the environment variable, so if we say hello in any PATH, It will automatically find the Hello executable to help us execute.

  1. The bin file in the root directory is used to store executable files. It is also possible to copy the generated hello executable file to the bin folder in the root directory.

This chapter complicates what we have learned before. Such as environment variable modification, GCC compilation. At the same time, we also know that the commonly used command is an executable program, and after we input our command on the keyboard, the command is sent to the shell. Bash, as shown below, is sent to it, and it will look in the environment variable based on the string we typed to see if there is a program with the same name as ours.

2.11 Linux Tools make tools and Makefile files

We used GCC directly when we wrote the first simple program hello.c on Linux. But if we work to compile a project, the project there are many source files, at this time Wait we all use this command to compile it very trouble, and if we modify a source file, then we use the command to compile will perform again the process again, can be very time consuming.

If you have learned single-chip computer before, you can compare the single-chip computer development software Keil in the separate compilation and all compilation. Separate compilation is very time saving, all compilation will be very time consuming, we use the command to compile is equivalent to our single chip software in all compilation.

In order to solve the problem that compiling a project is very tedious, our predecessors invented the compilation assistant tool make. Its compilation idea is very simple. It will compare which file has its time changed before compiling. Instead of wasting time rebuilding other files, it will rebuild them as required.

If you use KEil to write a C file on the microcontroller, the other files in this project have not changed, so we do not need to point to compile all, as long as we have modified the file can be compiled. Make does the same thing, except it’s a little smarter. It doesn’t have to do it for you, it does it for you before you compile it.

2.11.1 Using make

The make tool is a compilation aid to solve the tedious problem of using commands to compile projects.

Call this command tool: we program on Windows using the IDE, we have a graphical interface, and we have buttons like Build or Run to compile. In fact, the make compiler is very easy to use, we directly enter the make command on the console, it will automatically call the make tool.

Type make directly in this directory and get an error because I didn’t tell the make utility what rules it should follow to compile our program. As shown below:

2.11.2 makefile

A Makefile is a file that describes rules such as build connections for the entire project. After we input the make command in the terminal, call the make tool, make will find makefile in the current directory according to the file name, makefile must be named makefile or makefile, m uppercase lowercase is ok.

The reason for the command error is that there is no makefile in the current directory. I’m going to create a new Makefile, and I’m going to type make in the current directory, and when I type make, it’s going to call the make tool, and it’s going to find the Makefile in the current directory, and there’s an error, Because of the makefile created here, he found it but it was empty because it didn’t contain any rules.

To try it out, open the Makefile and press Tab to indent the first line of the file instead of Spaces. Then type, save and exit, as shown below:

After make run hello:

2.12 Basic Syntax for Makefiles

2.12.1 Setting the first Indentation of VIM

Sudo vi /etc/vim/vimr// rc usually ends in a configuration fileCopy the code

Enter set tabstop=4 in the last line, save the Settings, and exit. It turns out that the indent of vim is four Spaces.

2.12.2 Basic Syntax for Makefiles

Syntax format:

Goal: Dependency

(TAB) command

GCC hello.c -o hello // Target: all // dependency: empty // command: GCC hello.c -o helloCopy the code

The above example could also be written as:

O -o hello hello.o:hello.c GCC -c hello.c -o hello.o // Use the -c argument to tell GCC that compiling the source file will compile, but not link. O files with the same name are generated if no output file is specified. // Targets: all and hello.c // dependencies: hello.o and hello.c // command: GCC hello.c -o hello and GCC -c hello.c -o hello.oCopy the code

C -o hello.o GCC hello.c -o hello.o GCC hello.c -o hello Therefore, the following figure shows the execution sequence after entering the make command:

At compile time, we can use the make target to compile. If we do not specify a target, the default rule for the first target is executed. So make is the same thing as make all

If you want to recompile, you need to delete the files generated by the previous compilation, and use clean to solve the problem as follows:

all:hello.o
	gcc hello.o -o hello
hello.o:hello.c
	gcc -c hello.c -o hello.o
	
clean:
	rm -rf *.o hello  
Copy the code

Then we can run the rm -rf *. O hello command by typing “make clean”.

However, we cannot have a file in the current directory with the same target name as the Makefile. For example, if I create a file named clean in the current directory and execute the “make clean” command, an error will be reported.

To solve this problem, makefiles introduce a new concept called pseudo targets, which we use to declare clean to avoid collisions with files with the same name in the current directory.

PHONY: indicates a targetCopy the code
all:hello.o
	gcc hello.o -o hello
hello.o:hello.c
	gcc -c hello.c -o hello.o
.PHONY:clean
clean:
	rm -rf *.o hello  
Copy the code

Then we execute the make clean command. The make clean command succeeds even though there is a file with the same name in the current directory. As shown in the figure below

2.12.3 Makefile variables and variable assignments

Variables can be used in many places, such as targets, dependencies. Or command.

Variables can be assigned using: “=” “? = “:=” “+=”

Use of variables: Reference variables with $()

  1. Use “: =” to assign: the assignment is immediate, the variable value is determined at the same time var1: =aaa is executed, so it is finally printed as aaABbb instead of CCCBBB.
var1:=aaa
var2:=$(var1)bbb
var1:=ccc
all:
	echo $(var2)  //aaabbb
Copy the code
  1. Assignment with “=” : Deferred assignment, which is used to assign the last value specified in the Makefile. Because we end up assigning the variable var1 to CCC, we end up printing CCCBBB instead of aaABbb, as in
var1=aaa
var2=$(var1)bbb
var1=ccc
all:
	echo $(var2)  //cccbbb
Copy the code
  1. The use of “? If var1 has not been previously assigned, it is given CCC. If var1 has been previously assigned, it fits the previous value, so print aaABbb instead of CCC
# this is a comment // the comment in the makefile is # var1? =aaa var2? =$(var1)bbb var1? =ccc all: echo $(var2) //aaabbbCopy the code

Use “+=” to assign: Append assignment, which is adding a new string to the string we defined earlier, so the run prints aaa CCCBBB. But there will be Spaces in between.

var1:=aaa
var2=$(var1)bbb
var1+=ccc
all:
	echo $(var2)  //aaa cccbbb
Copy the code

2.12.4 Automated variables

Automation variables are variables that are not defined and can change from one program to another.

Here are the three most commonly used automation variables:

Let’s start with a few programs:

  1. main.c
#include <stdio.h>
#include <hello.h>

void main(void)
{
		hello();
}
Copy the code
  1. hello.c
#include <stdio.h> #include <hello.h> void hello(void) { printf("hello world! \n"); }Copy the code
  1. hello.h
#ifndef _HELLO_
#define _HELLO_

void hello(void);

#endif
Copy the code
  1. Makefile
hello:hello.o main.o
	gcc hello.o main.o -o hello
hello.o:hello.c
	gcc -c hello.c -o hello.o
main.o:main.c
	gcc -c main.c -o main.o
clean:
	rm -rf *.o hello
Copy the code

You can compile successfully with this makefile, though.

However, once more files are compiled, it becomes very complicated to write makefiles this way. So, automated variables come in handy. Let’s simplify the makefile step by step

  1. Use variables to represent dependent files
Hello :$(var) GCC $(var) -o hello hello.o:hello.c GCC -c hello.c -o hello.o main.o:main.c  gcc -c main.c -o main.o clean: rm -rf *.o helloCopy the code
  1. Use a wildcard %, and automatic variables < ∗ ∗, ∗ ∗ < * *, * * (∗ ∗, ∗ ∗ @ instead of relying on and goals, simplify the finish is as follows:
O hello:$(var) GCC $(var) -o hello %. O :%. C GCC -c $< -o $@ //$< (% is a wildcard character, similar to * on Linux); $@ indicates all targets clean: rm -rf *. O helloCopy the code

3. Use the automation variable “$^” to represent the list of all file dependencies.

O hello:$(var) GCC $^ -o hello %. O :%. C GCC -c $< -o $@ //$^ Clean: rm -rf *Copy the code

2.12.5 wildcard function

Function: Expand the specified directory

Format: $(Wildcard PATTENR)Copy the code

Eg: There is an “A.C” c file and a “test1” folder in the test1 directory, and a “B.C” file in the test1/test1 folder

Create a new Makefile in the test1 directory:

Var =$(wildcard. /*.c./test1/*.c) all: @echo $(var) #echoCopy the code

Execute make as follows:

2.12.6 notdir function

Run the following command to delete paths: Take file name only

$(notdir $(var))Copy the code

Eg: We add the following code to the makefile above, because in the example above we get the result that./a.c and./test/b.c have paths, and we can use this variable directly.

var=$(wildcard ./*.c ./test1/*.c)
var1=$(notdir $(var))
all:
	@echo $(var1)
Copy the code

/a.c and./test/b.c remove paths to get a.c and b.c

2.12.7 dir function

Function: Retrieve the directory, where the directory refers to the part before the last backslash /, if there is no backslash/to return the current.

Format: $(dir <names... >)Copy the code

Eg: We add the following code to the above example, as follows:

var=$(wildcard ./*.c ./test1/*.c)
var1=$(notdir $(var))
var2=$(dir $(var))
all:
	@echo $(var2)
Copy the code

Make: Since var2 has the values./a.c and./test/b.c, the directories are./ and./test

2.12.8 patsubst function

Function: Replace file suffixes

Format: $(Patsubst source file, object file, file list)Copy the code

Eg: We add the following code to the above example, as follows:

var=$(wildcard ./*.c ./test1/*.c)
var1=$(notdir $(var))
var2=$(dir $(var))
var3=$(patsubst %.c,%.s,$(var1))
all:
    @echo $(var3)
Copy the code

This function replaces the.c suffix for var1 variables a.c and b.c with.s, as follows

However, this substitution does not change the suffix in the current directory. What does this function do? We can use this function to replace our suffix name and do other things, and this function will be used in conjunction with other functions.

$(var: a=b); var stands for the name of the file we want to replace. A is the original file and b is the target file. Let’s change the above code as follows:

var=$(wildcard ./*.c ./test1/*.c)
var1=$(notdir $(var))
var2=$(dir $(var))
var3=$(patsubst %.c,%.s,$(var1))
var4=$(var1:%.c=%.o)
all:
    @echo $(var4)
Copy the code

2.12.9 foreach function

Function: Fetches the words in the argument one by one into the variables specified by the argument, and then executes the contained expression. It returns a string each time

$(foreach <var>,<list>,<text>) eg:  var=$(wildcard ./*.c ./test1/*.c) var1=$(notdir $(var)) var2=$(dir $(var)) var3=$(patsubst %.c,%.s,$(var1)) var4=$(var1:%.c=%.o) var5=$(foreach n,$(var2),$(wildcard $(n)*.c)) all: @echo $(var5)Copy the code

Since the var2 variables are./ and./test1, the./ is taken out of the n variable first, and then wildcard is used to get the path to the C files under./test1 and./test1/. Therefore, the execution result is as follows:

2.13 Installing samba software

Sudo apt-get install sudo vi /etc/samba/smb.conf // Modify the configuration fileCopy the code

Add the following: This information is all samba instructions and Settings, copy it, format it correctly, use the Tab key to indent it, and then remove the comments, otherwise it might make an error.

[ubuntu_samba] comment = arm Ubuntu samba dir # note path = /home/samba # Public = yes # writable = yes # writable create mask = 0755 # Permissions when creating new files security = share # share mode force user = root Arm force group =root arm force group =rootCopy the code
Run the following command to query the VM IP address 192.168.0.103: sudo service SMBD restart // Restart ifconfig // Query the VM IP address 192.168.0.103. In Windows, enter \\192.168.0.103. Add network driver mapping. Sudo vim /etc/rc.local Add command: /etc/init.d/smbd start exit 0Copy the code