构建VIM下的C++编程环境

1、VIM配置

  Vim强大的配置与功能,其来源基本上就两个地方:Vim插件以及Vim配置文件
  Vim本身的系统配置文件夹是在/usr/share/vim/和/etc/vim/两个文件夹下。一般情况下,我们不需要改变这两个文件夹下的配置文件,只需在自身用户文件夹/home/username(其中,username为用户名,我的用户名是xiaoku)下建立自己的配置文件(.vimrc)。

  一个具有语法高亮的.vimrc配置文件参考内容如下:

 1 " This line should not be removed as it ensures that various options are
 2 " properly set to work with the Vim-related packages available in Debian.
 3 " debian.vim
 4 
 5 " Uncomment the next line to make Vim more Vi-compatible
 6 " NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
 7 " options, so any other options should be set AFTER setting 'compatible'.
 8 set nocompatible
 9 
10 " Vim5 and later versions support syntax highlighting. Uncommenting the
11 " following enables syntax highlighting by default.
12 if has("syntax")
13     syntax on            " 语法高亮
14 endif
15 colorscheme ron        " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下
16 
17 " detect file type
18 filetype on
19 filetype plugin on
20 
21 " If using a dark background within the editing area and syntax
22 "    highlighting
23 " turn on this option as well
24 set background=dark
25 
26 " Uncomment the following to have Vim jump to the last position when
27 " reopening a file
28 if has("autocmd")
29     au BufReadPost * if line("'"") > 1 && line("'"") <= line("$") | exe "normal! g'"" | endif
30     "have Vim load indentation rules and plugins according to the detected
31     filetype
32     filetype plugin indent on
33 endif
34 
35 " The following are commented out as they cause vim to behave a lot
36 " differently from regular Vi. They are highly recommended though.
37 
38 "set ignorecase        " 搜索模式里忽略大小写
39 "set smartcase        " 如果搜索模式包含大写字符,不使用 'ignorecase'
40 "    选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
41 set autowrite        " 自动把内容写回文件: 如果文件被修改过,在每个
42 "    :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-]
43 "    和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9}
44 "    命令转到别的文件时亦然。
45 set autoindent        "设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent
46 "    取消设置
47 "set smartindent        " 智能对齐方式
48 set tabstop=4        " 设置制表符(tab键)的宽度
49 set softtabstop=4     " 设置软制表符的宽度    
50 set shiftwidth=4    " (自动) 缩进使用的4个空格
51 set cindent            " 使用 C/C++ 语言的自动缩进方式
52 set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
53 "设置C/C++语言的具体缩进方式
54 "set backspace=2    " 设置退格键可用
55 set showmatch        " 设置匹配模式,显示匹配的括号
56 set linebreak        " 整词换行
57 set whichwrap=b,s,<,>,[,] " 光标从行首和行末时可以跳到另一行去
58 "set hidden " Hide buffers when they are abandoned
59 set mouse=a            " Enable mouse usage (all modes)    "使用鼠标
60 set number            " Enable line number    "显示行号
61 "set previewwindow    " 标识预览窗口
62 set history=50        " set command history to 50    "历史记录50条
63 
64 "
65 "    "--状态行设置--
66 set laststatus=2 "
67 "    总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行
68 set ruler            "
69 "    标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。
70 "
71 "    "--命令行设置--
72 set showcmd            " 命令行显示输入的命令
73 set showmode        " 命令行显示vim当前模式
74 "
75 "    "--find setting--
76 set incsearch        " 输入字符串就显示匹配点
77 set hlsearch      
View Code

2、构建源代码索引

  利用ctags可以建立源码树的标签索引(标签就是一个标识符被定义的地方,如函数定义),使程序员在编程时能迅速定位函数、变量、宏定义等位置去查看原形。

  举个例子来说,可以构建Linux内核源代码的索引树,然后就能够在编程的过程中查看系统函数的原型与定义等内容,下面详细说一下过程:

2.1 下载Linux内核源代码

  详细过程请参考我的上一篇博文《在Fedora 20环境下安装系统内核源代码》。

2.2 安装ctags

  利用yum在线安装ctags:

[xiaoku@localhost linux-3.16.2-200.fc20.x86_64]$ sudo yum install ctags

 2.3 建立内核源代码的索引

  建立之前,先进入内核源代码所在的工程目录:

[xiaoku@localhost linux-3.16.2-200.fc20.x86_64]$ pwd
/home/xiaoku/rpmbuild/BUILD/kernel-3.16.fc20/linux-3.16.2-200.fc20.x86_64
[xiaoku@localhost linux-3.16.2-200.fc20.x86_64]$ ctags -R *

   完成之后,会发现在此工程目录中,新增了一个tags文件夹,该目录中就是代码索引内容。

2.4 给VIM中添加TAGS索引

   编辑用户对应的VIM配置文件,在.vimrc文件结尾添加配置信息内容,如下:

[xiaoku@localhost linux-3.16.2-200.fc20.x86_64]$ pwd
/home/xiaoku/rpmbuild/BUILD/kernel-3.16.fc20/linux-3.16.2-200.fc20.x86_64
[xiaoku@localhost linux-3.16.2-200.fc20.x86_64]$ vi ~/.vimrc

   添加的内容如下所示:

"--ctags setting--
"" 按下F5重新生成tag文件,并更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=/home/xiaoku/rpmbuild/BUILD/kernel-3.16.fc20/linux-3.16.2-200.fc20.x86_64 "add new tags file(刚刚生成tags的路径,在ctags -R 生成tags文件后,不要将tags移动到别的目录,否则ctrl+]时,会提示找不到源码文件)

   其中,set tags+=./tags 表示在当前工作目录下搜索tags文件,set tags+=/home/xiaoku/rpmbuild/BUILD/kernel-3.16.fc20/linux-3.16.2-200.fc20.x86_64 表示在搜寻tags文件的时候,也要搜寻指定目标文件夹下的tags文件。然后保存并退出vi。这样,你就可以用vim在任意地方查看有关Linux的函数原形。

3、TAGS用法简介

3.1 tag命令常用用法

Ctrl+]  跳到当前光标下单词的标签
Ctrl+O  返回上一个标签
Ctrl+T  返回上一个标签
:tag TagName 跳到TagName标签
以上命令是在当前窗口显示标签,当前窗口的文件替代为包标签的文件,当前窗口光标跳到标签位置。如果不希望在当前窗口显示标签,可以使用以下命令:
:stag TagName 新窗口显示TagName标签,光标跳到标签处
Ctrl+W + ]  新窗口显示当前光标下单词的标签,光标跳到标签处
当一个标签有多个匹配项时(函数 (或类中的方法) 被多次定义),":tags" 命令会跳转到第一处。如果在当前文件中存在匹配,那它将会被首先使用。
可以用这些命令在各匹配的标签间移动:
:tfirst    到第一个匹配
:[count]tprevious 向前 [count] 个匹配
:[count]tnext  向后 [count] 个匹配
:tlast    到最后一个匹配
或者使用以下命令选择要跳转到哪一个
:tselect TagName
输入以上命令后,vim会为你展示一个选择列表。然后你可以输入要跳转到的匹配代号 (在第一列)。其它列的信息可以让你知道标签在何处被定义过。
以下命令将在预览窗口显示标签
:ptag TagName 预览窗口显示TagName标签,光标跳到标签处
Ctrl+W + }  预览窗口显示当前光标下单词的标签,光标跳到标签处
:pclose   关闭预览窗口
:pedit file.h 在预览窗口中编辑文件file.h(在编辑头文件时很有用)
:psearch atoi 查找当前文件和任何包含文件中的单词并在预览窗口中显示匹配,在使用没有标签文件的库函数时十分有用。 

3.2 简单应用举例

  建立一个最简单的代码示例:

int main()
{
    printf("Hello world!
");
    return 0;      
}

  输入完成后,退出到命令行状态输入:w,但不要退出VI,将光标移动到printf函数位置,按下组合件Ctrl + W + ],vi就能够自动跳转到Linux系统中的printf函数原型位置,再按下Ctrl + o返回到原来位置。

4、C++编译器

  Linux环境下默认是支持C语言编译的,即默认带了gcc编译器。不过,如果我们需要编译c++代码,在Fedora环境下需要安装g++编译器,安装的命令如下:

[root@localhost workspace]# yum install gcc-c++

  我记得以前安装g++的命令就是直接yum install g++啊,不知道Fedora环境下为什么改了名字。在编译C++文件的时候,要注意使用g++而不是gcc,否则会出现错误:

[root@localhost workspace]# gcc main.cpp -o out
/tmp/ccCafWzx.o:在函数‘main’中:
main.cpp:(.text+0xa):对‘std::cout’未定义的引用
main.cpp:(.text+0xf):对‘std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)’未定义的引用
main.cpp:(.text+0x14):对‘std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)’未定义的引用
main.cpp:(.text+0x1c):对‘std::ostream::operator<<(std::ostream& (*)(std::ostream&))’未定义的引用
/tmp/ccCafWzx.o:在函数‘__static_initialization_and_destruction_0(int, int)’中:
main.cpp:(.text+0x4a):对‘std::ios_base::Init::Init()’未定义的引用
main.cpp:(.text+0x59):对‘std::ios_base::Init::~Init()’未定义的引用
collect2: 错误:ld 返回 1
原文地址:https://www.cnblogs.com/kuliuheng/p/3975008.html