Run cl.exe from cmd - c++

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

Related

How do I setup MSVC "Native Tools Command Prompt" from .bat file?

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 %*

How do I find the exact compiler executable Visual Studio is calling?

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

Qt Windows x64 build succeeds in IDE but not on the command line

I have a Qt project I'm trying to automate. On Windows I build for both 32-bit and 64-bit platforms. I am using Visual Studio 2013. I have the following Qt Kits installed: Desktop Qt 5.5.1 MSVC2013 32bit, and Desktop Qt 5.5.1 MSVC2013 64bit. All four versions (32-bit debug and release, and 64-bit debug and release) can be built and run without any trouble from the IDE. Unfortunately, building from the command line fails with this error:
fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
My command-line system, intended for eventual use in a batch file, and run in my project directory, is:
pushd Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
call vcvars32.bat
popd
md build64
cd build64
G:\Qt\5.5\msvc2013_64\bin\qmake.exe -spec win32-msvc2013 -o Makefile ..\Maps.pro
G:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Release
I use this command in a command window (after setting up the path using vcvars32 so that the Visual Studio tools like nmake, rc and link can be found) and get the error. Note that the command-line method works perfectly for my 32-bit build.
The qmake command line I use is copied from the one recorded in a comment in the Makefile created by the IDE, which claims it is the command used to create that makefile. I know that the "-spec win32-msvc2013" option looks strange, but it works when the IDE uses it.
The jom command line I use is the one used by the IDE, as logged by the Compile Output window.
The only thing I can think of is that Qt sets up some 'magic' environmental variables which I don't know about.
[Yes, I have googled very extensively and looked at other questions and answers here, and can't find anything that fits this case.]
This code
pushd Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
vcvars32.bat
popd
sets up environment for building for 32-bit architecture. Run vcvars32.bat from different directory (I think it should have amd64 in its name, don't have Windows computer at the moment so can't say the exact directory name, but you'll figure out).
Or do
pushd "<DRIVE>:\Program Files (x86)\Microsoft Visual Studio <Version>\Common7\Tools"
vsvars32 x64
popd
Update for VS2017:
For 64-bit architecture use this command:
"<DRIVE>:\Program Files (x86)\Microsoft Visual Studio\2017\<Version>\VC\Auxiliary\Build\vcvars64.bat"
and for 32 bits:
"<DRIVE>:\Program Files (x86)\Microsoft Visual Studio\2017\<Version>\VC\Auxiliary\Build\vcvars32.bat"

How Can Be Used the Command Line of Developer Command Prompt in Vim on Windows?

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 %

How to compile with Visual Studio x64?

I would like to compile in VS2008 x64 from bat file.
When I compile in VS2008 32 bits I call vsvars32.bat.
What do I need to call to compile in x64?
Nowadays the recommendation is not to use vsvar32.bat and use SetEnv.cmd instead to set up your build environment. Pass the /x64 argument to select a 64 bit build environment. This MSDN page has some more information about SetEnv.cmd.
A simple way is to go in the directory : "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64" and execute the file : "vcvarsamd64.bat"
You need to install "X64 Compilers and Tools" feature in your VS2008 IDE.
Please try adding "X64 Compilers and Tools" feature via Control Panel >> Add/Remove Programs (For Vista or later OS, please go to "Programs and Features").
After entering Visual Studio 2008 Maintenance Mode, please expand "Visual C++" node >> click "X64 Compilers and Tools" >> click "Update" button.
Then you can enter "Visual Studio 2008 Command Prompt (x64)"
Hope this helps!
Visual Studio will install shortcuts to launch command prompts that setup an appropriate environment for triggering builds. You can see where the shortcut for the x64 Command Prompt points to by right clicking on it and checking out its target property. It typically points to:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
There is more information here: https://msdn.microsoft.com/en-us/library/x4d2c09s.aspx