More Exploration

TODO

  1. Read the whole text carefully.
  2. Try the commands of Linux mentioned.
  3. Configure vim as described.
  4. Write a "hello world" program and compile it, execute it, debug it, then organize it with make.
  5. Answer all the questions given in the quoting part.

Configuring Vim

Using vim to edit codes makes things simpler than you can imagine, and settings to vim in this section will help you fall in love with it.

Enabling Syntax Highlight

vim provides more improvements comparing with vi. But these improvements are disabled by default. Therefore, you should enable them first.

We take syntax highlight as an example to illustrate how to enable the features of vim. To do this, you should modify the vim configuration file. The file is called vimrc, and it is located under /etc/vimdirectory. We first make a copy of it to the home directory by cp command:

cp /etc/vim/vimrc ~/.vimrc

And switch to the home directory if you are not under it yet:

cd ~

If you use ls to list files, you will not see the .vimrc you just copied. This is because a file whose name starts with a . is a hidden file in GNU/Linux. To show hidden files, use ls with -a option:

ls -a

Then open .vimrc using vim:

vim .vimrc

After you learn some basic operations in vim (such as moving, inserting text, deleting text), you can try to modify the .vimrc file as following:

--- before modification
+++ after modification
@@ -?,3 +?,3 @@
 " Vim5 and later versions support syntax highlighting. Uncommenting the next
 " line enables syntax highlighting by default.
-"syntax on
+syntax on

We present the modification with GNU diff formatopen in new window. Lines starting with + are to be inserted. Lines starting with - are to be deleted. Other lines keep unchanged. If you do not understand the diff format, please search the Internet for more information.

After you are done, you should save your modification. Exit vim and open the vimrc file again, you should see the syntax highlight feature is enabled.

Enabling More Vim Features

Modify the .vimrc file mentioned above as the following:

--- before modification
+++ after modification
@@ -?,3 +?,3 @@
 " If using a dark background within the editing area and syntax highlighting
 " turn on this option as well
-"set background=dark
+set background=dark
@@ -?,5 +?,5 @@
 " Uncomment the following to have Vim load indentation rules and plugins
 " according to the detected filetype.
-"if has("autocmd")
-"  filetype plugin indent on
-"endif
+if has("autocmd")
+  filetype plugin indent on
+endif
@@ -?,10 +?,10 @@
 " The following are commented out as they cause vim to behave a lot
 " differently from regular Vi. They are highly recommended though.
 "set showcmd            " Show (partial) command in status line.
-"set showmatch          " Show matching brackets.
-"set ignorecase         " Do case insensitive matching
-"set smartcase          " Do smart case matching
-"set incsearch          " Incremental search
+set showmatch          " Show matching brackets.
+set ignorecase         " Do case insensitive matching
+set smartcase          " Do smart case matching
+set incsearch          " Incremental search
 "set autowrite          " Automatically save before commands like :next and :make
-"set hidden             " Hide buffers when they are abandoned
+set hidden             " Hide buffers when they are abandoned
 "set mouse=a            " Enable mouse usage (all modes)

You can append the following content at the end of the .vimrc file to enable more features. Note that contents after a double quotation mark " are comments, and you do not need to include them. Of course, you can inspect every features to determine to enable or not.

setlocal noswapfile " 不要生成swap文件
set bufhidden=hide " 当buffer被丢弃的时候隐藏它
colorscheme evening " 设定配色方案
set number " 显示行号
set cursorline " 突出显示当前行
set ruler " 打开状态栏标尺
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
set softtabstop=4 " 使得按退格键时可以一次删掉 4 个空格
set tabstop=4 " 设定 tab 长度为 4
set nobackup " 覆盖文件时不备份
set autochdir " 自动切换当前目录为当前文件所在的目录
set backupcopy=yes " 设置备份时的行为为覆盖
set hlsearch " 搜索时高亮显示被找到的文本
set noerrorbells " 关闭错误信息响铃
set novisualbell " 关闭使用可视响铃代替呼叫
set t_vb= " 置空错误铃声的终端代码
set matchtime=2 " 短暂跳转到匹配括号的时间
set magic " 设置魔术
set smartindent " 开启新行时使用智能自动缩进
set backspace=indent,eol,start " 不设定在插入状态无法用退格键和 Delete 键删除回车符
set cmdheight=1 " 设定命令行的行数为 1
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ Ln\ %l,\ Col\ %c/%L%) " 设置在状态行显示的信息
set foldenable " 开始折叠
set foldmethod=syntax " 设置语法折叠
set foldcolumn=0 " 设置折叠区域的宽度
setlocal foldlevel=1 " 设置折叠层数为 1
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR> " 用空格键来开关折叠

If you want to refer different or more settings for vim, please search the Internet. In addition, there are many plug-ins for vim (one of them you may prefer is ctags, which provides the ability to jump among symbol definitions in the code). They make vim more powerful. Also, please search the Internet for more information about vim plug-ins.

Writing a "Hello World" Program under GNU/Linux

We need you to try to write a "Hello World" program, using vim and compile it with GCC, execute it in command line, debug it with GDB and at last organize it with Make.

Writing Codes and Compiling it

Online documentation for GNU Compiler: https://gcc.gnu.org/onlinedocs/

The main steps are:

  1. Change working path to ~ with command cd ~.
  2. Create a empty file with command touch hello.c.
  3. Edit the code file by command vim hello.c.
  4. Type codes in the file and save it, then leave vim.
  5. Make sure hello.c is under current working path with command ls.
  6. Execute command gcc -o ./hello hello.c to compile it.
  7. Execute command ls again and you will find that a new file hello is generated at the current path.
  8. Execute the newly compiled file by command ./hello and you will see the program print a "hello world" on the screen.

{% panel style="success", title="Have a try!" %}

Write and run a "helloworld" program according to steps above.

{% endpanel %}

Debugging

Online documentation for GNU debugger GDB: http://sourceware.org/gdb/current/onlinedocs/gdb/

The main steps are:

  1. Change working path to where the program hello exists.
  2. Use command gdb ./hello to load hello in GDB.
  3. In GDB mode, you can see the prompt starting with (gdb).
  4. Type br main to make a breakpoint at the entry of function main.
  5. Type r or run to execute the loaded program (hello).
  6. You will see the GDB stopped the program at the entry of main.
  7. Type disas to see the disassembly in the current function.
  8. Type c or continue to continue running the program.
  9. When GDB tells you that the program exited, type q or quit to exit.

{% panel style="success", title="Have a try!!" %}

Debug the "helloworld" program according to steps above.

{% endpanel %}

Organizing Project with Make

Make is a widely used tool for project management, a huge proportion of projects are managed with it and so is PA. You can follow the documentationsopen in new window provided by GNU to learn how to compile hello.c with Make. You just need to read the whole part of Chapter 1 and 2, that's enough. We also provide you with a manual of Make in our Linux Manual.

Make a new directory helloproject and move the code file you wrote into it, then edit the Makefile by

cd ~
mkdir helloproject/
mv hello.c helloproject/
cd helloproject/
touch Makefile
vim Makefile

{% panel style="success", title="Have a try!!!" %}

Use Make to organize this "helloproject" and the Makefile you wrote should consist of functions below:

  1. Able to compile hello.c by command

    make
    

    directly, the generated program should be named ./hello.

  2. Able to clean the project which means deleting ./hello by

    make clean
    
  3. Able to run the project (./hello) by

    make run
    

{% endpanel %}

{% panel style="info", title="RTFM" %}

The most important command in GNU/Linux is man - the on-line manual pager. This is because mancan tell you how to use other commands. Here is a small tutorial for man. Remember, learn to use man, learn to use everything. Therefore, if you want to know something about GNU/Linux (such as shell commands, system calls, library functions, device files, configuration files...), RTFMopen in new window.

{% endpanel %}

Note that if you encounter with any problem, please use man first, if that can't solve your problem, search the Internet.

Checking Time of VM

Time in VMs usually goes faster or slower. To make it able to record Git logs correctly, we need to ensure the time of your VM is the same of the real time of Beijing (GMT +8). You are supposed to sync time regularly. Command below will tell you the current time of your VM.

date

Syncing with NTP Servers

Install ntpdate firstly by

sudo apt install ntpdate

and sync with command

sudo ntpdate ntp.ntsc.ac.cn

to sync time with NTP server of NTSCopen in new window.

Execute command

sudo date -s "YYYY-MM-DD HH:mm:SS"

For example

sudo date -s "2018-01-01 11:20:30"

but this way is not recommended for the latency.


That's everything in PA0.5.

Last Updated 2023-04-02 14:55:00