[Linux] The use of common tools

        A compiler like vs2019 is an integrated compiler, a tool that combines editor, compiler, and Debug, but it is separate in Linux. The editor is the editor, the compiler is the compiler, and the Debug tool . It's not easy to use, but you have to learn how to use it.


1. vim (editor)

        Vim has three basic modes: normal mode, insert mode, and low-line mode .

        Press i in the normal mode to enter the insert mode, only in the insert mode can the text input, press ESC to return to the normal mode.

Press shift+:         in normal mode to enter low-line mode, where you can save and exit files, replace text, and search for strings.

        The bottom line mode is the same as writing commands under Linux, and it can also remember the commands entered in history.

Vim common commands:

        Open a file and use vim + file name directly , if you don't have this file, you can also enter! This file will be automatically generated after saving and exiting, so pay attention to sometimes when you open the file and find it is empty, go to see if the file name is wrong! ! ! .

        Two very commonly used operations after opening a file: w (save), q (quit), ! (force). It can be used individually or together, and it needs to be used in insert mode.

cursor:        

        

        The cursor can be moved using the arrow keys on the keyboard, but vim's default movement keys are h (left), j (down), k (up), l (right).

PS: Cleverly remember the leftmost h of the keyboard, the most right l of the keyboard, jump (falling), king (the king is on top).

gg : The cursor is positioned at the very beginning of the file.

shift + g (capital G): The cursor is positioned at the end.

PS: These two commands are very useful. Here I want to explain that when editing files in vim, you cannot use the mouse wheel to turn pages!

shift + 6 (^): Position the cursor at the very beginning of the current line.

shift + 4 ($): Position the cursor at the end of the current line.

w, b : move the cursor back and forth in units of words.  

yy : From the position where the cursor starts, copy the line where the cursor is located. You can copy multiple lines by adding a number in front of the command, for example, 6yy is to copy 6 lines from the cursor position.

p : Paste the copied data to the position of the cursor.

dd : Cut the line at the current cursor position (can also be used as delete).

shitf + ~ : Switch the selected characters to upper and lower case.

shift + r (uppercase R): replace mode, enter a new one to replace without deleting characters, and press ESC to exit. (It’s not very useful, and you have to switch modes, it’s better to delete the rewrite)

Enter a single r : replace a single character at the cursor. Enter the number in front, you can replace multiple characters together, 6r is to replace 6 characters.

x or ( shift+x ): The lowercase x is to delete the current character of the cursor, and the larger X is to delete the character in front of the cursor. Numbers can be added in front to delete multiple characters.

u : Undo operation, which can be used as Ctrl+z for usual use.

ctrl + r : Undo the operation of u. (Undo the undo operation, but note that you cannot undo your own operation, you can still use u to undo your own operation)

Bottom line command:

set nu/nonu : Turn on the line number, or cancel the line number.

vs : Turn on the comparison mode (split screen, where the cursor operates), and enter q to exit the split screen. 

ctrl + ww : Jump cursor in split screen mode.      

expand:

        Vim has a lot of operating instructions, and it is difficult to memorize them by rote. It is recommended to be proficient in adding, deleting, modifying, and checking. 

         vim needs to be configured in the file, and auxiliary functions such as inputting a character and giving possible functions need to be set by yourself.

         However, it is very troublesome to set up here, the learning cost is high, and it is not very useful. You can use the open source configuration already available on the Internet.

VimForCpp: Quickly build vim into a c++ IDE

        You only need to enter a line of commands, which is convenient and fast, and remember to give stars and praise.

Two, g++ (compiler)

        It should be noted here that gcc is a compiler specially used to compile and link C language programs. And g++ is c++, g++ can be used to compile c language, and vice versa.

        gcc -v, g++ -v view version

The compilation process of the program:

Program translation:

    

gcc mytest.c -o test 

        Here is to directly generate an executable file test from the source file, omitting other steps. It can also be disassembled to observe the difference.

gcc -E mytest.c -o mytest.i

        Generate the code preprocessing structure into the mytest.i file

-E : Translate the program from now on, stop if preprocessing is complete.

         Here is the preprocessed file, which completes header file expansion, comment removal, and macro replacement

gcc -S mytest.i -o mytest.s

-S : Translate the program from now on, and stop if the compilation is complete.

        Generate a code-to-assembler file.

gcc -c mytest.s -o mytest.o     //.o是可重定位目标文件

        After compilation , the generated binary file. This file cannot be run directly.

-c : Translate the program from now on, and stop if the compilation is complete.

 

         It's all a bunch of unintelligible binary code.

gcc mytest.o -o mytest

        The link of the program generates the executable program mytest.

 PS: The tricky way to remember ESc is the button in the upper left corner of the keyboard, and iso is the suffix of the image file.

makefile:

        Under Linux, if you have to enter so many instructions every time you compile a file, which is troublesome and inefficient, then you can use makefile here to help reduce the workload.

        make is a command, and makefile is a file, which contains the instructions for compiling and linking above. Generally, it is rarely used, and the main thing is to learn tools that automatically form makefiles, such as Cmake.

Write makefile: a, dependencies b, dependent methods

        You can directly use vim to edit Makefile. 

        

        mytest:mytest.c is the dependency , and gcc mytest.c -o mytest is the dependent method. The following clean is a pseudo-target, and generally writes instructions to delete files.

        

Entering the make command         here is equivalent to entering the command to compile and link, and the program will be compiled and linked. And enter make clean to execute the delete command to delete the generated executable file.

expand:

        But be aware that the pseudo-target is always executed , and the method will be executed every time make clean is executed. The make command, if no program changes are found, will not execute the method.

How does the makefile know that the executable program has been changed?      

        Judging according to ACM: A: access time M: modification time C: time to modify attributes

        Makefile checks the time of the source file and the executable program. If the time of the source file is later than the executable program, it means that the source code has been changed, and the executable program can be regenerated at this time.

        If it is found that the executable program is later than the source file, it means that it is the latest and cannot be regenerated.

Multi-file writing:

        Two files test.c and test.h are generated here, and then a file main.c is generated to call the functions in test.c.

 

 

        When writing Makefile, you don’t need to pay attention to the order here, and you will execute the ones that can be executed first. Here, the binary file is generated first, and then linked together to generate the executable program mytest.

         Execute the check to see if there is no problem.

Expansion: dynamic and static library

        In CentOS7, the library files are under the /lib64 directory. Generally, there are two ways to access the library: 1. Dynamic library. 2. Static library.

        The file suffixes in the Linux system are .so (dynamic library), .a (static library) , and in the windows system, .dll (dynamic library), .lib (static library)

Dynamic link: When using the method in the dynamic library, the library file is required. Occupies less resources.

Static link: copy the implementation of the method in the library to our executable program! After compiling, there is no need for library files. But it takes up more resources.

       Linux generally uses dynamic linking to generate executable programs by default . If you want to become a static link, when generating an executable program, add -static: Use static linking to generate an executable program.

gcc test.c -o mytest-s -static

PS: Generally, machines on the cloud do not have static libraries, so you have to download them yourself.

sudo yum install -y glibc-static #C静态库

sudo yum install -y libstdc++-static #C++静态库

3. gdb (debugger)

        Errors will inevitably occur when writing code. There are error messages in vs2019, and debugging can also be performed. However, if you want to debug under Linux, you need to debug separately. The debugger used here is gdb.

PS: There are two ways to release the program: debug and release. The program compiled by gcc/g++ is in the release mode by default. If you want to debug the program, you must use the debug mode.

gcc mytest.c -o mytest -g     //加上-g就是以debug方式发布的
readelf -S mytest|grep -i debug   //查看程序是不是debug模式的

         If it is in release mode, no information will appear, and if it is in debug mode, the following information will appear.

 

        Use gdb+program name to start debugging the program directly.

 Common commands:

q (quit): Quit debugging. You can also exit directly by using the shortcut key ctrl+d.

l (list): numbers can be added, and list 0 will print 10 lines of code starting from line 0.

r (run): Start debugging. If no breakpoint is set, the program will be finished (when debugging, press r again to return to the place where the breakpoint was interrupted and start debugging again).

b (break): You can add numbers, b18 is the break point on line 18. But here you can't see where the breakpoint is set.

 

info b : View breakpoints.

PS: The breakpoints here are numbered for easy distinction. A breakpoint is hit here, and the number is 1.

d (delete): d 1, is to delete the breakpoint numbered 1. Note that the number that follows is the number of the breakpoint.

n (next): Debugging program, step by step. (Like F10 in vs2019)

s (step): debugger, single step into. (Like F11 in vs2019)

p + variable name : print variable content. ()

finish : Finish the current function and stop. When entering a function, you can use this instruction to quickly complete the function.

display : Set a long display, track a variable, and display the variable content every time it stops. (The effect is more like the monitoring variable in vs2019. Every time a variable is set, it will also be given a number, just like a breakpoint.)

undisplay + number : cancel long display.

until + line number : jump the program to the specified line.

c (continue): Stop at the next breakpoint. (if multiple breakpoints are hit)

disable/enable + breakpoint number : turn off or enable the breakpoint with the specified number. (without removing breakpoints)

 

        gdb is very difficult to use. There is a length limit in the code. If a project has a lot of code, it is very troublesome and inconvenient to use.

Four, yum (downloader)

        Some of the previous software may not be available on other versions of the system, so you have to download it yourself, then it is the same as downloading software under the windows system, you need to download tools, and you can use yum under Linux.

        Then yum sometimes downloads the software and finds that there is no download speed, and sometimes it is always 0. This is because the source of yum uses the source of the external network, and there is a wall on the Chinese network that cannot access the external network well. At this time, it is necessary Set the source to some domestic open source sources.

        The source of yum is a configuration file, location: /etc/yum.repos.d

        

sudo vim /etc/yum.repos.d/CentOS-Base.repo

 

         Here is the source of Tencent, and other sources can also be used.

Yum three tricks:

yum list  //把能下载的软件罗列出来。

yum search sl  //把有sl的字符的软件搜索出来

yum list|grep sl 
yum install  //下载软件

yum install  -y sl.x86_64   //加个-y,表示不要询问

yum remove //移除软件

Five, git (version controller)

        A very common and easy-to-use tool, which can save the written code to the online warehouse, which is convenient for saving and downloading the code.        

        Why is it called a version controller? This is because at work, multiple people need to collaborate to modify and write code, and git is a tool that can divide a project into multiple branches. After checking that there is no problem with the code, merge the code into the main branch, and if the code is still If there is an error, the version can also be traced back.

        Here you can use Github to save the project, or you can use gitee (code cloud). After registering an account, create a new warehouse.

 

 

 

        Then you need the address of the warehouse here

        Finally, add the email address and password of Code Cloud in git.

//设置用户名
git config --global user.name "username"

//设置邮箱 (没有双引号)
git config --global user.email [email protected] 

//查看用户名和密码
git config user.name
git config user.email

//查看其他配置信息(git设置列表)
git config --list   

Open .gitconfig to add configuration information:

 

[credential]
    helper = store

Execute the command in the project root directory:

git config --global credential.helper store

        When you execute a git command that requires an account password, you will be prompted to enter the password. At this time, the password will be remembered , and you do not need to enter it when you perform similar operations in the future.

git clone + 自己仓库的地址        //下载仓库到本地

        Then download the newly created warehouse to the system, put the code you wrote in the warehouse, and then upload and save it.

git three tricks:

git add + 你的软件     //添加文件到你的目录 

        Submit the project.

git commit -m "这里必须写提交日志"

        When submitting changes to the local, the log must be clearly written, regardless of the length, and the details and date of the changes should be written, so that others can know what you have done when they look at your project.

 

git push   //提交

        Synchronize to the remote server.

         The first time you need to enter your account password.

 

expand:

git log //提交记录  

 

         Note that you must be in the warehouse folder to view the submission record.

git pull   //有时候无法提交,会出现提交冲突

        Merge the code in the remote warehouse with the local code.

git rm +你的软件   //删除文件,但是git仓库里面不会删除

 

The .gitignore         file will not be submitted to the warehouse. Some files in the project do not have to be uploaded, so add the suffix of the file that does not need to be uploaded here to reduce the space pressure to be uploaded.

​​​​​​​


Guess you like

Origin blog.csdn.net/weixin_45423515/article/details/126713660
Recommended