Using Visual C++ Toolkit 2003 from the Command Prompt

Summary: In this article, Brian Johnson describes how to use the Microsoft Visual C++ compiler that ships with the Visual C++ Toolkit 2003 from the command line. This article is for beginners who've never used a command line compiler before. (4 printed pages)

The Visual C++ Toolkit 2003 package is designed to enable developers to build and test applications with the full optimizing compiler that ships with Visual Studio .NET 2003. This package isn't a substitute for the full version of the product; rather, it's a subset that lets you evaluate and explore the optimizing compiler. In addition, it will let you build small applications suitable for academic study. I've written this article to help readers who are very new to C++ learn how to use the Visual C++ Toolkit 2003 compiler from the command line to build simple applications.

The Visual C++ Toolkit 2003 doesn't contain any sort of IDE, and so you'll need to build applications using the compiler the old fashioned way, with a text editor and command prompt. In this article, I'll outline the steps that you need to take to build and run applications with the Visual C++ Toolkit 2003 using only Notepad and a command prompt.

To get started, you'll need to install the Visual C++ Toolkit 2003 package. We have a toolkit home page at http://msdn.microsoft.com/visualc/vctoolkit2003 that provides a link to the download center page. Visit that page and download and install the compiler. To help you get started, you will also find articles and a frequently asked questions list.

After you install the toolkit, you'll find a new Visual C++ Toolkit 2003 folder on your Start menu. This menu contains a link to the Visual C++ Toolkit 2003 Readme file and it includes a shortcut to the Visual C++ Toolkit 2003 Command Prompt. We'll do all of our work from this shortcut to the command prompt, so go ahead and click on it to open it now.

The Visual C++ Toolkit 2003 Command Prompt sets a number of paths and options that are required for building applications with the compiler. The key settings that the compiler requires are these:

Set PATH=C:\Program Files\Microsoft Visual C++ Toolkit 2003\bin;%PATH%
Set INCLUDE=C:\Program Files\Microsoft Visual C++ Toolkit 2003\include;%INCLUDE%

Set LIB=C:\Program Files\Microsoft Visual C++ Toolkit 2003\lib;%LIB%

These settings place the path of the Visual C++ Toolkit 2003 at the beginning of your PATH variable. Likewise, the INCLUDE variable sets the path to the header files that ship with the toolkit, and the LIB variable sets the path to the library files that ship with the toolkit. The header files declare the functions available in the library files and make them accessible from your application. I'm not going to cover too much of that in this article, but you will find some recommended books for learning C++ at the end of this article.

The Microsoft C++ command line compiler is named CL.EXE. To compile a C++ application using this tool, you'll normally write your application using a text editor such as Notepad, save the file with an extension of .cpp, and then invoke the compiler, passing the name of the file that you want to compile as a parameter. Let's go ahead and do that now.

The first application that's traditionally built when learning a language is Hello World. This is a simple application that prints the words "Hello World!" to your command prompt. The standard C++ Hello World application looks like this:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World! \n";

return 0;
}

Hello.cpp

To test this bit of code, create a new folder by typing the following in the Visual C++ Toolkit 2003 Command Prompt window:

mkdir Hello

Navigate to the Hello folder by typing:

cd Hello

Now that you're in the Hello folder, create a new Hello.cpp file by typing:

notepad Hello.cpp

Notepad will open and you'll be prompted to create the new file. Type or paste the code from the Hello.cpp listing into Notepad. Click File, then Save in Notepad and then go back to the Visual C++ Toolkit 2003 Command Prompt window and type:

cl Hello.cpp 

You'll see a number of warnings and messages that you can safely ignore for now. You should see the following message at the end of the list:

Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.


/out:hello.exe
hello.obj

This message indicates that Hello.cpp was compiled into hello.obj and that it was linked to the appropriate C++ libraries to create the Hello.exe application. You can test the Hello.exe application by typing hello at the command prompt. You should see the following message.

C:\Program Files\Microsoft Visual C++ Toolkit 2003\Hello>hello

Hello World!

Earlier I mentioned that you may have seen some warnings when you compile the application, including a warning about the C++ exception handler and whether unwind semantics are enabled. I'm not going to cover that specific topic in this article, but you can specify the /EHsc command line option in your compile command to specify the synchronous exception handling model and to make the messages go away:

cl /EHsc Hello.cpp 

Now that you have the Visual C++ Toolkit 2003 Command Prompt window open, take a look at the number of options available with the Visual C++ compiler. To get the list, run the compiler with the /help option.

cl /help

The large list of options is common for modern C++ compilers and it should give you some indication of how complex the compiler is. You can get more information about the compiler options on the Visual C++ Compiler Options page .

Now that you've seen how to build and run a very simple application using the Visual C++ Toolkit 2003, you should be able to build and test any of the examples that you might find in an introductory C++ book. Here are a few introductory books that I would suggest reading if you're very new to C++:

If you already have a programming background and you want to get a fast start with C++, I would suggest Essential C++ by Stan Lippman.

You can continue to explore the Visual C++ Toolkit 2003 by reading the excellent articles written by Kate Gregory, which are included with the toolkit. These articles are also available online from the MSDN toolkit home page .

This is the first of what might turn out to be a number of articles that explain how to use the Visual C++ Toolkit 2003 to explore C++. Please feel free to drop me a note atbrianjo@microsoft.com with comments about this article and with suggestions for other articles that you would like to see on this topic.

Comments