[vim配置]windows下在vim中使用gcc/g++编译调试c/cpp文件

 

在Linux里面混了一个多月,vim编程用得甚爽。无奈前天将Linux里面的编程文件夹误删,而技术不精无法找回,悲痛欲绝。再者,无限怀念windows里面的游戏,并觉得现在在Linux里面也学不到什么东西,遂决定回到windows。

 

回到windows里面第一件事就是想把Linux里面的vim完美移植到windows。花了两天功夫,大部分功能已基本能够实现。当中遇到的最大困难是在windows里面实现用gcc/g++编译调试c/cpp文件,现在就把我这个过程完整记录下来。

 

1,首先下载安装MinGW,下载地址在 http://sourceforge.net/projects/mingw/。这个是边下载边安装的,下载完成即安装完成。我的安装目录是G:MinGW 

 

2,设置环境变量。右击我的电脑,点属性->高级->环境变量。然后: 

 

1、在PATH里加入G:MinGWin,记得,如果里面还有其他的变量,记得要加个分号啊,分号得在英文输入模式下输入的。

2、新建LIBRARY_PATH变量,如果有的话,在值中加入G:MinGWlib,这是标准库的位置。

3、新建C_INCLUDEDE_PATH变量,值设为G:MinGWinclude。

4、新建CPLUS_INCLUDE_PATH变量,值为G:MinGWinclude;G:MinGWincludec++4.5.0;G:MinGWincludec++4.5.0ackward;G:MinGWincludec++4.5.0mingw32

 

具体路径请根据你的MinGW选择。

 

3,在你的_vimrc文件中配置编译调试选项。我的个人配置如下:

 

"定义CompileRun函数,用来调用进行编译和运行

func CompileRun()

exec "w"

"C程序

if &filetype == 'c'

exec "!gcc -Wall -enable-auto-import % -g -o %<.exe"

"c++程序

elseif &filetype == 'cpp'

exec "!g++ -Wall -enable-auto-import  % -g -o %<.exe"

"Java程序

elseif &filetype == 'java'

exec "!javac %"

endif

endfunc

"结束定义CompileRun

"定义Run函数

func Run()

if &filetype == 'c' || &filetype == 'cpp'

exec "!%<.exe"

elseif &filetype == 'java'

exec "!java %<"

endif

endfunc

"定义Debug函数,用来调试程序

func Debug()

exec "w"

"C程序

if &filetype == 'c'

exec "!gcc % -g -o %<.exe"

exec "!gdb %<.exe"

elseif &filetype == 'cpp'

exec "!g++ % -g -o %<.exe"

exec "!gdb %<.exe"

"Java程序

elseif &filetype == 'java'

exec "!javac %"

exec "!jdb %<"

endif

endfunc

"结束定义Debug

"设置程序的运行和调试的快捷键F5和Ctrl-F5

map <F5> :call CompileRun()<CR>

map <F6> :call Run()<CR>

map <C-F5> :call Debug()<CR>

4,完成上面几步基本上就大功告成啦,尽情享受vim编程吧。

 

可能遇到的问题:

 

编译的时候可能会出现:

 

Info: resolving std::cout  by linking to __imp___ZSt4cout (auto-import)

 

c:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: a

 

uto-importing has been activated without --enable-auto-import specified on the c

 

ommand line.

 

This should work unless it involves constant data structures referencing symbols

 

from auto-imported DLLs.)

 

在编译命令中加入 -enable-auto-import 就行啦

 

源文档 <http://shoppon.diandian.com/post/2010-11-02/6889610>

原文地址:https://www.cnblogs.com/AI-Algorithms/p/3751428.html