Skip to main content

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.

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.