Visual C++ Build Tutorial

Questions

How do I build (compile and link) and execute a single-file program in Visual C++ starting from an already existing C++ file (.cpp extension)? Answer
How do I build (compile and link) and execute a single-file program in Visual C++ starting from an new, empty file? Answer
How do I build (compile and link) and execute a multi-file program in Visual C++ ? Answer

QUESTION: How do I build (compile and link) and execute a single-file program in Visual C++ starting from an already existing C++ file (.cpp extension)?

ANSWER: We will use the program ruleof72.cpp as our example. Note that we are using Visual C++ version 6. However, the steps in Visual C++ version 5 are very similar. Open Visual C++.

1. You begin by opening the file ruleof72.cpp as follows:

    1a. Select File | Open.

    1b. An "Open File" window should appear.  Find the directory on your harddrive (usually C:) where your ruleof72.cpp file is stored.

    1c. Select ruleof72.cpp and click on Open.

    1d. Visual C++ will then display the file ruleof72.cpp in the code window.

2. The standard "windows style" text editing functions are available such as Copy, Cut and Paste.

3. Once the file ruleof72.cpp is open, you build an executable version of the program, as follows:

    3a. Select Build | Build.

    3b. Visual C++ will ask if you want to create a default project workspace. Select Yes.


    3c. Visual C++ will then compile and link ruleof72.cpp and produce the executable file ruleof72.exe. Compilation and linking errors are listed at the bottom of the screen in the message window.

4. Finally, you execute the program as follows:

    4a. Select Build | Execute ruleof72.exe.

    4b. Visual C++ will run the program as a separate application in its own (MS-DOS) application window. Note that the program ruleof72 is included on the task bar at the bottom of the screen.

    4c. Type the appropriate input and press enter. The program output is written to the ruleof72 application window. As shown, press any key to end the run of the program and close the ruleof72 application window.

5. Now close the Project Workspace (and get ready for your next program) by selecting File | Close Workspace. Visual C++ will ask if you want to close all document windows. Select Yes.

QUESTION: How do I build (compile and link) and execute a single-file program in Visual C++ starting from an new, empty file?

NOT YET AVAILABLE

QUESTION: How do I build (compile and link) and execute a multi-file program in Visual C++?

ANSWER: We will use the children's calculator program as our example. This program consists of the following C++ classes and associated files:

Class Header file Implementation file
Calculator calc.h calc.cpp
Face face.h face.cpp
Interface interf.h interf.cpp
Main program kidcalc.cpp

Suppose that these files are in the directory c:\cs331\kidcalc. The following steps will create a new project. Note that we are using Visual C++ version 5. However, the steps in Visual C++ version 4 are very similar.

  1. Select File | New | Project.
  2. Select Win32 Console Application.
  3. Set the project location to c:\cs331.
  4. Set the project name to kidcalc.
    Note that this will change the project location to c:\cs331\kidcalc.
  5. Select OK to create the project.

Once the project is created, you add files to the project, as follows:

  1. Select Project | Add to Project | Files.
  2. Highlight the .h and .cpp files in the project:
    calc.cpp, calc.h,
    face.cpp, face.h,
    interf.cpp, interf.h,
    and kidcalc.cpp.
  3. Select OK to add the files to the project.

The resulting project is shown below in both File View and Class View.

  • (Left) File View showing the files in the project.
  • (Right) Class View showing the classes in the project. This view can be expanded to show each class's member functions and data members.

Once the files have been added to the project, you build (compile and link) and execute the resulting program as follows:

  1. To build the program, select Build | Build kidcalc.exe.
  2. To execute the program, select Build | Execute kidcalc.exe.
    Visual C++ will run the program as a separate application in its own (MS-DOS) application window.

9/99