Use of Linux environment basic development tools (use of yum software installation tool, use of vim editor and use of gcc/g++ compiler, etc.)

insert image description here

1. Linux package manager yum

1.1 What is a software package

A common way to install software under Linux is to download the source code of the program and compile it to obtain an executable program. But this is too troublesome, so some people compile some commonly used software in advance and make it into a software package ( It can be understood as an installer on windows) on a server, and the compiled software package can be easily obtained through the package manager and installed directly. The software package and the package manager are like "App" and "Application Store".
Yum (Yellow dog Updater, Modified) is a very commonly used package manager under Linux. It is mainly used in Fedora, RedHat, Centos and other distributions.

In the blog I wrote about configuring the environment before, I wrote about changing the domestic Ali source, which refers to the yum source. If we use the default foreign source, we may not be able to connect, or the network speed is extremely slow. About changing the source You can take a look at my blog Linux history and environment construction

We can first look at the yum source on our own Linux, and enter ls /etc/yum.repos.d
insert image description here
CentOS-Base.repo as the default source

1.2 Common commands of yum

Command yum listto view the list of software packages contained in all your sources
insert image description here
Command yum search 软件名称to view the software packages we are looking for
insert image description here
This is actually not easy to search when the amount of data is large, so we can replace it with yum list | grep 软件名称
insert image description here
Then we can install this software, enter the command yum install -y links, need Note that for non-root user installation, remember to add sudo (as shown in the figure below) or switch to root user. The -y in the middle can be omitted, and the addition is to skip some packages and let us confirm during the installation process.
insert image description here
If we don’t need this software, we can also delete it through the command, enter the command yum remove -y links, the same, non-root user installation remember to add sudo (as shown below) or switch to root user
insert image description here

1.3 Fun yum package

Small train
yum install -y sl
insert image description here
parameters :
-c #display faulty small trains
-F #bottom-up small trains

If you are interested, you can search for interesting yum commands on the Internet

2. Linux development tools

When using the Windows platform, most of the development tools we use are integrated development environments (also known as IDEs)

Integrated Development Environment (Integrated Development Environment, IDE for short, also known as Integration Design Environment, Integration Debugging Environment) is an application software that assists program developers to develop software. Inside the development tool, it can assist in writing source code text, compiling and packaging become usable programs, and some can even design graphical interfaces.

An IDE typically includes a programming language editor, build automation tools, and often a debugger. Some IDEs include compilers/interpreters, such as Microsoft Visual Studio, while others do not, such as Eclipse, SharpDevelop, etc. These IDEs implement code compilation by calling third-party compilers. Sometimes an IDE also includes a version control system and tools for designing graphical user interfaces. Many modern IDEs that support object orientation also include class browsers, object viewers, and object structure diagrams. Although some IDEs currently support multiple programming languages ​​(such as Eclipse, NetBeans, Microsoft Visual Studio), in general, IDEs are mainly tailored for specific programming languages ​​(such as Visual Basic, Spyder)

However, in the development under the Linux system environment, the tools used are not integrated, but the tools for writing code, compiling tools, and debugging tools are all separated. Next, we will introduce the first tool

2.1 The origin of the vim tool

insert image description here

Vim is a text editor developed from vi. Its code completion, compilation, error jumping and other convenient programming functions are particularly rich, and are widely used among programmers. Tied with Emacs as the favorite editor for Unix-like system users.

The first version of Vim was released by Bram Miller in 1991. The initial abbreviation was Vi IMitation. With the continuous increase of functions, the official name was changed to Vi IMproved. It is now free software released under the open source code.

When Bram Miller bought his Amiga computer in the late 80s, the Amiga didn't have vi, his favorite editor. Bram developed version 1.0 of Vim, starting with Stevie, an open source vi clone. The original goal was to completely replicate the functionality of vi, and Vim at that time was short for Vi IMitation (simulation). In 1991 Vim version 1.14 was included in "Fred Fish Disk #591", a collection of free software for the Amiga. Version 1.22 of Vim was ported to UNIX and MS-DOS in 1992. Since then, Vim's full name has become Vi IMproved (improved).

Since then, Vim has added countless new features. As the first milestone, the version 3.0 in 1994 added the multi-window editing (split window) mode, which can edit multiple files at the same time in the same terminal. Vim 4.0, released in 1996, was the first to utilize a GUI (Graphical User Interface). In 1998, version 5.0 of Vim added the highlight (syntax highlighting) function. The Vim 6.0 version in 2001 added functions such as code folding, plug-ins, multi-language support, and vertical split windows. The Vim 7.0 version released in May 2006 added new features such as spell checking, context-sensitive completion, and tab page editing. Vim 7.2, released in August 2008, merged all the fixes since Vim 7.1, and added support for floating-point numbers in scripts.

2.2 vim mode

Vim derived from vi has multiple modes, and this unique design can easily confuse beginners. Almost all editors have two modes of inserting and executing commands, and most editors use a completely different method from Vim: command list (mouse or keyboard driven), key combinations (usually through the control key (CTRL) and alt key (ALT)) or mouse input. Vim, like vi, switches between these modes only with the keyboard. This allows Vim to avoid menu or mouse operations, and minimize the operation of key combinations. It can greatly enhance speed and efficiency for text entry staff or programmers.

Vim has 6 base modes and 5 derived modes

①Basic mode

Normal Mode
In normal mode, the editor commands used, such as moving the cursor, deleting text, and so on. This is also the default mode after Vim starts. This is exactly the opposite of what many new users expect to do (the default mode for most editors is insert mode).

Vim's powerful editing capabilities come from its normal mode commands. Normal mode commands often require an operator ending. For example, the normal mode command "dd" deletes the current line, but the first "d" can be followed by another movement command to replace the second "d". For example, the "j" key to move to the next line can delete the current line. line and the next line. In addition, you can also specify the number of command repetitions, "2dd" (repeat "dd" twice), and the effect of "dj" is the same. Users have learned a variety of commands for moving/jumping between texts and other common mode editing commands, and if they can be used in combination flexibly, they can edit text more efficiently than those without mode editors.

In normal mode, there are many ways to enter insert mode. The more common way is to press the "a" (append/addition) key or the "i" (insert/insert) key.

Insert Mode
In this mode, most keystrokes insert text into the text buffer. Most new users expect this mode to remain in the text editor while editing.

In insert mode, you can press the ESC key to return to normal mode.

Visual Mode
This mode is similar to normal mode. But the move command enlarges the highlighted text area. A highlighted area can be a character, a line, or a block of text. When executing a non-moving command, the command will be executed to this highlighted area. Vim's "text objects" can also be used in this mode as well as the move commands.

Selection Mode
This mode is similar to the behavior of a modeless editor (the way Windows standard text controls do). In this mode, you can use the mouse or the cursor keys to highlight the selected text, but if you enter any character, Vim will replace the selected highlighted text block with this character, and automatically enter the insert mode.

Command line mode
In command line mode you can enter text that will be interpreted and executed. Examples include executing commands (":" key), searching ("/" and "?" keys) or filtering commands ("!" key). After the command is executed, Vim returns to the mode it was in before command line mode, usually normal mode.

Ex mode
This is similar to the command line mode, before using the ":visual" command to leave Ex mode, you can execute multiple commands at a time

②Derived mode

Operator wait mode
This derived mode refers to the normal mode, after executing an operation command, Vim waits for an "action" to complete the command. Vim also supports the use of "text objects" as actions in the operator waiting mode, including "aw" a word (a word), "as" a sentence (a sentence), "ap" a paragraph (a paragraph) and so on.

For example, "d2as" deletes the current and next sentence in normal mode. "apU" capitalizes all letters in the current paragraph in visual mode.

Insert normal mode
This mode is entered when pressing the ctrl-o key in insert mode. Temporarily enter normal mode at this time, after executing a command, Vim returns to insert mode

Insert visual mode
This mode is started when pressing the ctrl-o key in insert mode and starting a visual selection. Vim returns to insert mode when the visible area selection is canceled.

Insert selection mode
Usually this mode is entered by mouse dragging or shift arrow keys in insert mode. When the selection area is canceled, Vim returns to insert mode.

Replace mode
This is a special insert mode, in which you can do the same operations as insert mode, but each input character will overwrite the existing characters in the text buffer. Press the "R" key to enter in normal mode.

As a beginner, we need to understand and master the three modes of vim (in fact, there are many modes, you can master these three at present), which are command mode, insert mode and last line mode. ), the functions of each mode are distinguished as follows:

Normal/Normal/Command mode (Normal mode)
controls the movement of the screen cursor, deletes characters, words or lines, moves and copies a section and enters Insert mode, or goes to last line mode

Insert mode (Insert mode)
Only in Insert mode can text input be done, press the "ESC" key to return to the command line mode.
This mode is the most frequent editing mode we will use later .

Last line mode (last line mode)
saves or exits the file, and can also perform file replacement, find strings, list line numbers and other operations. In command mode, press shift+: to enter this mode
. To view all your modes: open vim, enter the bottom line mode directly:help vim-modes

2.3 Basic operation of vim

insert image description here
insert image description here
Enter vim, enter vim and the file name in the system prompt symbol, and then enter the vim full-screen editing screen vim test.c
or enter directly vim
insert image description here
. mode] to be able to input text.

[Normal Mode] Switch to [Insert Mode]
Input a
Input i
Input o
insert image description here
[Insert Mode] Switch to [Normal Mode]
Currently in [Insert Mode], you can only input text all the time. If you find that you have entered a wrong word, you want to use the cursor key to move back to delete the character, you can press the "ESC" key to enter [normal mode] and then delete the character. Of course, it can also be deleted directly.
insert image description here
[Normal Mode] Switch to [Last Line Mode]
"shift + ;", in fact, enter ":"
insert image description here
to exit vim and save the file. In [Normal Mode], press the ":" colon key to enter "Last line mode", For example:
: w (save the current file)
: wq (enter "wq", save and exit vim)
: q! (enter q!, force exit vim without saving)

2.4 vim normal mode command set

Insert mode
Press "i" to switch to insert mode "insert mode". Press "i" to enter insert mode to start inputting files from the current position of the cursor; press "
a" to enter insert mode to enter the next file from the current cursor position Enter the text at the position;
press "o" to enter the insert mode, a new line is inserted, and the text is entered from the beginning of the line.
Switch from insert mode to command mode
Press the "ESC" key.
Move the cursor
Vim can directly use the cursor on the keyboard to move up, down, left, and right, but regular vim uses lowercase English letters "h", "j", "k", and "l" to control the cursor left, down, up, and right
respectively Move one grid
Here we need to explain why these 4 keys are used, because when the keyboard was originally designed, there were no arrow keys Press
insert image description here
"G": Move to the end of the article, that is, shift+g
insert image description here
insert image description here
Press "$": Move to the cursor position The "end of the line" of the line, that is, shift+4
and press "^": move to the "beginning of the line" of the line where the cursor is located, that is, shift+6 and
press "w": the cursor jumps to the beginning of the next word
insert image description here
insert image description here
insert image description here

Press "e": the cursor jumps to the end of the next word
insert image description here
insert image description here
Press "b": the cursor returns to the beginning of the previous word
insert image description here
insert image description here

Press "#l": move the cursor to the #th position of the line, such as: 5l, 56l
Press [gg]: Enter the beginning of the text
Press [shift+g]: Enter the end of the text
Press "ctrl" + "b": Go to the screen "Back" to move one page
Press "ctrl" + "f": the screen moves one page to the "front"
Press "ctrl" + "u": the screen moves half a page "back" Press
"ctrl" + "d": the screen Move half a page to the "front" and
delete the text
"x": each time you press, delete a character
"#x" at the cursor position: for example, "6x" means delete "behind (including yourself)" at the cursor position 6 characters
"X": uppercase X, each press will delete a character "#X" "before" the cursor position : for example, "20X" means delete 20 characters "dd" "
before" the cursor position :
Delete the line where the cursor is located
"#dd": Delete #line from the line where the cursor is located
Copy
"yw": Copy the characters from the cursor position to the end of the word to the buffer.
"#yw": Copy # characters to the buffer
"yy": Copy the line where the cursor is located to the buffer.
"#yy": For example, "6yy" means to copy 6 lines of text "counting down" from the line where the cursor is located.
"p": Paste the characters in the buffer to the position of the cursor. Note: All copy commands related to "y" must cooperate with "p" to complete the copy and paste functions.
Replace
"r": Replace the character where the cursor is located.
"R": Replace the character where the cursor is, until the "ESC" key is pressed.
undo last action
"u": If you execute a command by mistake, you can immediately press "u" to return to the previous operation. Press "u" multiple times to perform multiple replies.
"ctrl + r": Undo the redo
change
"cw": Change the word where the cursor is to the end of the word
"c#w": For example, "c3w" means change 3 characters
and jump to the specified line
"ctrl"+" g" lists the line number of the line where the cursor is located.
"#G": For example, "15G", means to move the cursor to the beginning of the 15th line of the article.

2.5 vim last line mode command set

Before using the end-line mode, please remember to press the "ESC" key to confirm that you are already in the normal mode, and then press the ":" colon to enter the end-line mode.
List line numbers
"set nu": After typing "set nu", the line numbers will be listed before each line in the file.
insert image description here

Jump to a line
"#" in the file: "#" means a number, enter a number after the colon, and then press the Enter key to jump to the line, such as input the number 15, and then press Enter, it will Skip to line 15 of the article.
Search characters
"/keyword": Press "/" key first, and then input the character you want to find, if the keyword you find for the first time is not what you want, you can keep pressing "
n" to find what you want of keywords.
"? Keyword": Press the "?" key first, and then enter the character you want to find. If the keyword you find for the first time is not what you want, you can keep pressing "n" to find the key you want up to the word.
Question: / and ? Find the difference between have and ? Experiment with the operation and
save the file
"w": Enter the letter "w" in the colon to save the file and leave
vim
"q": Press "q" to exit. If you can't leave vim, you can follow "q" with a "!" Force to leave vim.
"wq": It is generally recommended to use it together with "w" when leaving, so that the file can be saved when exiting.

2.6 Simple vim configuration

The location of the configuration file
Under the directory /etc/, there is a file named vimrc, which is a public vim configuration file in the system and is valid for all users. And in each user's home directory, you can create your own private configuration file, named: ".vimrc". For example, under the /root directory, there is usually a .vimrc file, and if it does not exist, it will be created.
Switch users to become yourself and execute su, enter your main working directory, execute cd ~
open the .vimrc file in your own directory, and execute vim. Vimrc
common configuration options are used for testing
. Set syntax highlighting: syntax on
display line number: set nu
Set the number of spaces for indentation to 4: set shiftwidth=4
insert image description here
insert image description here
If you are interested, you can also find other plug-in settings on the Internet. The following picture is the vim
insert image description herereference set by the author
. Vim from entry to proficiency

3. Linux compiler - gcc/g++ use

Before introducing and using, let's check the gcc and g++ versions.
Input gcc -v
insert image description here
, you can see that my system here is version 4.8.5, which is enough for beginners. Of course, you can also upgrade yourself if necessary. Then
enter g++ -v
insert image description here
here we may You will encounter the following situation,
insert image description here
which means that we have not installed g++, we can install the following to enter
, sudo yum install -y gcc-c++and then enter the password, the installation is successful when the following interface appears
insert image description here
Then install the cpp static library and enter
the commandsudo yum install -y libstdc++-static
insert image description here

3.1 Background knowledge

  1. Preprocessing (macro replacement)
  2. compile (generate assembly)
  3. Assembly (generates machine readable code)
  4. linking (generating executable or library files)

3.2 How gcc is done

format gcc [options] file to compile [options] [object file]

Preprocessing (macro replacement)
The preprocessing functions mainly include macro definition, file inclusion, conditional compilation, and comment removal.
Preprocessing directives are lines of code that begin with the # sign.
Example: gcc -E test.c -o test.i
option "-E", the function of this option is to let gcc stop the compilation process after the preprocessing is completed.
The option "-o" refers to the object file, and the ".i" file is the original C program that has been preprocessed.
insert image description here
test.c file

insert image description here

insert image description here
We can see that there are a total of 851 lines after compilation, including the expansion and copying of header files, replacing macro definitions, and directly outputting conditional compilation.

Compilation (generating assembly)
In this stage, gcc first checks the standardization of the code, whether there are grammatical errors, etc., to determine the actual work to be done in the code, and after checking that it is
correct, gcc translates the code into assembly language.
Users can use the "-S" option to view, this option only compiles but not assembles, and generates assembly code.
Example:gcc -S test.i -o test.s
insert image description here
insert image description here

Assembly (generating machine recognizable code)
The assembly stage is to convert the ".s" file generated in the compilation stage into an object file.
Readers can use the option "-c" here to see that the assembly code has been converted into a ".o" binary Object Code
Example: gcc -c test.s -o test.o
insert image description here
insert image description here
Binary Viewerod test.o
insert image description here

After linking (generating executable files or library files)
is successfully compiled, it enters the linking phase.
Example:gcc test.o -o test
insert image description here
insert image description here

An important concept is involved here: function library
In our C program, the function implementation of "printf" is not defined, and there is only the declaration of this function in "stdio.h" included in the precompilation, but no definition The implementation of the function, then, where is the "printf" function implemented? The
final answer is: the system implements these functions in the library file named libc.so.6. When there is no special designation , gcc will search under the system's default search path "/usr/lib", that is, link to the libc.so.6 library function, so that the function "printf" can be realized, and this is the function of the link

The function library is generally divided into two types: static library and dynamic library.
Static library means that when compiling and linking, all the code of the library file is added to the executable file, so the generated file is relatively large, but the library is no longer needed at runtime. file up. Its suffix is ​​generally ".a". On the contrary, the code of the library file is not added to the executable file when compiling and linking, but the library is loaded by the runtime link file when the program is executed, which can save system overhead. The general suffix of the dynamic library is ".so", as mentioned above, libc.so.6 is the dynamic library. gcc uses dynamic libraries by default when compiling. After the link is completed, gcc can generate the executable file, as shown below.
gcc hello.o –o hello
Let’s first check our gcc’s dependent library.
First, enter ls /lib64/libc.*
the following.
insert image description here
If it is the following situation, don’t panic.
insert image description here
In this case, there is a lack of static library. We can install it and
enter the following command sudo yum install glibc-static insert image description here
After the installation is complete, you can check it again

The binary program generated by gcc by default is dynamically linked, which can be verified by the file command.
insert image description here
We can see that the test file processed here depends on the dynamic library of c
insert image description here
, so how do we use static linking?
We enter the following code gcc test.c -o tests -static
insert image description here
and we can see that the statically linked file is much larger than the dynamically linked file, we analyze their differences as follows

  1. Dynamic linking is to fill in the address of the function we need into my executable program and establish an association, which can save resources
  2. Static linking is to copy the entire library you need into our executable program, which takes up a lot of resources

Summary of gcc options
-E only activates preprocessing, this does not generate a file, you need to redirect it to an output file
-S compile to assembly language without assembly and link
-c compile to object code
-o file output to file
- static This option applies static linking to the generated files
-g Generates debug information. The GNU debugger can use this information.
-shared This option will try to use the dynamic library as much as possible, so the generated file is relatively small, but the system needs the dynamic library.
-O0
-O1
-O2
-O3 The 4 levels of compiler optimization options, -O0 means no optimization, -O1 is The default value, -O3 is the highest optimization level
-w does not generate any warning messages.
-Wall Generate all warning messages.

4. Linux debugger -gdb use

4.1 Background

There are two ways to release the program, debug mode and release mode
. The binary program from Linux gcc/g++ is the default release mode
. To use gdb debugging, you must add the -g option when generating the binary program from the source code

4.2 Getting Started

gdb binFile Exit: ctrl + d or quit Debugging command:
list/l Line number: Display the source code of binFile, then go down to the last position, and list 10 lines each time.
list/l function name: List the source code of a function.
r or run: Run the program.
n or next: single execution.
s or step: Enter the function call
break(b) Line number: Set a breakpoint at a certain line
break Function name: Set a breakpoint at the beginning of a certain function
info break: View breakpoint information.
finish: Execute until the current function returns, and then stand down and wait for the command
print§: Print the value of the expression, you can modify the value of the variable or call the function
p variable through the expression: print the value of the variable.
set var: modify the value of the variable
continue (or c): execute the program continuously from the current position instead of single-step
run (or r): execute the program continuously from the beginning instead of single-step
delete breakpoints: delete all breakpoints
delete breakpoints n: Delete the breakpoint whose serial number is n
disable breakpoints: disable the breakpoint
enable breakpoints: enable the breakpoint
info (or i) breakpoints: see which breakpoints are currently set
display variable name: track and view a variable, and display its name every time it stops value
undisplay: untrace those variables that were previously set
until X line number: jump to X line
breaktrace (or bt): View function calls and parameters at all levels
info (i) locals: View the value of local variables in the current stack frame
quit: Exit gdb

4.3 Understanding

First we create a test.c file, and then compile it, the code is as follows:

insert image description here
But what we need to know is that in the gcc or g++ environment, the dynamic link release executable file is generated by default, that is to say, it cannot be debugged. We can add
-g when compiling, that is, the way gcc test.c -o testdebug -g
we can pass readelf -S 文件名Check the difference between the two executable files.
The test executable file generated by default. readelf -S test
insert image description here
The debug file generated. readelf -S testdebug
insert image description here
You can see that the second file contains debugging information, but the first one does not, so the second file can be debugged with gdb

The first step is to enter the command gdb testdebugto start debugging.
insert image description here
It should be noted that gbd will record the latest command. If there is no change in the command, you can directly press Enter.
For example, we will first display all the codes and print the l 0
insert image description here
input from the beginning. Enter command b 行号breakpoint
insert image description here
Enter command info bto view breakpoint
insert image description here
Enter command d 断点编号Delete the breakpoint, remember that it is not the line number but the num number in the breakpoint.
insert image description here
Then we enter the command rto start debugging. If the breakpoint is not set, the operation will end directly. Then
insert image description here
we set a breakpoint before executing the function, and the input command sis step by step Statement, input nis process-by-process
insert image description here
Input command btto view the current call stack
insert image description here
Input command p 变量名to print variable content, or command p &变量名to view address
insert image description here
If you want to always display input commands display 变量名, cancel the constant display of input commands undisplay
insert image description here
insert image description here
Enter commands finishto run the current function directly and then stop
insert image description here
Or you can also enter commands until 行号Jump directly to a certain line.
insert image description here
For example, let’s hit a few more breakpoints below, and execute the command to crun directly to the next breakpoint and stop.
insert image description here
insert image description here
Execute the command disable/enable 断点编号to close/open the breakpoint.
insert image description here
Finally, execute the command quitto exit the gdb debugging tool
insert image description here
. All the basic gdb debugging commands are these La!

5. Linux project automation build tool - make/Makefile

5.1 Background

Whether you can write a makefile, from one side shows whether a person has the ability to complete large-scale projects

The source files in a project are not counted, and they are placed in several directories according to type, function, and module. The makefile defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files need to be re-compiled. Compile, and even perform more complex functional operations

The benefit brought by makefile is - "automatic compilation". Once written, only one make command is needed, and the entire project is completely automatically compiled, which greatly improves the efficiency of software development.

make is a command tool that explains the instructions in the makefile. Generally speaking, most IDEs have this command, such as: make in Delphi, nmake in Visual C++, make in GNU under Linux. It can be seen that makefiles have become a compilation method in engineering.

make is a command, makefile is a file, and the two are used together to complete the automatic construction of the project.

5.2 Understanding

First of all, we first create a Makefile file in the same directory as the c file (the first letter can be uppercase or lowercase)
input touch Makefile
insert image description here
We open the Makefile file, enter the following information, save and exit

  1 tests:test.c
  2   gcc test.c -o tests 
  3 .PHONY:clean
  4 clean:
  5   rm -f tests

insert image description here

The first line of test in this code is the Makefile command defined by ourselves, pointing to the test.c file, which is a dependency relationship. Only when the corresponding instruction is executed, the corresponding dependent method will be executed. The .PHONY in the third line is Pseudo-target definitions that allow the clean command to be executed repeatedly.
Let's see how to use these two makefile commands.
If we enter directly make, the first command will be executed by default.
insert image description here
Of course, we can also usemake tests
insert image description here

Let's execute the second command. make clean
insert image description here
What we need to pay attention to here is that if the first command is used repeatedly, the following situation will appear.
insert image description here
This is because the modification time of our tests file is later than the test.c file, so it defaults to the tests file No need to regenerate.
insert image description here
If we modify the test.c file and call the first command, it can be called again. You can
insert image description here
see that the latest modification time of the test.c file is later than the tests file. At this time, we call the make command again
insert image description here

5.3 Example code

First, we create three files, as follows:
test.h

  1 #pragma once 
  2 #include<stdio.h>
  3 
  4 extern void show(); 

test.c

  1 #include"test.h"
  2 void show()
  3 {
    
    
  4   printf("hello linux!\n");                                                                                                                                                                                                      
  5 }   

code.c

  1 #include"test.h"
  2 
  3 int main()
  4 {
    
    
  5   show();                                                                                                                                                                                                                        
  6   return 0;                                                                                                            
  7 }  

Let's modify the Makefile again, as follows:

  1 test:test.o code.o
  2   gcc -o tests test.o code.o
  3 test.o:test.c
  4   gcc -c test.c -o test.o
  5 code.o:code.c
  6   gcc -c code.c -o code.o
  7 
  8 .PHONY:clean
  9 clean:
 10   rm -f *.o tests  

Save and exit.
We enter the command make
insert image description here
to execute the command. make clean
insert image description here
The initial test command corresponds to two .o files, but there are no files in the directory, so it will continue to search for dependencies and execute dependent methods.

5.4 Principle

How make works, in the default way, that is, we only enter the make command. So,

  1. make will look for a file named "Makefile" or "makefile" in the current directory.
  2. If found, it will find the first target file (target) in the file. In the above example, he will find the "tests" file and use this file as the final target file.
  3. If the tests file does not exist, or the file modification time of the test.o and code.o files that the tests depend on is newer than the tests file (you can use touch to test), then it will execute the commands defined later To generate the tests file.
  4. If the test.o and code.o files that tests depend on do not exist, then make will find the dependencies of the target test.o and code.o files in the current file, and if found, then generate test.o according to that rule and code.o file. (This is a bit like a stack process)
  5. Of course, your C file and H file exist, so make will generate test.o and code.o files, and then use test.o and code.o files to declare the ultimate task of make, which is to execute the file tests .
  6. This is the dependency of the entire make, and make will find the dependencies of the files layer by layer until the first target file is finally compiled.
  7. In the process of searching, if there is an error, such as the last dependent file cannot be found, then make will directly exit and report an error, and make will ignore the error of the defined command or the failure of compilation.
  8. make only cares about the dependencies of the files, that is, if the files after the colon are still not there after I find the dependencies, then I'm sorry, I won't work.

5.5 Project cleanup

  1. Projects need to be cleaned up
  2. Like clean, if it is not directly or indirectly associated with the first target file, then the commands defined behind it will not be automatically executed, but we can display make execution. That is, the command - "make clean" to clear all target files for recompilation.
  3. But generally we set the clean target file as a pseudo-target and modify it with .PHONY. The feature of the pseudo-target is that it is always executed.
  4. We can declare our tests target file as a pseudo-target and test it.

5.6 The first small program in Linux - progress bar

First we create a Makefile and edit and save it

  1 proc:proc.c
  2   gcc -o proc proc.c
  3 .PHONY:clean
  4   rm -f proc  

Create proc.c file

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    
    
 	int i = 0;
 	char bar[102];
 	memset(bar, 0 ,sizeof(bar));
 	const char *lable="|/-\\";
 	while(i <= 100 ){
    
    
 		printf("[%-100s][%d%%][%c]\r", bar, i, lable[i%4]);
 		fflush(stdout);
 		bar[i++] = '#';
 		usleep(10000);
	}
 	printf("\n");
 	return 0;
}

Save and execute make
insert image description here
to run proc
insert image description here

6. The use of git on Linux

First, we enter on the command line git -vto check whether git is installed.
insert image description here
If it is not installed, execute it first. sudo yum install -y git
Because the author uses GitHub managed code, if it is gitee, the method is similar.
First, we first create a project warehouse to test that
insert image description here
these two options are used to build New project warehouse
insert image description here
Next, we copy the local link of the warehouse
insert image description here
Next, we return to Linux and perform the first step git clone 刚刚复制的仓库链接
insert image description here
Next, we visit the directory with the same name
insert image description here
Now we can upload our own code, the first step is to upload the code to the local
git add test1.c
and then write the submission log
git commit -m "这是一个测试代码"

insert image description here
Then push to upload, the input git push
insert image description here
needs to be noted that during the push process, you need to enter the user name and password, but now GitHub does not support password push, so we need to create a token, use the token instead of the password to successfully push (getee user does not need)


insert image description here
Swipe down to find the token settings
insert image description here
because I created it before, so it may be a little different. There should be only one button if I haven’t created it. It may be

directly the following page
insert image description here
insert image description here
. Remember to copy it and put it in Notepad or other places where you can record it. , the next time you come in, you
insert image description here
won’t be able to see it. You can use this token directly for the next upload.
insert image description here
insert image description here
There is another situation. If we change the content of the warehouse on GitHub on other devices, we may encounter the following when we directly push The
insert image description here
GitHub warehouse has another file, but my local warehouse does not. At this time, we create a new file to upload and see what happens. We can see that
insert image description here
our upload failed. This is because there is a discrepancy between the local warehouse and the platform. In the case of a match, that is, a conflict has occurred. At this time, we have no brains
git pull
insert image description here
and then upload
insert image description here
insert image description here
it. We can edit and modify the .gitignore file by ourselves. The main purpose is to filter out unnecessary files when uploading in the form of a directory file
insert image description here
insert image description here
. Add two information with suffixes of .x and .X. Next, we create a directory containing some files.
insert image description here
We can see that when the commit command is executed, the .x and .X files have been ignored locally.
insert image description here
Let’s look at the GitHub warehouse again. not uploaded
insert image description here

epilogue

Interested friends can pay attention to the author, if you think the content is good, please give a one-click triple link, you crab crab! ! !
The 22,000-word article on Linux took me a lot of time and energy! ! !
It is not easy to make, please point out if there are any inaccuracies
Thank you for your visit, UU watching is the motivation for me to persevere.
With the catalyst of time, let us all become better people from each other! !
insert image description here

Guess you like

Origin blog.csdn.net/kingxzq/article/details/131607570