在vim中调用vc编译并执行c++

 

 

本文提供一种方法, 可以在vim中按F5键来让cl编译并执行当前正在编辑的c++代码. 这样在写一些简单的测试代码的时候, 只要用vim编辑一个文件就可以开始工作, 不需要打开vc创建一个工程了, 比较方便:

 

1. 首先需要创建一个批处理文件, 命名为compile_cpp.bat


@echo off
rem        Author: orit
rem Last-Modified: 2009-10-14
rem       Created: 2009-10-14
rem  Introduction: %1 - compiled file.
set vc_path=c:/Program Files/Microsoft Visual Studio 9.0/VC/bin
set output_file=_temp_output_.exe
set compile_option=/w /Fe%output_file%
echo ====================================================
echo                         Compile......
echo ====================================================
if exist %output_file% (del %output_file%)
call "%vc_path%/vcvars32.bat"
call "%vc_path%/cl.exe" %compile_option% %1
rem Clear screen if no ERROR.
if exist %output_file% (cls) else (goto end)
echo ====================================================
echo                             Output
echo ====================================================
call %output_file%
rem Clear temporary files.
del %output_file%
del *.obj
:end

  

根据自己vc安装的路径和版本不同, 需要修改vc_path.


2. 将 compile_cpp.bat 所在目录添加到系统搜索路径中


3. 修改vim的配置文件vimrc, 在其中添加:

 

autocmd FileType cpp map <F5> <Esc>:w!<CR>:!compile_cpp.bat %<CR>  

 

  

更新:

依葫芦画瓢,写的调用g++来编译的批处理:

 

@echo off
rem Author: xue
rem
Last-Modified: 2011-12-11
rem
Created: 2011-12-11
rem
Introduction: %1 - compiled file.
set gcc_path=D:\Program Files\CodeBlocks\MinGW\bin
set output_file=_temp_output_.exe
set compile_option=-o %output_file%
echo ====================================================
echo Compile......
echo ====================================================
if exist %output_file% (del %output_file%)
rem g++ toBeCompile.cpp -o _temp_output_.exe
call "%gcc_path%/g++.exe" %1 %compile_option%
rem Clear screen if no ERROR.
if exist %output_file% (cls) else (goto end)
echo ====================================================
echo Output
echo ====================================================
call %output_file%
rem Clear temporary files.
del %output_file%
rem del *.obj
:end




这样在vim中按下F5键就可以编译执行当前正在编辑的c++代码了.

 

本文转自:http://orit7.blogbus.com/logs/59646465.html

 

在此向原作者致谢。这是我一直想要解决的问题。

 

 

posted on 2010-12-18 17:45  LateStop  阅读(2078)  评论(0)    收藏  举报

导航