分享

gVim/Vim 一键编译、连接、运行 C/C++ 单文件(Windows 与 Linux通用)

 Rainboy913 2013-11-21
用于Gvim 或 Vim 配置文件的一键编译与运行函数(注:需要机器上安装了GCC才行)
本代码只加入了对C/C++的编译与运行,如果要加入其语言的可以参考此代码加入即可
同时,本代码加入了对Windows下用Gvim编译UTF-8编码格式的源文件时中文乱码的解
决方法(也就是在编译选项中加入了 “-fexec-charset=gbk”)。
将上面的代码加入到_vimrc文件里即可
标签: GVim Vim

代码片段(2) [全屏查看所有代码]

1. [代码]gvim配置文件     跳至 [1] [全屏预览]

001"------------------------------------------------------------------------------
002"  < 判断操作系统是否是 Windows 还是 Linux >
003"------------------------------------------------------------------------------
004if(has("win32") || has("win64") || has("win95") || has("win16"))
005    let g:iswindows = 1
006else
007    let g:iswindows = 0
008endif
009 
010"------------------------------------------------------------------------------
011"  < 判断是终端还是 Gvim >
012"------------------------------------------------------------------------------
013if has("gui_running")
014    let g:isGUI = 1
015else
016    let g:isGUI = 0
017endif
018 
019"------------------------------------------------------------------------------
020"  < 编译、连接、运行配置 >
021"------------------------------------------------------------------------------
022" F9 一键保存、编译、连接存并运行
023map <F9> :call Run()<CR>
024imap <F9> <ESC>:call Run()<CR>
025 
026" Ctrl + F9 一键保存并编译
027map <c-F9> :call Compile()<CR>
028imap <c-F9> <ESC>:call Compile()<CR>
029 
030" Ctrl + F10 一键保存并连接
031map <c-F10> :call Link()<CR>
032imap <c-F10> <ESC>:call Link()<CR>
033 
034let s:LastShellReturn_C = 0
035let s:LastShellReturn_L = 0
036let s:ShowWarning = 1
037let s:Obj_Extension = '.o'
038let s:Exe_Extension = '.exe'
039let s:Sou_Error = 0
040 
041let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
042let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
043 
044let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
045let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o'
046 
047func! Compile()
048    exe ":ccl"
049    exe ":update"
050    if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx"
051        let s:Sou_Error = 0
052        let s:LastShellReturn_C = 0
053        let Sou = expand("%:p")
054        let Obj = expand("%:p:r").s:Obj_Extension
055        let Obj_Name = expand("%:p:t:r").s:Obj_Extension
056        let v:statusmsg = ''
057        if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou)))
058            redraw!
059            if expand("%:e") == "c"
060                if g:iswindows
061                    exe ":setlocal makeprg=".s:windows_CFlags
062                else
063                    exe ":setlocal makeprg=".s:linux_CFlags
064                endif
065                echohl WarningMsg | echo " compiling..."
066                silent make
067            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
068                if g:iswindows
069                    exe ":setlocal makeprg=".s:windows_CPPFlags
070                else
071                    exe ":setlocal makeprg=".s:linux_CPPFlags
072                endif
073                echohl WarningMsg | echo " compiling..."
074                silent make
075            endif
076            redraw!
077            if v:shell_error != 0
078                let s:LastShellReturn_C = v:shell_error
079            endif
080            if g:iswindows
081                if s:LastShellReturn_C != 0
082                    exe ":bo cope"
083                    echohl WarningMsg | echo " compilation failed"
084                else
085                    if s:ShowWarning
086                        exe ":bo cw"
087                    endif
088                    echohl WarningMsg | echo " compilation successful"
089                endif
090            else
091                if empty(v:statusmsg)
092                    echohl WarningMsg | echo " compilation successful"
093                else
094                    exe ":bo cope"
095                endif
096            endif
097        else
098            echohl WarningMsg | echo ""Obj_Name"is up to date"
099        endif
100    else
101        let s:Sou_Error = 1
102        echohl WarningMsg | echo " please choose the correct source file"
103    endif
104    exe ":setlocal makeprg=make"
105endfunc
106 
107func! Link()
108    call Compile()
109    if s:Sou_Error || s:LastShellReturn_C != 0
110        return
111    endif
112    let s:LastShellReturn_L = 0
113    let Sou = expand("%:p")
114    let Obj = expand("%:p:r").s:Obj_Extension
115    if g:iswindows
116        let Exe = expand("%:p:r").s:Exe_Extension
117        let Exe_Name = expand("%:p:t:r").s:Exe_Extension
118    else
119        let Exe = expand("%:p:r")
120        let Exe_Name = expand("%:p:t:r")
121    endif
122    let v:statusmsg = ''
123    if filereadable(Obj) && (getftime(Obj) >= getftime(Sou))
124        redraw!
125        if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj))
126            if expand("%:e") == "c"
127                setlocal makeprg=gcc\ -o\ %<\ %<.o
128                echohl WarningMsg | echo " linking..."
129                silent make
130            elseif expand("%:e") == "cpp" || expand("%:e") == "cxx"
131                setlocal makeprg=g++\ -o\ %<\ %<.o
132                echohl WarningMsg | echo " linking..."
133                silent make
134            endif
135            redraw!
136            if v:shell_error != 0
137                let s:LastShellReturn_L = v:shell_error
138            endif
139            if g:iswindows
140                if s:LastShellReturn_L != 0
141                    exe ":bo cope"
142                    echohl WarningMsg | echo " linking failed"
143                else
144                    if s:ShowWarning
145                        exe ":bo cw"
146                    endif
147                    echohl WarningMsg | echo " linking successful"
148                endif
149            else
150                if empty(v:statusmsg)
151                    echohl WarningMsg | echo " linking successful"
152                else
153                    exe ":bo cope"
154                endif
155            endif
156        else
157            echohl WarningMsg | echo ""Exe_Name"is up to date"
158        endif
159    endif
160    setlocal makeprg=make
161endfunc
162 
163func! Run()
164    let s:ShowWarning = 0
165    call Link()
166    let s:ShowWarning = 1
167    if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0
168        return
169    endif
170    let Sou = expand("%:p")
171    let Obj = expand("%:p:r").s:Obj_Extension
172    if g:iswindows
173        let Exe = expand("%:p:r").s:Exe_Extension
174    else
175        let Exe = expand("%:p:r")
176    endif
177    if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou)
178        redraw!
179        echohl WarningMsg | echo " running..."
180        if g:iswindows
181            exe ":!%<.exe"
182        else
183            if g:isGUI
184                exe ":!gnome-terminal -e ./%<"
185            else
186                exe ":!./%<"
187            endif
188        endif
189        redraw!
190        echohl WarningMsg | echo " running finish"
191    endif
192endfunc

2. [图片] 无标题.png    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多