How to compile C programs using the Vim 'make' command with the Visual Studio compiler on Windows 7?

I am trying to configure Vim to a custom VS (express) compiler C cl.exe . Adding

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

(I just tried to escape with \\ , \\\ , \\\\ ) in my _vimrc file and call :make % returns the following:

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

and loads myfile.c in the VS IDE! Even if cl.exe needs its own 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.

+6
c vim windows-7
source share
3 answers

I believe the problem with your makeprg expression is mainly due to spaces in the path. Cmd.exe requires double quotes around spaces. First, use escaped double quotes around the path (\ "). Then do all backslashes (\). Finally, avoid all spaces (\).

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

Alternatively, you can configure PATH accordingly and just install makeprg in cl.exe directly.

+3
source share

Judge Maygarden’s decision was right until he left and the Vim problem I had, but there were a few other issues that I had to solve to get everything working. In particular,

  • cl.exe requires the correct environment as well as the correct path; that is, adding C:\my\path\to\cl not sufficient. Otherwise, you will receive an error message without DLL s. The solution is to run vcvars32.bat (or any other batch file that installs a similar environment) and cl as one command.

  • cmd requires that any paths with spaces be double, but you cannot escape them with \ , because :! ... :! ... processes \ literally; instead, you must double-quote the double quote, ""...

So, for completeness, I thought I would 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 just call

 :make % 

and you're done.

This method is easily generalized to any compiler. Also, as devemouse's answer suggests, creating a makefile for nmake is probably the best way to do something for any non-trivial project (I wanted the Vim-only solution to fit my current job).

+5
source share

I created a makefile where I had this goal:

 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 the vim path, so there was no need to modify makeprg .

Thus, VS usually compiled the whole project and vim analyzed the errors.

+3
source share

All Articles