[Linux Advanced Road] Basic Development Tools

Preface

Before we officially start, let's add one knowledge point - the operation of ordinary users to escalate privileges.

sudo 【指令】

Insert image description here

  • So how to add tmp1 to the trust whitelist-sudoers file?

You definitely can’t add it yourself, you have to add it as super administrator root—— 第一步:切到超级用户.

Find the sudoers file -/etc/sudoers

第二步:编辑此文件

vim /etc/sudoers

第三步: 在大概一百行左右会出现这样的标志。

Additional knowledge:

如何显示行号呢?
:set nu

Insert image description here

第四步:复制粘贴修改用户名即可

Insert image description here
第五步:强制保存并退出
Supplement: Because of this file 不具备写权限, I have to force save and exit.

:wq!

If you switch to the tmp1 account and perform the operation, you will see that root is displayed when sudo is executed as a normal user and whoami is executed.
Insert image description here

1. Compiler——gcc/g++

Install

Install gcc instructions

yum install -y gcc

Install g++ instructions

yum install -y gcc-c++ libstdc++-devel

Basic use

 1. Let’s first use vim to write a piece of code like this.
Insert image description here

 2. Directly compile and generate executable programs.

	gcc test.c

Insert image description here
The executable program here a.outis generated by default. Is there any instruction that can change the name of the final executable program? ——Redirect

gcc test.c -o test

Insert image description here
Note: The -o option must be followed by the target file, because the meaning of redirection is to redirect the new content generated by file processing to another target file.

With this instruction, subsequent preprocessing, compilation, assembly, and linking will be easy.

We have talked about these contents in previous articles, so we will not go into details. They are mainly used in combination with instructions. If you are interested, 自行点击check out the following articles.

[Advanced C Language] Programming Environment and Preprocessing
[In-depth Analysis of C Language] Preprocessing (Full)

Expand your knowledge of compilation:
Insert image description here
The preparations are completed, let’s start using instructions!

① Execute the file until the preprocessing is completed and then stop.

instruction

gcc - E test.c -o test.i

Insert image description here

  • Next, let’s take a look at what’s in the generated file.
    Insert image description here

② Process the file and stop when compilation is completed.

instruction

gcc -S test.c -o test.s

Insert image description here

  • Let’s take a look at what’s in the generated file?
    Insert image description here

③ Process the file and stop when it is assembled.
instruction

gcc -c test.c -o test.i

Insert image description here

  • Let's use vim to view this file.
    Insert image description here

The result is garbled code because vim is a text editor and this file is a binary file and cannot be read.

We can view it in binary mode.

od test.o

result:
Insert image description here

Even though it’s in binary form, I still can’t understand it, so bad~


At this point our preprocessing stage is complete, and the remaining stage is linking.

 We must first think clearly why we need to link, that is, what does the link do? Linkage links the declaration of a function to the definition of the function, such as a library function, 头文件里面有函数的声明where 定义is that? Isn't it just added during linking? The place where library functions are defined is called a link library.

 The link library is divided into 动态链接库and 静态链接库.

  • The dynamic link library, that is, all programs 公用一份代码, is convenient and saves space, but once the link library is deleted, all programs will not be able to run!
  • Static link libraries mean that all programs have them 拷贝一份代码自己用. Although the library will run normally after deletion, it will make the code space unusually large, usually around 几十倍到几百倍.

Installation of static libraries

gcc

yum install -y glibc-static

g++

yum install -y libstdc++-static

illustrate:

Generate executable programs under Linux 默认使用动态库.
To use a static library, you need to add the -static option.
The library is usually in the lib64 directory, with a .so. number as the suffix.
Check the libraries used by the executable program -ldd 【文件】

How to verify that the static link space is large?
Insert image description here

Use the ll command to see the generated size
Insert image description here

Add debugging information

instruction

gcc test.c -g

result:
Insert image description here

2. Project automatic construction tools - make and makefile

Concept explanation

 Let's first understand make and makefile. Make is an instruction with the function of 找到makefile,并执行相应的指令makefile. It 文本文件stores the instructions for building the project, that is 依赖关系和依赖方法.

  • Why do we need to learn this tool?

When we need to generate an executable program, there are a lot of source files, no more than a few hundred. Would you rather spend a few minutes typing each time to generate an executable program, gcc命令or use make and makefile in a few seconds? The answer is nothing. Being proficient in using these two tools 对大型项目的掌控能力also improves us to a certain extent 效率.

  • Some friends may ask, since the makefile is a text file, can we replace it with another name?

The answer is yes, but it is limited to these names “GNUmakefile”、“makefile”、“Makefile”, and 优先级从前往后it is recommended to use them “Makefile”(more conspicuous). I can’t find any other name for make!

  • What do makefile and make usually involve?

The makefile mainly deals with the relationship between source files and executable programs, and the tools involved are 编译器和链接器.

Let's do it next, using our code above.

Basic use

① First we create a Makefiletext file

Insert image description here
② Use vim to edit and add 依赖关系和依赖方法
Insert image description here
: The next line of each dependency 必须隔一个Tab键才能写依赖方法
is as simple as this! Let's save and exit to use it.
Insert image description here
The usage is: make plus the required operation, that is :左边的部分, of course, you can also not write it. In this way, the instruction corresponding to the first dependency will be executed by default.

How to verify this? Let's clean up the project a bit.
Insert image description here
Directly add no content after make.
Insert image description here
What is executed is the instruction in our first dependency in the Makefile.

Can we make it again?

Insert image description here
The result is something like this: test.exe is already there 最新的.

Why does such content appear? Or what is this 原理?

Since the make command says that test.exe is the latest, then there is a comparison of time. Who should it be compared with? It must be a comparison of the time information of the source files. How to see the time information?

指令:stat 【文件】

Insert image description here
As long as the modification time of the test.exe file is newer than the content of test.c, then there is no need to modify it. How does this compare? Remember the timestamp? 转换成时间戳比较看谁大谁小That’s it.

So what if we want to update the test.c time?

指令:touch 【文件】 

Insert image description here
Let's make it again.
Insert image description here
At this point it is successfully regenerated.

So what if we want to make it at any time and execute it successfully?

Then you need relationships——.PHONY : 【目标文件】

Insert image description here

We save and exit and execute make multiple times.
Insert image description here
This way make will be executed no matter when.

But generally we write like this~

Insert image description here
Here are two more small operations~

①Do not display the contents of make dependencies.
Insert image description here

②Replace - $@used to replace :the content on the left, $^used to replace :the content on the right.

Insert image description here
Let’s take a look at the effect:Insert image description here

3. Progress bar applet

basic concept

Combined with the previous ones, vim 、gcc、make与makefile we can write a small program on Linux!

First we take a look at the final version.

Insert image description here
Not much to say, let’s get started and play is real!

First we look at a fixed format, divided into 进度条部分,显示的进度,光标three parts.

First of all, the first problem we have to solve is-how to keep the progress printed on the same line?

Two concepts are involved here - line feed and carriage return.

Insert image description here

This means that our Enter key in C language \nand on the keyboard actually involves two operations, Enter + Line Feed.

How do you express the carriage return operation? \r
There is another concept - buffer zone.

1. When the program is opened, three streams will be opened by default, the default output stream (stdout), the default input stream (stdin), and the default error stream (stderr).
2. In Linux, the buffer can be understood as a character array that is not too large. When the character array is full, or the program ends, or when characters such as \n are encountered that will refresh the buffer, it will automatically Contents are output and reinitialized.
3. In Linux, when we need to force a refresh, we need the fflush function. The parameter is the stream that needs to be refreshed, and the header file is stdio.h. The stream we need to refresh here is stdout.

Then here we have the basic ability to write a progress bar.

operate

Let’s first write a countdown to practice a little bit.

Before writing the program, we need to build the framework of the project.

Insert image description here
Insert image description here
Insert image description here
Let’s write a countdown first.
Insert image description here

OK, let's write another simple progress bar.

The main functions we are processbar.cimplementing

Insert image description here
There is still a problem of rotating the cursor and color and arrows.

The same goes for rotating the cursor - we can keep overwriting - | / \to achieve the effect.

I will list some of the color parameters we use here for ease of use.

#define NONE         "\033[m"//这个算是结束标志
#define RED          "\033[0;32;31m"
#define LIGHT_RED    "\033[1;31m"
#define GREEN        "\033[0;32;32m"
#define LIGHT_GREEN  "\033[1;32m"
#define BLUE         "\033[0;32;34m"
#define LIGHT_BLUE   "\033[1;34m"
#define DARY_GRAY    "\033[1;30m"
#define CYAN         "\033[0;36m"
#define LIGHT_CYAN   "\033[1;36m"
#define PURPLE       "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN        "\033[0;33m"
#define YELLOW       "\033[1;33m"
#define LIGHT_GRAY   "\033[0;37m"
#define WHITE        "\033[1;37m"

So how to achieve the flashing effect? We can progress as 偶数时打印一种颜色,为奇数打印另外一种颜色.

So how would there be an arrow? In fact, every time you write, write one more arrow and overwrite it next time.
Supplementing these functions, our code looks like this:

head File:
Insert image description here

process.c file
Insert image description here
As for some more practical uses, I won’t go into details here. What we have learned so far is a callback function.

4. Version controller - git

story background

  • Since Linus needs to do a lot of version maintenance work when maintaining Linux, in order to improve efficiency, it is urgent to use tools for managing versions. The preliminary work was supported by a company that operates version management tools, but the talents of the Linux maintenance community are too strong. After trying to crack this tool and destroying the company's job, the boss of the company revoked the right to use it. Linus didn't want to do the original heavy work anymore, so he wrote the first version of open source within a few weeks. git, and continues to develop in the Linux community, so we have today's convenient and easy-to-use git.

  • Nowadays, our domestic websites gitee and github are both commercial versions based on git, which is very similar to some Linux operating systems developed based on the Linux kernel as we mentioned before.

Function

  • A distributed version control software with network functions.

Basic use

If you are using git on Windows, you can watch this tutorial:

Registration and code submission of gitee (code cloud) [step by step]
Note: The premise of reading this article is 已经安装好git.

Below we have a step-by-step tutorial on Linux.

  • Step one: Register an account on gitee and create a new warehouse.

Insert image description here

This is the repository I created.

Insert image description here
In addition to C language, such as C++, you can set the sum to C++ 初始化仓库there .语言.gitignore

  • Step 2: Check whether git software is installed on Linux.

instruction

git --version

If the picture below is displayed, it means it has been installed.

Insert image description here
If there are other circumstances, let's install git

root账号Execute the following command below.

yum install -y git
  • Step 3: Pull the remote warehouse to the local warehouse

Insert image description here
Then execute the following command under Linux.

git clone 【复制的内容】

Then enter your gitee account and password, this is mine

Insert image description here
After entering, the warehouse will be cloned locally.

I moved the Linux applet written above to the local warehouse.

Then we started uploading the written applet to the remote warehouse.

Execute command ①

git add .//这里的 . 的意思是将新增的内容添加到本地仓库

Execute command ②

git commit -m "添加的文件的信息"//也就是说你干了啥

The following message may appear here.

Insert image description here
What I write here is my email and account number.
Insert image description here

Execute command ③

git push

Insert image description here
Note: If the version is too old, we can execute the installation command under the root account.

Check your warehouse on gitee to see if anything has been uploaded, and if there is a little green dot, you can check whether the upload was successful!

Insert image description here
Obviously, the upload was successful.


Practical little operations ①——View log information

git log

Here is the log of the progress bar I just uploaded:
Insert image description here
Practical little operation ② - filtering unwanted files

Edit this file:
Insert image description here
add the file suffix you want to filter:
Insert image description here
save and exit.

5. Debugger-gdb

Basic instructions

In order to better understand gdb, here is a sample code for debugging.

Debug code:
Insert image description here
Makefile content:
Insert image description here

  • We generate the executable program and view the debugging information.

Insert image description here

  • Let's start the gdb program again for debugging.

Basic use

Prerequisite: First make sure you download gbd.
Download instructions:

yum install -y gdb

①Start gdb

instruction:

gdb 【文件】

Insert image description here

②View code

instruction:

l 【行号】

Insert image description here

③Set breakpoints

instruction:

b 【行号】

Insert image description here

④View breakpoint information

instruction:

info b

Insert image description here

Description: Num所在行,为断点的编号.

⑤Disable breakpoints

instruction:

disable 【断点的编号】

Insert image description here

⑥Enable breakpoints

instruction:

enable 【断点的编号】

Insert image description here

⑦Run from the beginning to the first breakpoint

instruction:

r

Insert image description here

⑧Set a breakpoint in the first sentence of valid code at the beginning of the function.

instruction:

b 【函数名】

Insert image description here

⑨Run to the next breakpoint

instruction:

c

Insert image description here

⑩Process-by-process debugging (equivalent to f10 of vs)

instruction:

n

Insert image description here

⑪Statement-by-statement debugging (equivalent to f11 of vs)

instruction:

s

Note: Under normal statements, there is no difference from process-by-process debugging. When executing the function call statement, you will enter the function.

Insert image description here

⑫View the value of the variable

instruction:

p 【变量名】

Insert image description here

⑬Dynamic display of variable values ​​- monitoring window

instruction:

display 【变量名】

Insert image description here
Note: If you debug later, the value of the variable will be displayed dynamically.

The leftmost number of the variable here is its number.

⑭Cancel the monitoring window

undisplay 【变量的编号】

Insert image description here
Result: After debugging, the monitoring window will not be displayed.

⑮Run directly to a certain line (valid code) and stop

instruction:

until 【行号】

Insert image description here

⑯View function stack frame

instruction:

bt

Insert image description here

⑰ Finish running the current function

instruction:

finish

Insert image description here

⑱Delete breakpoints

instruction:

delete breakpoints
//删除所有断点

Insert image description here
instruction:

delete breakpoint 【断点编号】

Insert image description here

⑲Exit gdb

instruction:

q

Insert image description here

Note: Since this is the debugging stage, you will be asked whether you want to terminal debug and exit. After all, all breakpoint information will not be saved after exiting.

Rest of the instructions:

info (i) locals: View the value of the local variable of the current stack frame
set var: Modify the value of the variable

Detailed instructions:

list/l line number: Display the binFile source code, followed by the last position, 10 lines at a 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 wait for the command
print§: print the value of the expression, through the expression you can modify the value of the variable or call the function
p variable: 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 with serial number 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 it every time you stop Value
undisplay: Cancel tracking of previously set variables
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

Summarize

 That’s it for today’s sharing. If you think the article is good,Give it a like and encourage it.! Us 下篇文章再见!

Guess you like

Origin blog.csdn.net/Shun_Hua/article/details/131547592