When Visual Studio 2019 is installed it installs a shortcut called:
x64 Native Tools Command Prompt for VS 2019
that calls this .BAT file:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat
That contains:
#call "%~dp0vcvarsall.bat" x64 %*
Which I guess just passes "x64" as the first argument to vcvarsall.bat and forwards the rest of its arguments. (I'm not sure what the "%~dp0" is or means).
vcvarsall.bat is much longer.
Anyway my question is: How do you write a .BAT file that sets up the environment that this shortcut does for the remainder of the .BAT file?
ie: I want to write a file called MyBuild.BAT that does:
sets up environment that vcvars64.bat does.
calls some build commands as if they were run from the "Native Tools Command Prompt" ?
On a unix-like system I might write something like:
source vcvars64.bat (runs vcvars64.bat "inline" in caller environment)
run_my_build_commands
As per #HansPassant suggestion the solution I am using is:
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
my_build_command %*
Related
I am using Visual Studio with C++.
For a particular reason, I now need to use CMake to build a piece of the project. I would like to tell CMake to use the same compiler I normally use with Visual Studio.
How can I find the exact cl.exe compiler executable path used by Visual Studio? There are a few of these on my machine.
Inside VS in menu Tools > Command Line select Developer Command Prompt then run where cl. You can also select Developer PowerShell and run Get-Command cl or specifically (Get-Command cl).Source
You can also open start menu, type developer and open the desired terminal
With the newer version, it probably lies in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x64 or C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx86\x86 (for 32-bit). 14.26.28801 will be different in your PC though
I have installed Visual Studio Community 2017 with C++. I wanted to use its compiler from cmd. I am able to use it from Developer Command Prompt for VS 2017 but I am unable to use it from normal cmd. I have tried running vsvarsall.exe by right click-> run as administrator. But nothing happens. Seems like I have to set environment variables manually.
Whenever I try to run the command
cl hello.c
it says hello.c(1): fatal error C1034: stdio.h: no include path set
Visual Studio includes a batch file that prepares the environment for you (actually, the Developer Command Prompt calls it under-the-hood).
I've never tried with the Community Edition, but for VS 2017 Professional it is located at "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat". It may vary if you changed the installation path, of course.
So, all you have to do is to invoke it:
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat"
Something like following should appear
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.7.3
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
After that you can invoke cl, nmake, msbuild as within cmd.
You can also invoke vcvarsall.bat x86 instead (the vcvars32.bat is just a shortcut for that).
You can avoid typing it each time by creating a batch that automatically invokes it and then open a command prompt
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat"
cmd
And then run that batch instead of cmd.
Another option is to add the "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\" to the path so you only have to type vcvars32.bat when you need the developer tools.
Taken from msdn:
A developer command prompt shortcut automatically sets the correct
paths for the compiler and tools, and for any required headers and
libraries. You must set these environment values yourself if you use a
regular Command Prompt window. For more information, see Setting the Path and Environment Variables for Command-Line Builds.
By running vcvarsall.bat in a plain Command Prompt window, you can set environment variables to configure the command line for native 32-bit or 64-bit compilation, or for cross-compilation to x86, x64, or ARM processors.
To run vcvarsall.bat
At the command prompt, change to the Visual C++ installation directory. (The location depends on the system and the Visual Studio installation, but a typical location is C:\Program Files (x86)\Microsoft Visual Studio version\VC\.) For example, enter:
cd "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
To configure this Command Prompt window for 32-bit x86 command-line builds, at the command prompt, enter:
vcvarsall x86
The command file sets the required environment variables for the paths to the build tools, libraries, and headers. You can now use this command prompt window to run the command-line compiler and tools.
If you wish to set the include paths etc. yourself, check out https://learn.microsoft.com/nb-no/cpp/build/reference/cl-environment-variables
open cmd as admin
cd "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\ (check for the correct version of VS)
while in cmd mode, run vcvars64.bat/vcvars32.bat,
now compile ur code for example
cl main.cpp // will give some warning. to supress it type
cl /EHs main.cpp
I have build a Qt project (qwt) using the vs2013 compiler in Qt creator. When I open the .pro file and look at the build steps I see:
qmake: qmake.exe qwt.pro -r -spec win32-msvc2013
Make: nmake.exe in C:\qwt-6.1.2
When I build the project, all goes fine and the project is build without errors. So far so good.
However when I try to build this project using the command line I get errors during the nmake step. I have used the same commands as above (and I'm sure that I'm using the same versions of qmake/nmake).
...\QtCore\qglobal.h(38) : fatal error C1083: Cannot open include
file: 'stddef.h': No such file or directory
My question is, what does Qt do more so that the project does build in Qt creator and not from the command prompt? And how can I copy this behaviour in the command prompt? I'm doing this so I can make a batch file that builds the project (on a machine that does not have Qt creator installed).
In order to use the Visual Studio compilation environment from the command-line you need to call the vcvars batch script to set up the paths correctly.
Either call the vcvars64.bat in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64 folder (that is for VS2013, you can replace the "12.0" with whatever visual studio version you have).
Or the vcvars32.bat in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin folder.
Or use the vcvarsall.bat x86 or vcvarsall.bat x64 calls for the script in C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
Alternatively Visual Studio creates shortcuts in the start-menu called along the lines of VS2013 x64 Native Tools Command Prompt which call those scripts.
It's likely to be that Qt Creator has added additional variables into the build environment, which are not presented when you try to compile manually through cmd.
If you look within Qt Creator under the Projects section there's Build information. Expand the Build Environment information and check to see if there's anything in there different to your machines global environment settings.
I have Visual Studio Express for Windows Desktop installed on this laptop and I can compile a C++ source file with the command cl /EHsc filename.cpp and a C++ program that uses .NET classes with the command cl /clr filenameclr.cpp on the command line of Developer Command Prompt for VS2012 using this instructions: http://msdn.microsoft.com/en-us/library/ms235639.aspx
How can I be able to compile in Vim (text editor) on Microsoft Windows with those commands? :cl /EHsc filename.cpp and :cl /clr filenameclr.cpp does not work.
You need to setup your PATH, INCLUDE and LIB environment variables.
You could do that by running D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat (or wherever it is located on your installation) in the same prompt you're running vim, or even manually setting these environment variables (here is a list of all values for VS2008 and Windows SDK: Using Visual Studio's 'cl' from a normal command line).
However, you just can't run the bat file from vim directly, because it will open another Prompt, so the environment variables will be set only for that new prompt. The other option is just to create bat file which you can put it inside your PATH, for example cvim:
call "D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
"C:\Program Files (x86)\Vim\vim74\gvim.exe"
After that, typing cvim.bat will get you in the environment ready for running :! cl /EHsc %
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:
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.
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.
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:
Add vcvarsall.bat to your PATH. In my case 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>