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

"------------------------------------------------------------------------------
"  < 判断操作系统是否是 Windows 还是 Linux >
"------------------------------------------------------------------------------
if(has("win32") || has("win64") || has("win95") || has("win16"))
    let g:iswindows = 1
else
    let g:iswindows = 0
endif
 
"------------------------------------------------------------------------------
"  < 判断是终端还是 Gvim >
"------------------------------------------------------------------------------
if has("gui_running")
    let g:isGUI = 1
else
    let g:isGUI = 0
endif
 
"------------------------------------------------------------------------------
"  < 编译、连接、运行配置 >
"------------------------------------------------------------------------------
" F9 一键保存、编译、连接存并运行
map 
   
   
    
     :call Run()
    
    
     
     
imap 
     
      
      
      
       
       :call Run()
       
       
         " Ctrl + F9 一键保存并编译 map 
        
          :call Compile() 
         
           imap 
           
           
             :call Compile() 
            
              " Ctrl + F10 一键保存并连接 map 
             
               :call Link() 
              
                imap 
                
                
                  :call Link() 
                 
                   let s:LastShellReturn_C = 0 let s:LastShellReturn_L = 0 let s:ShowWarning = 1 let s:Obj_Extension = '.o' let s:Exe_Extension = '.exe' let s:Sou_Error = 0 let s:windows_CFlags = 'gcc\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' let s:linux_CFlags = 'gcc\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' let s:windows_CPPFlags = 'g++\ -fexec-charset=gbk\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' let s:linux_CPPFlags = 'g++\ -Wall\ -g\ -O0\ -c\ %\ -o\ %<.o' func! Compile() exe ":ccl" exe ":update" if expand("%:e") == "c" || expand("%:e") == "cpp" || expand("%:e") == "cxx" let s:Sou_Error = 0 let s:LastShellReturn_C = 0 let Sou = expand("%:p") let Obj = expand("%:p:r").s:Obj_Extension let Obj_Name = expand("%:p:t:r").s:Obj_Extension let v:statusmsg = '' if !filereadable(Obj) || (filereadable(Obj) && (getftime(Obj) < getftime(Sou))) redraw! if expand("%:e") == "c" if g:iswindows exe ":setlocal makeprg=".s:windows_CFlags else exe ":setlocal makeprg=".s:linux_CFlags endif echohl WarningMsg | echo " compiling..." silent make elseif expand("%:e") == "cpp" || expand("%:e") == "cxx" if g:iswindows exe ":setlocal makeprg=".s:windows_CPPFlags else exe ":setlocal makeprg=".s:linux_CPPFlags endif echohl WarningMsg | echo " compiling..." silent make endif redraw! if v:shell_error != 0 let s:LastShellReturn_C = v:shell_error endif if g:iswindows if s:LastShellReturn_C != 0 exe ":bo cope" echohl WarningMsg | echo " compilation failed" else if s:ShowWarning exe ":bo cw" endif echohl WarningMsg | echo " compilation successful" endif else if empty(v:statusmsg) echohl WarningMsg | echo " compilation successful" else exe ":bo cope" endif endif else echohl WarningMsg | echo ""Obj_Name"is up to date" endif else let s:Sou_Error = 1 echohl WarningMsg | echo " please choose the correct source file" endif exe ":setlocal makeprg=make" endfunc func! Link() call Compile() if s:Sou_Error || s:LastShellReturn_C != 0 return endif let s:LastShellReturn_L = 0 let Sou = expand("%:p") let Obj = expand("%:p:r").s:Obj_Extension if g:iswindows let Exe = expand("%:p:r").s:Exe_Extension let Exe_Name = expand("%:p:t:r").s:Exe_Extension else let Exe = expand("%:p:r") let Exe_Name = expand("%:p:t:r") endif let v:statusmsg = '' if filereadable(Obj) && (getftime(Obj) >= getftime(Sou)) redraw! if !executable(Exe) || (executable(Exe) && getftime(Exe) < getftime(Obj)) if expand("%:e") == "c" setlocal makeprg=gcc\ -o\ %<\ %<.o echohl WarningMsg | echo " linking..." silent make elseif expand("%:e") == "cpp" || expand("%:e") == "cxx" setlocal makeprg=g++\ -o\ %<\ %<.o echohl WarningMsg | echo " linking..." silent make endif redraw! if v:shell_error != 0 let s:LastShellReturn_L = v:shell_error endif if g:iswindows if s:LastShellReturn_L != 0 exe ":bo cope" echohl WarningMsg | echo " linking failed" else if s:ShowWarning exe ":bo cw" endif echohl WarningMsg | echo " linking successful" endif else if empty(v:statusmsg) echohl WarningMsg | echo " linking successful" else exe ":bo cope" endif endif else echohl WarningMsg | echo ""Exe_Name"is up to date" endif endif setlocal makeprg=make endfunc func! Run() let s:ShowWarning = 0 call Link() let s:ShowWarning = 1 if s:Sou_Error || s:LastShellReturn_C != 0 || s:LastShellReturn_L != 0 return endif let Sou = expand("%:p") let Obj = expand("%:p:r").s:Obj_Extension if g:iswindows let Exe = expand("%:p:r").s:Exe_Extension else let Exe = expand("%:p:r") endif if executable(Exe) && getftime(Exe) >= getftime(Obj) && getftime(Obj) >= getftime(Sou) redraw! echohl WarningMsg | echo " running..." if g:iswindows exe ":!%<.exe" else if g:isGUI exe ":!gnome-terminal -e ./%<" else exe ":!./%<" endif endif redraw! echohl WarningMsg | echo " running finish" endif endfunc 
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
个人常用vim的IDE开发环境2015最新版。。 vim的快捷键和常用插件都已配好。。 支持cscope ctag,扩展工具栏,支持各类代码跳转查看。可以完全替代sourceinsight 支持自动编译输出窗口 支持代码符号定义快速预览窗口,需要生成完cscope数据库,按Fx键启用,具体哪个 忘了。。 = = 内置tab自动完成和提示,c/c++成员。声明提示。风格配色比较清新简洁。 字体采用courier new,space 4对齐 右边有buffer栏,函数符号栏, 可以按f2切换到文件树列表 f3 f4:大小写格式化 从F1 按到 f12 会有意外发现哦。。你可以一个个试过来。。哈哈。。 安装简: 需要提前安装好 vim gvim/mvim cscope ctag 然后解压在当前目录 执行 install 就行了。。 windows下安装只需要吧 windows目录下的文件覆盖过去就好 macosx上是 mvim 哦。。 sourceinsight的代码查看功能启用,按下工具栏某个生成符号数据库的图标,就行了,各类跳转和搜索都有图标,快速使用。 注意:gvimvim 必须在源码目录下打开才行,他只会查找当前目录下的所有代码来生成。。 安装完 右击代码文件 系统右键菜有个script菜项 的gvim 可以在当前目录下打开代码文件。。 打开一个就可以管理整个工程了。。其他的文件拖进来。。 或者通过f2的目录树访问。。 还有隐藏插件 需要自己按快捷键启用 自己摸索吧。。 比如 文件搜素 图表绘制 matrix动画 。。。 一些集成好的常用插件: 1. tabbar符号列表 2. buffer当前打开文件列表 3. 文件树列表 4. cscope, ctags集成,可进行符号跳转,引用跳转,头文件跳转等。。 5. autotab自动补全 6. c/c++符号提示 7. 静态语法检测 8. 快速翻译 9. 注释绘图 10. 还有好多其他的,暂时想不起来的。。自己摸索吧。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值