How to Compile and build Visual Studio (Visual C++) Projects on the Command Line

Compiling on the Command Line

The project management and building of projects in Visual C++ IDE is very usefull and easy to use. Sometimes we might need to build projects from the command line,it might be more usefull in such casess.Visual C++ project files are very similar to carefully formatted scripts for the Nmake utility, Microsoft's version of the standard Make program, but if you need to compile from the command line, you will need to export the .mak file.

If you're going to directly modify a Visual C++ produced .mak file, you should be careful about how you do it, but if you do make a mistake, you can always go back to the .dsw file and use it to export a new makefile.

Once you've properly set up the environment to include the location of the tools in your PATH= environment variable, the libraries in your LIB= environment variable, and the headers in your INCLUDE= environment variable, you can run a build for a project named Sample by making the project's directory current and typing the following command in a Windows NT Command window or Windows 95 DOS Window:

nmake /f sample.mak

This will kick off a build for the default target. At the command line, the default target isn't the same as that defined in the IDE, as you can't set the default target for command line builds. You can force a given build type to be done by naming the target of your desire on the command-line. For example:

nmake /f sample.mak CFG="Sample - Win32 Debug"

The exact string you can provide with the CFG option is dependent on the names of the targets that you have specified in the project. You can see a list of acceptable targets by providing the CFG option with no setting, like this:

nmake /f sample.mak CFG

The Nmake script will then enumerate the targets it knows how to build.

The C++ compiler and all of the other Windows programming tools with which you are familiar are also available at the command-line. The available command line tools are listed in the following table:

Tool Command
C/C++ Compiler Cl
Librarian Lib
Linker Link
Maintenance Utility Nmake
Resource Compiler Rc
Interface Definition Language Compiler Midl

You can use these tools to compile an older project. You may also wish to use them when you know that you are planning something special; for example, you might craftily use Cl to invoke the C pre-processor to massage a file that you will use later as an input file to some other tool.

Without exception throughout this book, we'll be working with projects that can be built within the Developer Studio. You are more than welcome to try building the programs from the command line to enhance your understanding of the underlying tools and their options. Since this book will concentrate on the Microsoft Foundation Classes, we won't be spending that much time on the underlying build tools or their special capabilities.

No comments: