Reputation: 59388
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?
Upvotes: 3
Views: 1986
Reputation: 65
You can use a plugin vcvars.vim to set up environment variables.
In Neovim, using lazy.nvim as package manager, one can set up environment variables with vcvars.vim by
{
"hattya/vcvars.vim",
event = "VeryLazy",
config = function()
vim.cmd([[
let vcvars_dict = vcvars#get(vcvars#list()[-1])
let $PATH = join(vcvars_dict.path, ';') .';'. $PATH
let $INCLUDE = join(vcvars_dict.include, ';') .';'. $INCLUDE
let $LIB = join(vcvars_dict.lib, ';') .';'. $LIB
let $LIBPATH = join(vcvars_dict.libpath, ';') .';'. $LIBPATH
]])
end,
},
Upvotes: 0
Reputation: 211
Mud's solution did not work for me with Visual Studio 2017 Community. Paths are different. I think DevSolar's way to use vcvarsall.bat is more reliable, as it lets VS take care of the correct paths.
As I don't want to carry around a .bat file with my vim config, I was looking for a solution to work only inside the .vimrc:
c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
.Put the following in your .vimrc to compile current file by F8
nnoremap <F8> :!vcvarsall.bat && cl /EHsc % <CR>
Upvotes: -1
Reputation: 70391
Expanding on the comment by David Rodríguez - dribeas, and seeing how vcvarsall.bat
accepts parameters, I found it helpful to have the following file in my source trees:
vc.bat
:
call "%VS100COMNTOOLS%\..\..\VC\vcvarsall.bat" amd64 & %*
Adjust MSVS version and platform as appropriate, or make them additional parameters if you like. The %VS...COMNTOOLS%
environment variable is set by the MSVC installer, which makes the above independent of the actual installation path.
This allows me to run any <command>
in the proper MSVC environment by running it as:
vc.bat <command>
If called without parameters, it will just set the environment for the current shell without executing any further commands.
If you want to go one step further, there is CMake, a meta-buildsystem that allows you to generate Unix Makefiles, MSVC solutions and several other buildfile types from a single (CMake syntax) configuration. One of the buildfile types supported on Windows are NMake Makefiles... and nmake <target>
on Windows handles just about as well as make <target>
does on Unix.
Upvotes: 1
Reputation: 29021
You can update environment variables from within Vim. For instance, to add "c:\foo" to the start of your PATH:
let $PATH='c:\\foo;'.$PATH
I copied the relevant portions of my vcvars*.bat file to my vimrc:
if !exists("visual_studio_paths_added")
let visual_studio_paths_added = 1
let $PATH="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\BIN;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\Tools;C:\\Windows\\Microsoft.NET\\Framework\\v3.5;C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\VCPackages;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\bin;".$PATH
let $INCLUDE="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\INCLUDE;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\INCLUDE;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\include;".$INCLUDE
let $LIB="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\LIB;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\lib;".$LIB
let $LIBPATH="C:\\Windows\\Microsoft.NET\\Framework\\v3.5;C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\LIB;".$LIBPATH
endif
Adapt as needed given your version of Visual Studio and installation directory.
In my case, I put this in a function. I call :vcvars
when I want to work with Visual Studio. But you could just have those changes always included.
Upvotes: 4