1. 程式人生 > >windows(8) 下在GVIM中使用gcc/g++編譯除錯c/cpp檔案

windows(8) 下在GVIM中使用gcc/g++編譯除錯c/cpp檔案

1. 首先下載安裝MinGW,下載地址: http://sourceforge.net/projects/mingw/。這個是邊下載邊安裝的,下載完成即安裝完成。我的安裝目錄為D:\MinGW;

2. 設定系統環境變數。右擊Computer -> Properties-> Advanced system settings -> Advanced -> Environment Variables。然後:

2.1 在PATH里加入D:\MinGW\bin ;

2.2 新建LIBRARY_PATH變數,如果有的話,直接加入D:\MinGW\lib ;

2.3 新建C_INCLUDEDE_PATH變數,值設為 D:\MinGW\include ;

2.4 新建CPLUS_INCLUDE_PATH變數,值為 D:\MinGW\include ;

3. 在_vimrc檔案的末尾增加編譯除錯選項,新增的程式碼如下:

"定義CompileRun函式,用來呼叫編譯和執行
func CompileRun()
exec "w"

if &filetype == 'c'
exec "!gcc -Wall -enable-auto-import % -g -o %<.exe"

elseif &filetype == 'cpp'
exec "!g++ -Wall -enable-auto-import % -g -o %<.exe"

elseif &filetype == 'java'
exec "!javac %"
endif
endfunc
"結束定義ComplieRun

"定義Run函式
func Run()
if &filetype == 'c' || &filetype == 'cpp'
exec "!%<.exe"
elseif &filetype == 'java'
exec "!java %<"
endif
endfunc

"定義Debug函式,用來除錯程式
func Debug()
exec "w"

if &filetype == 'c'
exec "!gcc % -g -o %<.exe"
exec "!gdb %<.exe"
elseif &filetype == 'cpp'
exec "!g++ % -g -o %<.exe"
exec "!gdb %<.exe"
elseif &filetype == 'java'
exec "!javac %"
exec "!jdb %<"
endif
endfunc

”設定程式的執行和除錯的快捷鍵F5和Ctrl-F5
map <F5> :call CompileRun()<CR>
map <F6> :call Run()<CR>
map <C-F5> :call Debug()<CR>
4. 完成上面幾步基本上就大功告成了。

可能遇到的問題:

編譯的時候可能會出現:

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

備註:以上大部分參考自網路,但是有點需要注意:


這種情況,把CompileRun()裡面加紅的 -Wall -enable-auto-import 去掉就好了