10

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

cbuchart
  • 10,847
  • 9
  • 53
  • 93
keen_learner
  • 211
  • 4
  • 8
  • 1
    Possible duplicate of [Using Visual Studio's 'cl' from a normal command line](https://stackoverflow.com/q/84404/608639) – jww Nov 23 '18 at 17:38

3 Answers3

10

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.

cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • 1
    Do I need to invoke it everytime I use cmd ? Isn't there any permanent solution like permanently setting those variables ? – keen_learner Jun 13 '18 at 07:07
  • @keen_learner Yes, you have to invoke it, or just open the Developer Command Prompt instead ;). Another option is to create a batch or add the directory to the path. I've edited the answer to cover those cases. – cbuchart Jun 13 '18 at 07:11
  • If I could upvote this 10 times I would. Thanks for a great and straightforward answer :-) – Stephen Wilson Oct 19 '20 at 08:27
3

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

  1. 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"

  2. 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

MSalters
  • 173,980
  • 10
  • 155
  • 350
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • Do I need to invoke it everytime I use cmd ? Isn't there any permanent solution like permanently setting those variables ? I was looking for something like setting INCLUDE, LIB, LIBPATH manually – keen_learner Jun 13 '18 at 07:08
0
  1. open cmd as admin

  2. cd "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\ (check for the correct version of VS)

  3. while in cmd mode, run vcvars64.bat/vcvars32.bat,

  4. now compile ur code for example

    cl main.cpp // will give some warning. to supress it type

    cl /EHs main.cpp

Amaresh Kumar
  • 586
  • 1
  • 8
  • 20