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?
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!
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.
You can simply run the batch file which sets the variables yourself. In VS08 it's located at:-
Create your own batch file (say clenv.bat), and call that instead of cl:
clenv.bat can now be invoked just like cl.exe, except that it will first load the needed environment variables first.
My version of opening the visual studio command line for Visual Studio Command Prompt in visual-studio-2010. 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.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.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.