Using Visual Studio's 'cl' from a normal command line
Asked Answered
F

8

44

Visual Studio 2003 and 2005 (and perhaps 2008 for all I know) require the command line user to run in the 'Visual Studio Command Prompt'. When starting this command prompt it sets various environment variables that the C++ compiler, cl, uses when compiling.

This is not always desirable. If, for example, I want to run 'cl' from within Ant, I'd like to avoid having to run Ant from within the 'Visual Studio Command Prompt'. Running vcvars32.bat isn't an option as the environment set by vcvars32.bat would be lost by the time cl was run (if running from within Ant).

Is there an easy way to run cl without having to run from within the Visual Studio command prompt?

Facsimile answered 17/9, 2008 at 15:22 Comment(0)
I
65

The compilers can be used from command line (or makefiles) just like any other compilers. The main things you need to take care of are the INCLUDE and LIB environment variables, and PATH. If you're running from cmd.exe, you can just run this .bat to set the environment:

C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat

If you're trying to use the compilers from a makefile, Cygwin, MinGW, or something like that you need to set the environment variables manually. Assuming the compiler is installed in the default location, this should work for the Visual Studio 2008 compiler and the latest Windows SDK:

Add to PATH:

  • C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin
  • C:\Program Files\Microsoft Visual Studio 9.0\VC\Bin
  • C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE

Add to INCLUDE:

  • C:\Program Files\Microsoft SDKs\Windows\v6.1\Include
  • C:\Program Files\Microsoft Visual Studio 9.0\VC\include
  • C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include

Add to LIB:

  • C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib
  • C:\Program Files\Microsoft Visual Studio 9.0\VC\lib

These are the bare minimum, but should be enough for basic things. Study the vcvarsall.bat script to see what more you may want to set.

Infuse answered 17/9, 2008 at 15:29 Comment(1)
Depending on the version, the file can be at a different path. For example, I have the build tools installed, and the path is C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\BuildCleora
A
23

Create your own batch file (say clenv.bat), and call that instead of cl:

@echo off
:: Load compilation environment
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
:: Invoke compiler with any options passed to this batch file
"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe" %*

clenv.bat can now be invoked just like cl.exe, except that it will first load the needed environment variables first.

Ajit answered 17/9, 2008 at 20:4 Comment(0)
H
8

For hard set globally environment system variables - add to Path and create Include, LIB.

And change to correct your versions MSVC, Windows SDK, and x86 or x64.

To check variables what you need - can simply run from windows start menu - x64 Native Tools Command Prompt for VS 2019 then type "set path" or "set lib", or "set include".

For example it's my env for compile from cmd.

Path

  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64

Include

  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include
  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include
  • C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt
  • C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared
  • C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um
  • C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt
  • C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt

LIB

  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\lib\x64
  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\lib\x64
  • C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64
  • C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64

enter image description here

enter image description here

enter image description here

Also you can set CL /MD variable to generate code with Dynamic linking runtime libs for less size of Executable - because on default /MT - release with static linking. Also can flexible use and overwrite option from cmd, but some warning message displayed about changed option. https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/3600tzxa(v=vs.100)

https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/kezkeayy(v=vs.100)

enter image description here

Hebrides answered 20/1, 2021 at 15:10 Comment(1)
Best answer for a static config working in 2021Tillandsia
C
7

You can simply run the batch file which sets the variables yourself. In VS08 it's located at:-

C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
Catnip answered 17/9, 2008 at 15:24 Comment(0)
S
2

What the vcvars32 or vsvars32 batch files do is not rocket science. They simply set the PATH, INCLUDE, LIB, and possibly the LIBPATH environment variables to sensible defaults for the particular compiler version.

All you have to do is make sure that those things are set properly for your Ant or makefile (either before invoking them or within them).

For INCLUDE and LIB/LIBPATH an alternative to setting those items in environment variables is to pass those settings to to command line as explicit parameters.

Shrove answered 17/9, 2008 at 17:36 Comment(0)
V
1

The vcvarsall.bat batch file which is run by the Visual Studio command prompt is simply trying to keep your system environment variables and paths nice and clean (and is important if you have multiple versions of Visual Studio).

If you're happy limiting your setup to one version and having a long path and set of environment variables, transfer these settings (manually) to the System Environment Variables (My Computer|Properties --- or Win-Pause/Break).

I'd recommend against this though!

Viglione answered 17/9, 2008 at 15:56 Comment(0)
S
1

The trick is to always use the correct vcvars batch file. IF you have just one version of VisualStudio installed, that's no big problem. If you're dealing with multiple versions like me, it becomes very easy to run a MSVC++ 14 build in a console that was set up with a MSVC++ 15 vcvars file. It might or might not work, but whatever you're getting will be different from what you'd be building from within VisualStudio.

We have dealt with that issue in terp by deriving the proper vcvars file from the chosen compiler and always setting up the environment internally to the tool invocation. This way, you always have the right vcvars file for the compiler you're using.

Just to reiterate: I highly recommend against trying to duplicate manually what the vcvars file does for you. You're bound to miss something or get it just right enough that it looks like it's working while actually doing something slightly different from what you wanted.

Sanctify answered 13/7, 2009 at 18:55 Comment(0)
G
0

My version of opening the visual studio command line for Visual Studio Command Prompt in . Used internally to build a library/project and then perform some extra steps with the resulting DLL files.

Copy these lines to your Compile and execute other steps.cmd file, or similar.

@echo off

REM Load Visual Studio's build tools
call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

REM Choose what you want to do, 1 or 2 by (un)commenting

REM     1. Add your cl.exe (or msbuild.exe or other) commands here
REM msbuild.exe MyProject.csproj
REM cl.exe
REM custom-step.exe  %*
REM pause

REM     2. Open a normal interactive system command shell with all variables loaded
%comspec% /k

In this version of the script, I "stay" in interactive command line mode afterwards. Comment to REM %comspec% /k to only use the script for non-interactive purposes.

Granulose answered 15/6, 2012 at 15:27 Comment(1)
Thank you, but in my solution above for simple compile without always waiting time to set environment.Hebrides

© 2022 - 2024 — McMap. All rights reserved.