I can compile from the command line by running vcvarsall.bat, then running cl.exe with my source file as the only argument. The problem is that just running cl.exe without first setting up the environment using the aforementioned batch file causes errors due to “missing libraries”.
I can picture a couple of workarounds to this, for example creating a batch file in my project directory that includes vcvarsall.bat and does all the compiling. That seems to me to be a very cumbersome and inflexible solution though. I would much prefer to be able to compile and run any C or C++ file from Vim like one would in Unix:
:nnorebind <F5> :! g++ %<CR>
I don’t want to add the pile of paths that vcvarsall.bat adds to the environment permanently though. I don’t know how to phrase my question any better than this: How do I set up Vim to be able to use the Visual Studio 2010 C++ compiler?
You can update environment variables from within Vim. For instance, to add “c:\foo” to the start of your PATH:
I copied the relevant portions of my vcvars*.bat file to my vimrc:
Adapt as needed given your version of Visual Studio and installation directory.
In my case, I put this in a function. I call
:vcvarswhen I want to work with Visual Studio. But you could just have those changes always included.