linux学习(9)——linux编程基础

1、编辑器

1.1 Vim编辑器

lenovo@lenovo-virtual-machine:~$ vim t.c
#include<stdio.h>
int main()
{
        printf("helloworld\n");
        return 0;
}
lenovo@lenovo-virtual-machine:~$ gcc t.c -o t
lenovo@lenovo-virtual-machine:~$ ./t
helloworld
## 忽略搜索的大小写set ignorecase
## 开启大小写set noignorecase
## 替换字符串,将pattern字符用string代替,ranges指替换范围,[range] s/pattern/string/[c,e,g,i]
##语法高亮
:syntax on
:syntax off
## 自动缩进set sutoindent
## 一个tab空几格set shiftwidth=4

模式切换:

命令操作
a在光标后插入
i在光标所在位置插入
o在光标所在位置的下一行插入
Esc进入命令模式
进入行命令模式

光标移动:

命令操作
h光标左移一格
l光标右移一格
j光标下移一格
k光标上移一格
^移动光标到行首
$移动光标到行尾
G移动光标到文件尾
Gg移动光标到文件头
W移动光标到下一个单词
B移动光标到前一个单词
Ctrl+f向文件尾翻一页
Ctrl+b向文件头翻一页

删除、复制和粘贴:

命令操作
x删除光标所在位置的字符
dd删除光标所在的行
D删除光标所在位置到行尾之间的字符
d普遍意义上的删除
yy复制光标所在的行
y普遍意义上的删除
p在光标所在位置粘贴最近复制的内容

撤销和重做:

命令操作
u撤销一次操作
Ctrl+R重做被撤销的操作

搜索和替换:

命令操作
:/string朝文件尾搜索字符string
:?string朝文件头搜索字符string
:s/pattern/string将pattern字符用string替换

保存和退出:

命令操作
:w保存文件
:w filename另存为filename
:q退出vim
:q!强制退出vim,放弃修改

1.2 Emacs编辑器

在这里插入图片描述

1.3 图形化编程工具

在这里插入图片描述

2、c和c++ 编译器:gcc

## gcc进行编译后,生产a.out文件,
lenovo@lenovo-virtual-machine:~$ gcc t.c 
lenovo@lenovo-virtual-machine:~$ ls
a.out  itop4412  snap  t  t.c  公共的  模板  视频  图片  文档  下载  音乐  桌面
## 运行a.ou
lenovo@lenovo-virtual-machine:~$ ./a.out
helloworld
## 指定输出文件名t
lenovo@lenovo-virtual-machine:~$ gcc t.c -o t
lenovo@lenovo-virtual-machine:~$ ls
a.out  itop4412  snap  t  t.c  公共的  模板  视频  图片  文档  下载  音乐  桌面
## 运行文件
lenovo@lenovo-virtual-machine:~$ ./t
helloworld
选项功能
-c只激活预处理、编译和汇编,生成扩展名.o的目标代码文件
-S只激活预处理、编译,生成扩展名.s的汇编代码文件
-E只激活预处理,并输出至标准输出
-g为调试程序,生成相关信息

3、调试:gdb

3.1 启动gdb

## gcc 编译文件生成a.out文件,gdb调试
lenovo@lenovo-virtual-machine:~$ gcc -g t.c
lenovo@lenovo-virtual-machine:~$ gdb a.out
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) 

3.2 获取帮助

## 进入gdb帮助文档
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
## 查看breakpoint帮助
(gdb) help breakpoint
Making program stop at certain points.

List of commands:

awatch -- Set a watchpoint for an expression
break -- Set breakpoint at specified location
break-range -- Set a breakpoint for an address range
catch -- Set catchpoints to catch events
catch assert -- Catch failed Ada assertions
catch catch -- Catch an exception
catch exception -- Catch Ada exceptions
catch exec -- Catch calls to exec
catch fork -- Catch calls to fork
catch handlers -- Catch Ada exceptions
catch load -- Catch loads of shared libraries
catch rethrow -- Catch an exception
catch signal -- Catch signals by their names and/or numbers
catch syscall -- Catch system calls by their names
catch throw -- Catch an exception
catch unload -- Catch unloads of shared libraries
catch vfork -- Catch calls to vfork
clear -- Clear breakpoint at specified location
commands -- Set commands to be executed when the given breakpoints are hit
---Type <return> to continue, or q <return> to quit---

3.3 查看源代码

## list查看程序源代码,第一次使用list显示出前10行,回车表示执行上一次命令,再显示10行
(gdb) list
1	#include<stdio.h>
2	
3	int summary( int n);
4	
5	int main()
6	{
7		int i,result;
8		result = 0;
9		for(i=1;i<=100;i++)
10			{

(gdb) 
11				result +=i;
12			}	
13		printf("Summary[1-100]=%d\n",result);
14		printf("Summary[1-450]=%d\n",summary(450));
15		return 0;
16	
17	}
18	
19	int summary (int n)
20	{
## 搜索源代码中int的行,显示第一个符合的,回车显示下一个
(gdb) search int
21		int sum =0;
## search向文尾搜索,reverse-search向文头搜索
(gdb) search int
21		int sum =0;
(gdb) 
22		int i;
(gdb) reverse-search int
21		int sum =0;

3.4 设置断点

## 设置断点,第10行
(gdb) break 10
Breakpoint 1 at 0x662: file t.c, line 10.

#3 查看break信息
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000000662 in main at t.c:10

3.5 执行和单步执行

## run表示执行操作
(gdb) run
Starting program: /home/lenovo/a.out 

Breakpoint 1, main () at t.c:11
11				result +=i;
## 可以使用clear命令清楚当前行的断点
## next表示下一步
(gdb) next
9		for(i=1;i<=100;i++)

## n也表示下一步
(gdb) n
Breakpoint 1, main () at t.c:11
11				result +=i;
##继续执行,直到遇到下一个断点,缩写c
(gdb) continue
Continuing.
Breakpoint 1, main () at t.c:11
11				result +=i;
## 另一种单步执行step,step会遇到函数调用进入函数内部,而next只是调用这个函数
(gdb) step
9		for(i=1;i<=100;i++)
(gdb) 

Breakpoint 1, main () at t.c:11
11				result +=i;
(gdb) 
9		for(i=1;i<=100;i++)

3.6 监视变量

## print监视参数,需要手动print查看
(gdb) print i
$1 = 4
(gdb) n

Breakpoint 1, main () at t.c:11
11				result +=i;
(gdb) 
9		for(i=1;i<=100;i++)
(gdb) 

Breakpoint 1, main () at t.c:11
11				result +=i;
(gdb) print i
$2 = 6
## 设置观察点,watch接受变量作参数,一旦参数发生变化,就停下
(gdb) watch i
Hardware watchpoint 2: i
(gdb) c
Continuing.

Hardware watchpoint 2: i

Old value = 6
New value = 7
0x000055555555466c in main () at t.c:9
9		for(i=1;i<=100;i++)

3.7临时修改变量

(gdb) print i
$3 = 7
(gdb) set var i=2
(gdb) print i
$4 = 2

3.8 查看堆栈

(gdb) bt
#0  0x000055555555466c in main () at t.c:9

3.9退出gdb

## 退出gdb,缩写q
(gdb) quit
A debugging session is active.

	Inferior 1 [process 3360] will be killed.

Quit anyway? (y or n) y

3.10 命令汇总

gdb命令缩写描述
helph获取帮助信息
listl 显示源代码
search向文件尾搜索代码
reverse-search向文件头搜索代码
breakb设置断点
info break查看断点信息
clear清除当前行断点
runr从头运行到第一个断点
nextn单步执行(不进入函数体)
steps单步执行(进入函数体)
continuec从当前运行到下个断点
printp打印变量的值
watch设置观察点
set var variable=value设置变量值
bt查看运行时堆栈
quitq退出gdb

4、版本控制系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值