Linux use vim editor under C program

. 1  VI / Vim use

1.1  vi / vim Profile

vi / vim editing tools are commonly used under Linux, Unix character interface, the system administrator is also commonly used editing tools. Many Linux distributions installed by default vi / vim. vim vi is an upgraded version, and the basic operation is the same as vi, with respect to the main advantage that it can display vi certain keywords, such as the C language keywords highlighted based on file type, to facilitate programming .

vi / vim has two states: the state command and editing.

1) order status: You can enter the relevant commands, such as saving a file, quit, character search, cut and other operations; when vi / vim start, enter the default command status. In edit mode, press the ESC key, enter the command state;

2) Edit state: character editor in this state. In the command state, press the i / a / I / A / O / o key to enter the edit mode and the like.

1.2  vim installed

If there is no vim installed Linux host, execute the following command (in the case can access the Internet in Linux host):

sudo apt-get install vim

Follow the prompts to install. (Press Y)

Wait a bit until you are prompted to complete the installation.

1.3  vi / vim commonly used commands

Table 1 vi / vim common commands (commands use state)

command

Function Description

Insert a character, line, after performing the following operation, enter the edit mode

a

Enter insert mode, the text is added at the rear of the cursor

i

Enter insert mode, the text added in front of the cursor

A

Enter insert mode, text is added at the end of the cursor line

I

Enter insert mode, the cursor first trekking add text (before non-null characters)

The

Enter insert mode, a new row in the cursor line

THE

Enter insert mode, the new line in the cursor line

R

Into the replacement mode, the text at the cursor covers

Cut and paste, the recovery operation

dd

Cut the cursor row

Ndd

N represents a number, N successive cut lines starting from the cursor line 

yy

Copy cursor line

Nyy

N represents a number, N successive rows copied from the start cursor line 

is

Copy the characters from the cursor to the beginning of the end of the line

Nyw

N represents a number, the cursor is copied from the beginning to the end of the word line N

and ^

Copy the characters beginning from the cursor to the beginning of the line

and $

Copy the characters from the cursor to the beginning of the end of the line

p

Paste the contents of the clipboard after the cursor (or the next line in the row, which copy the whole line)

P

Paste the contents of the clipboard before the cursor (or the row line, for the entire row copy)

in

Undo operation done by the previous step

Save, exit, open multiple files

:q!

Forced to exit without saving

:w

Save the file, use: w file, save the file as the current file

:wq

Save and exit

:new 

Create a text in the current window, use: new file, open the file file, use Ctrl + ww switch between multiple windows

Set the line number, jump

: Not set

Display line numbers, use: set nu or:! Set nonu can cancel the display line numbers 

n+

N jump down the line

n-

N line upward jump

ng

Skip line number n of rows

G

Skip to the last line

H

Jump to the first line

Find and replace

/***

Find and highlight *** string, such as / abc 

:s

: S / old / new //, old line to be replaced with a new first occurrence of

: S / old / new / g, with all of the new line to be replaced old

: N, m s / old / new / g, replaced with new lines from n to m all new

:% S / old / new / g, replace the current file with all the new old

1.4  VI / Vim uses examples

To edit the current directory named  helloworld.c  file:

step1: Input vim helloworld.c , you can enter the vim window, such as helloworld.c does not exist, the new file, or open the file. vim default in the command state.

step2: Press i, enter the edit mode.

step3: edit the code.

step4: press the Esc , return to command state.

step5: Input  : WQ , save and exit.

1.5 VIM配置

在我们的环境中,为了使VIM更好使用,建议更改vim配置文件:/etc/vim/vimrc文件。

修改时,请使用如下命令:

sudo vim /etc/vim/vimrc

 

 

注:请大家对应上面的修改,修改自身机器的配置。(上面红字是为了解释而增加的,大家不要把这些红色汉字也写入)

1.6  vi/vim常见问题及解决方法

问题1:按Ctrl+s键(Windows下的保存快捷键)后,发现vim对后续按键不再反应。原因是Ctrl+s命令在linux下是取消回显命令,所键入字符不显示在屏幕上,按Ctrl+q即可恢复回显。

问题2:在启动vim时,没有键入文件名。vim默认会创建一个新的文件,编辑完成后,进入命令状态,键入:w filename,将其保存为filename。

问题3:vim非正常退出后,再次编辑该文件时,会出现“swap file .helloworld.c.swp already exists!”的提示(假设helloworld.c是vim非正常退出时编辑的文件名),使用rm .helloworld.c.swp删除该文件,重新编辑即可。

 

Linux下C程序开发

C语言的编译器被简称为cc,不同厂商的类UNIX系统所带的C语言编译器均包含不同的功能和选项。Linux系统中,通常使用GNU C编译器,简称为gcc,下面以HelloWorld为例,简单介绍Linux下C语言开发过程。

注:本实验环境需要gcc版本为4.8以上。(可使用gcc -v查看gcc版本)

2.1 简要C语言开发过程

step1:使用vim编辑hello.c

复制代码

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello World \n");
    return 0;
}

复制代码

step2:编译hello.c

gcc -o hello hello.c

gcc命令将hello.c编译成可执行文件hello,如不加-o选项,编译器会把编译后的可执行文件命名为a.out。

step3:执行hello

./hello

在hello前面添加./,是让shell在当前目录下寻找可执行文件,如不添加./,shell会在PATH环境变量设置的目录中去寻找该可执行文件,但这些目录中通常不会包含当前目录。

:对于复杂的大型程序,一般编写makefile文件来进行编译链接,makefile文件的编写请参考相关资料。

 

1.1 gcc常用选项

gcc选项很多,下表列出gcc常用的一些选项。

 

 

表2 gcc常用选项

选项

说明

-c

只做预处理、编译和汇编,不作链接,常用于不含main的子程序

-S

只进行预处理和编译,生成.s汇编文件

-o

指定输出的目标文件名

-Idir

头文件搜索路径中添加目录dir

-Ldir

库文件搜索路径中添加目录dir

-lname

链接libname.so库来编译程序

-g

编译器编译时加入debug信息,供gdb使用

-O[0~3]

编译器优化,数字越大,优化级别越高,0表示不优化

 

使用gdb调试C程序

gdb是Unix/Linux下的一个功能强大的程序调试工具。当程序出现段错误(segment fault)或者逻辑错误时,可以使用gdb进行调试。

gdb主要有四大功能:

1、启动程序,可以按照自定义要求随心所欲的运行程序;

2、可让被调试程序在所指定的调置断点处停住(断点可以是条件表达式);

3、当程序被停住时,可以检查程序中所发生的事;

4、动态改变程序的执行环境。

2.1 gdb常用调试命令

可以使用#gdb program启动目标代码进行调试,但目标代码编译时,必须使用-g选项编译。进入调试界面后,可以输入相关gdb命令控制目标代码的运行。下表为gdb常用的调试命令。

表3 gdb常用调试命令

命令

说明

list(或l)

列出源代码,接着上次位置往下列,每次列10行

list 行号

从给定行号开始列出源代码

list 函数名

列出某个函数的源代码

break(或b)行号

在给定行号出设置断点,gdb会给出一个断点号

break 函数名

在给定函数开头设置断点

delete breakpoint 断点号

删除给定的断点

start

开始执行程序,停在main函数第一句前面等待命令

run(或r)

开始执行程序,直到遇到断点

next(或n)

执行下一条语句

step(或s)

执行下一条语句,如是函数调用,则进入函数中

continue(或c)

继续执行程序,直到遇到断点

finish

连续运行到当前函数返回,然后停下来等待命令

print(或p)

打印表达式的值

display 变量名

跟踪查看某个变量的值,每次停下来都显示该变量的值

undisplay 跟踪显示号

取消对变量的跟踪查看

set var

修改变量的值

quit

退出gdb

2.2 gdb调试示例

下面以一个例子简要说明使用gdb的调试过程。

step1:编辑源代码tst_gdb.c

复制代码

#include <stdio.h>
int add()
{
    int sum =0, i;
    for(i = 0; i < 10; i ++)
    {
        sum += i;
    }
    return sum;
}
int main()
{
    int result = 0;
    result = add();
    printf(“the result is %d \n”, result);
    return 0;
}

复制代码

step2:编译链接源代码,形成目标代码,注意使用-g编译选项

gcc -g  -o tst_gdb tst_gdb.c

step3:使用gdb进行跟踪调试,如下图所示。

 

这里再给出一张图片作为说明。

 

发布了23 篇原创文章 · 获赞 2 · 访问量 1万+

Guess you like

Origin blog.csdn.net/wenson0705/article/details/104815475