Compiling from command line with Microsoft Visual C++

Visual C++ supports the possibility to compile 32 bits programs with no need to use the integrated development environment, simply from the comman line.

For that, first of all we need that a series of system environment variables be suitably defined. More concretely they are the variables %INCLUDE% and %LIB% that define the directories where the include and library files are, as well as it is also recommendable to add the directories where the executable file that we need to compile are to the path.

Luckily, during the initial installation of Visual C++ a BAT file called VCVARS32.bat would have been automatically created defining all these environment variables for us. This BAT file is located at the subdirectory BIN that hangs from the directory where you have installed Visaul C++, that by default would be something similar to:

C:\Program Files\Microsoft Visual Studio\VC98\BIN
Maybe this file is already being automatically executed whenever our operating system starts up, to know that we can just try this test: from the command line type:
if the system shows a series of paths separated by commas the file has already been executed. If a message telling that the environment variable has not yet been defined (is not defined) is shown you will have to execute the file VCVARS32.bat manually.

The command line compiler (CL).

In the BIN subdirectory where our compiler is (that one which VCVARS32 includes in the PATH) there is an utility called CL.EXE specially designed to compile programs from the command line. Its prototype is the following one:
CL option(s) file(s) @command_file /link link_options
where the only obligatory parameter is file(s) that shall be a list of the sourcecode files and libraries that we want to compile together. The different file types will be distinguished according to its extension, thus:
c will be considered C language source code files, and
cpp and cxx will be considered C++ language source code files.
if we want to use other extensions for our sourcecode files we will have to precede the name of the file with /Tc for files in C language and with /Tp for files written in C++ language (with no space character between these espefier tags and the file name).

The option(s) parameter can go intermingled between the files, or before or after them. They are compiling options, because there are many, mainly different types of optimizations, I'm not going to detail them (consult the compiler help typing CL /HELP for a complete list, or take a llok at the Article Compiler Command-Line Syntax of the Visul C++ Programmer's Guide includes in the MSDN).

The @command_file parameter is used to specify the name of a file that contains other command line options preceded by an at sign (@). CL will insert the content of that file at the point of the command line where you specify its name as if you typed the content of the file.

Finally /link link-options is used to specify options to the linker, that are automatically called by CL if the /c option is not specified. It serves to specify the type of executable file that we want to create, the location of libraries and include files and other linker options like the debugging level. Consult the Visual C++ help for more details.

In summary

In summary, if you want to compile a unique C++ source code file into an executable like for example test.cpp and you have already executed VCVARS32.BAT it would be enough to write at the command line:
LC test.cpp
that would generate the file test.exe.

Microsoft and Visual C++ are registered trademarks of Microsoft Corporation

©The C++ Resources Network, 2000 - All rights reserved
Return back