开发者

How do I compile C programs using Vim's 'make' command with Visual Studio's compiler on Windows 7?

I'm trying to set up Vim to user VS (express) C compiler cl.exe. Adding

set makrprg='c:\Program Files (x86)\Microsoft Visual Stud开发者_JAVA技巧io 10.0\VC\bin\cl.exe'

(I've tried escaping with \\, \\\, \\\\ just to be sure) to my _vimrc file and invoking :make % returns the following:

:! myfile.c >C:\Users\gvkv\AppData\Local\Temp\VIe7BF5.tmp 2>&1

and the loads myfile.c into VS's IDE! Even if cl.exe needs its environment:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat

this is still strange and I have no idea how to proceed.


Judge Maygarden's solution is correct up to proper escaping and solved the Vim problem I had but there were a couple of other issues that I had to resolve to make things work. In particular

  1. cl.exe requires the correct environment as well as the correct path; i.e., adding C:\my\path\to\cl isn't sufficient. Otherwise you will get errors reporting on missing DLLs. The solution is to run vcvars32.bat (or any other batch file which sets up a similar environment) and cl as a single command.

  2. cmd requires any paths with spaces to be double quoted but you can't escape them with \ because :! ... treats \ literally; instead, you must double quote the double quote, ""....

So, for completeness I thought I'd post my actual solution. In Vim (or _vimrc):

:set makeprg=\"\"Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 10.0\\VC\\bin\\vcvars32.bat\"\&\&cl\"

and then you can simply call

:make %

and you're done.

This method is easily generalizable for any compiler. Also, as devemouse's answer suggests, creating a makefile for nmake is probably the best way to go about doing things for any non-trivially sized project (I wanted a Vim-only solution that suits my current work).


I believe the problem with your makeprg statement is mostly due to the spaces in the path. Cmd.exe requires double quotes around the spaces. First, use escaped double quotes around the path (\"). Next escape all back slashes (\). Finally, escape all spaces (\ ).

set makrprg=\"c:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 10.0\\VC\\bin\\cl.exe\"

Alternatively, you could setup your PATH appropriately and just set makeprg to cl.exe directly.


I created a makefile where I had such target:

VCPROJ = /path/to/MyProject.vcproj

all:
    "c:\Program Files\Microsoft Visual Studio 9.0\VC\vcpackages\vcbuild.exe" /nocolor /r $(VCPROJ) Debug

.PHONY: all

I had make in vim's path so did not have to modify makeprg.

This way VS compiled normally whole project and vim parsed the errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜