工欲善其事,必先利其器。一个强大的开发环境可以大大提高工作效率。估计大家经常用的IDE工具如SourceInsight,Notepad++,VScode等等。SourceInsight属于收费软件,其实费用并不贵。Notepad++和VScode均属于开源免费软件。其实IDE工具使用完全看个人习惯与爱好。本人SOC和MCU都开发过,基本基于ARM架构。所以平常开发工具和环境基本实在Ubuntu/Linux环境下。使用的IDE环境自然是Vim. 个人习惯基本都是Linux的开发习惯。但是日常工作办公使用windows的Laptop居多。那如何可以在windows下搭建跟Linux下一样的开发IDE环境呢?
其实在windows下类Unix的工具有三个。
- Git-2.33.0.2-64-bit.exe 最新的版本你可以到Git官网下载。因为简化的MINGW64。
- Cygwin 是运行于Windows平台的POSIX”子系统“,提供Windows下的类Unix环境,并提供将部分Linux应用”移植“到Windows平台的开发环境的一套软件。
- msys2-installer MSYS是用于辅助Windows版MinGW进行命令行开发的配套软件包,提供了部分Unix工具以使得MinGW的工具使用起来方便一些。整套MSYS以及MinGW,主要一基本Linux工具为主,大小在200M左右,并没有多少扩展能力。
总概括MinGW和Cygwin的主要区别:MinGW设计思想是“最小化支持”,Cygwin是最大可能的模拟一个Linux环境。
下面介绍三种工具搭建vim+ctags+cscope的方法。基本可以理解Cygwin和MinGW的自动安装版,Git-bash类似于绿色安装版。
首先根据安装教程Windows系统Git安装教程(详解Git安装过程),安装好Git。
Git-bash是一个简易版的mingw64环境,shell为bash,基本就和Linux一样了,但它没有携带gcc,,make,cmake,ctags 和 cscope等程序。
从http://www.equation.com/servlet/equation.cmd?fa=fortra 下载gcc套件,gcc套件内包含make,所以一并就装了这俩。
解压gcc套件,比如默认按照在了个人账户目录内为 C:\Users\Westo\gcc ,那好,打开这个文件
C:\Program Files\Git\etc\profile.d\env.sh
添加一行:
export PATH="$HOME/gcc/bin:$PATH"
再次打开git-bash,gcc套件内工具就能使用了,make自然包括。
如果用不到gcc,只想使用下make。那从 http://www.equation.com/servlet/equation.cmd?fa=make 下载make程序即可,同样,将make程序直接放到 C:\Program Files\Git\mingw64\bin 内就可以了。
从 https://cmake.org/download/ 下载相应的安装包,仿前,安装后打开这个文件
C:\Program Files\Git\etc\profile.d\env.sh
再添加一行:
export PATH="/c/Program Files/CMake/bin:$PATH"
从https://code.google.com/archive/p/cscope-win32/downloads 下载相应的压缩包。同样,将cscope.exe程序直接放到 C:\Program Files\Git\mingw64\bin 内就可以了。
从http://ctags.sourceforge.net/ 下载ctags58.zip之后。同样,将ctags.exe程序直接放到 C:\Program Files\Git\mingw64\bin 内就可以了。
Cygwin的安装很简单,但是选择正确的软件包可能很棘手。 我们建议您按照Cygwin主页上的说明进行操作,这是我使用的步骤的摘要: 1.下载并运行/安装 setup-x86_64.exe (64-bit installation)。
2.选择“从Internet安装”作为下载源。
我这里选择在D盘根目录下。
下面是你下载的软件包存放在哪里,我这里是把我的安装程序和软件包放在同一个文件夹里
选择下载代理。
3.安装下面列出的软件包。 Cygwin Devel软件包要安装:
- autobuild
- autoconf
- autoconf-archive
- automake
- dos2unix
- git
- gcc-core
- gcc-g++
- wget
- vim
- ctags
- cscope
- make
- cmake
- pkg-config
- usbutils
- libtool
- libusb1.0
- libusb1.0-devel
查看已安装的软件包:
简单举例git软件包截图:
其他的软件包操作类型。
MinGWW64的安装同样很简单,直接安装msys2-x86_64-latest.exe及其工具组件
a.双击msys2-x86_64-latest.exe,并点击下一步
选择安装文件夹在对应的toolchain目录下
然后下一步
直到开始安装
b.安装完成后点击完 成
退出安装程序
c.进入到“MSYS2 MSYS”命令界面
打开后执行命令pacman -Syu
更新系统核心和各种包等,如下面的截图
输入“Y”键开始更新 注意: 更新时会遇到下图的警告,这时一定要点击右上角的×来关闭窗口
关闭窗口时会弹出一个对话框(如下图),点击Y就可以了。
上面的更新没有结束,需要继续更新, 再执行一次pacman -Syu
继续输入“Y”键开始更新。再执行一次pacman -Syu
这样更新就可以顺利结束了, msys2的安装也顺利结束了。
使用下面命令安装所需组件pacman -S mingw-w64-x86_64-gcc make cmake git vim ctags cscope wget pkg-config openssh dos2unix
。
# pacman -S mingw-w64-x86_64-gcc make cmake git vim ctags cscope wget pkg-config openssh dos2unix
warning: wget-1.21.2-1 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...
Packages (59) expat-2.4.3-1 heimdal-7.7.0-3 jsoncpp-1.9.5-1 libarchive-3.5.2-1 librhash-1.4.2-1 libuv-1.42.0-1 mingw-w64-x86_64-binutils-2.37-4 mingw-w64-x86_64-crt-git-9.0.0.6373.5be8fcd83-1 mingw-w64-x86_64-gcc-libs-11.2.0-6 mingw-w64-x86_64-gmp-6.2.1-3
mingw-w64-x86_64-headers-git-9.0.0.6373.5be8fcd83-1 mingw-w64-x86_64-isl-0.24-1 mingw-w64-x86_64-libiconv-1.16-2 mingw-w64-x86_64-libwinpthread-git-9.0.0.6373.5be8fcd83-1 mingw-w64-x86_64-mpc-1.2.1-1 mingw-w64-x86_64-mpfr-4.1.0.p13-1
mingw-w64-x86_64-windows-default-manifest-6.4-3 mingw-w64-x86_64-winpthreads-git-9.0.0.6373.5be8fcd83-1 mingw-w64-x86_64-zlib-1.2.11-9 mingw-w64-x86_64-zstd-1.5.1-1 perl-Authen-SASL-2.16-2 perl-Clone-0.45-2 perl-Convert-BinHex-1.125-1
perl-Encode-Locale-1.05-1 perl-Error-0.17029-1 perl-File-Listing-6.14-1 perl-HTML-Parser-3.76-1 perl-HTML-Tagset-3.20-2 perl-HTTP-Cookies-6.10-1 perl-HTTP-Daemon-6.12-1 perl-HTTP-Date-6.05-1 perl-HTTP-Message-6.35-1 perl-HTTP-Negotiate-6.01-2
perl-IO-HTML-1.004-1 perl-IO-Socket-SSL-2.072-1 perl-IO-Stringy-2.113-1 perl-LWP-MediaTypes-6.04-1 perl-MIME-tools-5.509-1 perl-MailTools-2.21-1 perl-Net-HTTP-6.21-1 perl-Net-SMTP-SSL-1.04-1 perl-Net-SSLeay-1.90-1 perl-TermReadKey-2.38-2
perl-TimeDate-2.33-1 perl-Try-Tiny-0.31-1 perl-URI-5.10-1 perl-WWW-RobotRules-6.02-2 perl-libwww-6.57-1 cmake-3.22.1-2 cscope-15.9-1 ctags-5.8-2 dos2unix-7.4.2-1 git-2.34.1-1 make-4.3-3 mingw-w64-x86_64-gcc-11.2.0-6 openssh-8.8p1-1
pkg-config-0.29.2-4 vim-8.2.3582-1 wget-1.21.2-1
Total Download Size: 72.53 MiB
Total Installed Size: 532.69 MiB
Net Upgrade Size: 529.64 MiB
:: Proceed with installation? [Y/n] Y
其实两个cscope_maps.vim和taglist.vim插件完全满足我们日常编写和查阅代码使用。
下面是我常用的.vimrc文件配置。
set wildmenu
set number
set nocompatible
set autoindent
set history=50
set ruler
set showcmd
set incsearch
set ai
set showmatch
set showmode
set hlsearch
set t_Co=8
set cino=:0
set cindent
colo ron
syntax on
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6: Stick this file in your ~/.vim/plugin directory (or in a
" 'plugin' directory in some other directory that is in your
" 'runtimepath'.
"
" -- vim 5: Stick this file somewhere and 'source cscope.vim' it from
" your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys). If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell [email protected] 2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled. If it wasn't, time to recompile vim...
""""""""""""" Standard cscope/vim boilerplate
" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
" set cscopetag
" check cscope for definition of a symbol before checking ctags: set to 1
" if you want the reverse search order.
set csto=0
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
"else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
" show msg when any other cscope db added
set cscopeverbose
""""""""""""" My cscope/vim key mappings
"
" The following maps all invoke one of the following cscope search types:
"
" 's' symbol: find all references to the token under cursor
" 'g' global: find global definition(s) of the token under cursor
" 'c' calls: find all calls to the function name under cursor
" 't' text: find all instances of the text under cursor
" 'e' egrep: egrep search for the word under cursor
" 'f' file: open the filename under cursor
" 'i' includes: find files that include the filename under cursor
" 'd' called: find functions that function under cursor calls
"
" Below are three sets of the maps: one set that just jumps to your
" search result, one that splits the existing vim window horizontally and
" diplays your search result in the new window, and one that does the same
" thing, but does a vertical split instead (vim 6 only).
"
" I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
" unlikely that you need their default mappings (CTRL-\'s default use is
" as part of CTRL-\ CTRL-N typemap, which basically just does the same
" thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
" If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
" of these maps to use other keys. One likely candidate is 'CTRL-_'
" (which also maps to CTRL-/, which is easier to type). By default it is
" used to switch between Hebrew and English keyboard mode.
"
" All of the maps involving the macro use '^$': this is so
" that searches over '#include " return only references to
" 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
" files that contain 'time.h' as part of their name).
" To do the first type of search, hit 'CTRL-\', followed by one of the
" cscope search types above (s,g,c,t,e,f,i,d). The result of your cscope
" search will be displayed in the current window. You can use CTRL-T to
" go back to where you were before the search.
"
nmap s :cs find s =expand("")
nmap g :cs find g =expand("")
nmap c :cs find c =expand("")
nmap t :cs find t =expand("")
nmap e :cs find e =expand("")
nmap f :cs find f =expand("")
nmap i :cs find i ^=expand("")$
nmap d :cs find d =expand("")
" Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
" makes the vim window split horizontally, with search result displayed in
" the new window.
"
" (Note: earlier versions of vim may not have the :scs command, but it
" can be simulated roughly via:
" nmap s :cs find s =expand("")
nmap s :scs find s =expand("")
nmap g :scs find g =expand("")
nmap c :scs find c =expand("")
nmap t :scs find t =expand("")
nmap e :scs find e =expand("")
nmap f :scs find f =expand("")
nmap i :scs find i ^=expand("")$
nmap d :scs find d =expand("")
" Hitting CTRL-space *twice* before the search type does a vertical
" split instead of a horizontal one (vim 6 and up only)
"
" (Note: you may wish to put a 'set splitright' in your .vimrc
" if you prefer the new window on the right instead of the left
nmap s :vert scs find s =expand("")
nmap g :vert scs find g =expand("")
nmap c :vert scs find c =expand("")
nmap t :vert scs find t =expand("")
nmap e :vert scs find e =expand("")
nmap f :vert scs find f =expand("")
nmap i :vert scs find i ^=expand("")$
nmap d :vert scs find d =expand("")
map :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
vim插件大家可以通过访问我的公众号“背包旅行码农”,输入关键字“vim ctags cscope”获取。把.vim目录和.vimrc文件解压到用户目录下。
git-bash对应目录在”/c/Users/用户名”。
Cygwin64对应目录在"/盘符/cygwin64/home/用户名"。
MinGWW64对应目录在"/盘符/msys64/home/用户名"。
Vim 键盘图:
====命令模式====
==字符间==
[NUM] h 向左NUM个字符
[NUM] l 向左NUM个字符
[NUM] j 向左NUM个字符
[NUM] k 向左NUM个字符
==单词间==
[NUM] w 下NUM个单词
[NUM] e 当前或下NUM个单词词尾
[NUM] b 当前或上NUM个单词词首
==页间==
H 页首
M 页中
L 页底`
zt 当前行移到顶
zz 当前行移到中
zb 当前行移到底
==行间、句间、段落间==
^ 行首(非空)
0 行首
$ 行尾
NUM G 跳到NUM行
G 尾行
gg 首行
( 下一句
) 下一句
{ 上一段
} 下一段
==翻屏快捷键==
Ctrl+f 向下一屏
Ctrl+b 向上一屏
Ctrl+d 向下半屏
Ctrl+u 向上半屏
==字符操作==
[NUM] x 删除当前NUM个字符
p 粘贴x删除的字符
~ 大小写转换
gu 变小写
gU 变大写
J 删除当前行的换行符
r 替换当前字符
R 替换模式
==删除命令==
[NUM] d 删除命令(结合下面使用),
d$ 或 D 删到尾
d0 删到头
d^ 删到头(非空)
dw 删除当前字符
de 后删单词
db 前删单词
[NUM] dd 删除NUM行
==删除命令==
[NUM] y 复制命令(结合下面使用),
y$ 复制到尾
y0 复制到头
y^ 复制到头(非空)
yw 复制当前字符
ye 复制后单词
yb 复制前单词
[NUM] yy 复制NUM行
==粘贴命令==
p 往后粘贴
P 往前粘贴
==改变命令==
c 修改后进入插入模式(下同)
c$ 或 C 删到尾
c0 删到头
c^ 删到头(非空)
cw 删除当前字符
ce 后删单词
cb 前删单词
[NUM] cc 删除NUM行
di" 删除" " 之间内容
yi( 复制( ) 之间内容
vi[ 选中[ ] 之间内容
dtCHAR 删除之后直到CHAR字符
ytCHAR 复制之后直到CHAR字符
==查找==
/PATTERN 向下查找
?PATTERN 向上查找
n 查找方向(对查找结果)
N 查找反向(对查找结果)
==撤消更改==
[NUM] u 撤消NUM次
U 撤消行的所有更改
Ctrl + r 重做
[NUM] . 重复前一个/NUM个操作
====扩展命令模式====
==关闭文件==
:q 退出
:q! 强制退出
:wq 保存退出
:x 保存退出
ZZ 保存退出
ZQ 不保存退出
==地址定界+COMMAND==
==地址定界==
:start_pos,end_pos 地址定界
:NUM 跳转到NUM行
:NUM,+NUM 如:2,+3表示2,5行
:. 当前行
:$ 最后行
:% 全文
/STR1/,/STR2/ 从匹配到的STR1到STR2
NUM,/STR/ 从第NUM行到STR匹配行
/STR/,$ 从STR匹配行到结束
==COMMAND==
d 删除
w FILE 选中范围保存到文件FILE
r FILE 选中范围保存到指定FILE
==查找并替换==
格式: s/要查的内容/要替换的内容/修饰符
I 忽略大小写
g 每行替换1次
gc 全部替换
s/string1/string2/g
s@string1@string2@gc
s#string1#string2#i
使用ctags的也很简单。 进入我们的项目代码根目录,执行以下命令:
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .
另外,由于在前面第5条,我们已经在Vim中配置了Ctrl-F12组合快捷键,所以我们也可以进入代码根目录后,打开Vim,按下Ctrl-F12快捷键自动生成tags文件。
命令执行完后,会在源代码目录生成tags文件。Vim默认会自动读取当前目录下的tags文件,所以不需要修改~/.vimrc文件。此时,我们已经具有定义跳转的功能了。有两组快捷键是最常用的。
Ctrl+] 跳转到光标所在符号的定义。
Ctrl+o 返回上一个标签
Ctrl+t 回到上次跳转前的位置。
:tag TagName 跳到TagName标签
:stag TagName 新窗口显示TagName标签,光标跳到标签处
Ctrl+W + ] 新窗口显示当前光标下单词的标签,光标跳到标签处
:tfirst 到第一个匹配
:[count]tprevious 向前 [count] 个匹配
:[count]tnext 向后 [count] 个匹配
:tlast 到最后一个匹配
:tselect TagName 展示TagName标签选择列表
:ptag TagName 预览窗口显示TagName标签,光标跳到标签处
Ctrl+W + } 预览窗口显示当前光标下单词的标签,光标跳到标签处
:pclose 关闭预览窗口
更多功能通过命令man ctags或在Vim命令行下运行help ctags查询。
生成索引cscope.out
先找出你关心的头文件和.c文件,我是在linux的根目录下执行find来找出所有的.h和.c文件。Linux中使用是GNU find命令,默认是递归查询的,所以找文件很简单。不像solaris中的find命令需要使用一些trick来实现递归查找。
$ find . -name "*.[h|c]" > cscope.files
然后,生成索引文件
$ cscope -bkq -i cscope.files
等命令结束后,你会发现根目录下生成了一个名为cscope.out的文件。此时,你就可以独立使用cscope来浏览代码了,执行Ctrl-d可以退出cscope。
:cs find s ---- 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
:cs find g ---- 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能
:cs find d ---- 查找本函数调用的函数
:cs find c ---- 查找调用本函数的函数
:cs find t ---- 查找指定的字符串
:cs find e ---- 查找egrep模式,相当于egrep功能,但查找速度快多了
:cs find f ---- 查找并打开文件,类似vim的find功能
:cs find i ---- 查找包含本文件的文
大家可以直接加我的公众号“背包旅行码农”,输入关键字“vim ctags cscope”获取。
将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete
MinGw 和 cygwin 的区别和联系
Cygwin 和MinGW 的区别与联系是怎样的
MinGW and Cygwin - An Introduction and Differences
给git-bash加点儿料——gcc/make/cmake
Linux入门_Vim文本编辑器详解
VIM-Editor-Mind-Map/VIM用法