用于Gvim 或 Vim 配置文件的一键编译与运行函数(注:需要机器上安装了GCC才行)
本代码只加入了对C/C++的编译与运行,如果要加入其语言的可以参考此代码加入即可
同时,本代码加入了对Windows下用Gvim编译UTF-8编码格式的源文件时中文乱码的解
决方法(也就是在编译选项中加入了 “-fexec-charset=gbk”)。
将上面的代码加入到_vimrc文件里即可
001 | "------------------------------------------------------------------------------ |
002 | " < 判断操作系统是否是 Windows 还是 Linux > |
003 | "------------------------------------------------------------------------------ |
004 | if(has("win32") || has("win64") || has("win95") || has("win16")) |
010 | "------------------------------------------------------------------------------ |
012 | "------------------------------------------------------------------------------ |
019 | "------------------------------------------------------------------------------ |
021 | "------------------------------------------------------------------------------ |
023 | map <F9> :call Run()<CR> |
024 | imap <F9> <ESC>:call Run()<CR> |
027 | map <c-F9> :call Compile()<CR> |
028 | imap <c-F9> <ESC>:call Compile()<CR> |
031 | map <c-F10> :call Link()<CR> |
032 | imap <c-F10> <ESC>:call Link()<CR> |
034 | let s:LastShellReturn_C = 0 |
035 | let s:LastShellReturn_L = 0 |
037 | let s:Obj_Extension = '.o' |
038 | let s:Exe_Extension = '.exe' |
041 | let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' |
042 | let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' |
044 | let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' |
045 | let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' |
050 | if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx" |
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 |
057 | if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou))) |
059 | if expand("%:e") == "c" |
061 | exe ":setlocal makeprg=".s:windows_CFlags |
063 | exe ":setlocal makeprg=".s:linux_CFlags |
065 | echohl WarningMsg | echo " compiling..." |
067 | elseif expand("%:e") == "cpp" || expand("%:e") == "cxx" |
069 | exe ":setlocal makeprg=".s:windows_CPPFlags |
071 | exe ":setlocal makeprg=".s:linux_CPPFlags |
073 | echohl WarningMsg | echo " compiling..." |
077 | if v:shell_error != 0 |
078 | let s:LastShellReturn_C = v:shell_error |
081 | if s:LastShellReturn_C != 0 |
083 | echohl WarningMsg | echo " compilation failed" |
088 | echohl WarningMsg | echo " compilation successful" |
091 | if empty(v:statusmsg) |
092 | echohl WarningMsg | echo " compilation successful" |
098 | echohl WarningMsg | echo ""Obj_Name"is up to date" |
102 | echohl WarningMsg | echo " please choose the correct source file" |
104 | exe ":setlocal makeprg=make" |
109 | if s:Sou_Error || s:LastShellReturn_C != 0 |
112 | let s:LastShellReturn_L = 0 |
113 | let Sou = expand("%:p") |
114 | let Obj = expand("%:p:r").s:Obj_Extension |
116 | let Exe = expand("%:p:r").s:Exe_Extension |
117 | let Exe_Name = expand("%:p:t:r").s:Exe_Extension |
119 | let Exe = expand("%:p:r") |
120 | let Exe_Name = expand("%:p:t:r") |
123 | if filereadable(Obj) && (getftime(Obj) >= getftime(Sou)) |
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..." |
130 | elseif expand("%:e") == "cpp" || expand("%:e") == "cxx" |
131 | setlocal makeprg=g++\ -o\ %<\ %<.o |
132 | echohl WarningMsg | echo " linking..." |
136 | if v:shell_error != 0 |
137 | let s:LastShellReturn_L = v:shell_error |
140 | if s:LastShellReturn_L != 0 |
142 | echohl WarningMsg | echo " linking failed" |
147 | echohl WarningMsg | echo " linking successful" |
150 | if empty(v:statusmsg) |
151 | echohl WarningMsg | echo " linking successful" |
157 | echohl WarningMsg | echo ""Exe_Name"is up to date" |
160 | setlocal makeprg=make |
164 | let s:ShowWarning = 0 |
166 | let s:ShowWarning = 1 |
167 | if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0 |
170 | let Sou = expand("%:p") |
171 | let Obj = expand("%:p:r").s:Obj_Extension |
173 | let Exe = expand("%:p:r").s:Exe_Extension |
175 | let Exe = expand("%:p:r") |
177 | if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou) |
179 | echohl WarningMsg | echo " running..." |
184 | exe ":!gnome-terminal -e ./%<" |
190 | echohl WarningMsg | echo " running finish" |
|