Visual Studio / Visual C++ FAQ 



Visual Studio questions
* How do I build (compile and link) and execute a single-file program in Visual C++? 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++?

ANSWER: We will use the program basics.cpp (one of the stream I/O sample programs) as our example. Note that we are using Visual C++ version 6. However, the steps in Visual C++ version 5 are very similar.

You begin by opening the file basics.cpp

build1a.gif (19566 bytes)  
  1. Select File | Open.
  2. Open the file basics.cpp.

  3. VC++ will then display the file basics.cpp in the code window (outlined in red).

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

build1b.gif (19806 bytes)  
  1. Select Build | Build.
  2. Visual C++ will ask if you want to create a default project workspace. Select Yes.

  3. Visual C++ will then compile and link basics.cpp and produce the executable file basics.exe
    Compilation and linking errors are listed in the message window (outlined in red).

Finally, you execute the program. 

build1c.gif (19601 bytes)  
  1. Select Build | Execute basics.exe.
  2. Visual C++ will run the program as a separate application in its own (MS-DOS) application window.

  3. Note that the program basics is included on the task bar at the bottom of the screen.

Back to the questions


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 6. However, the steps in Visual C++ version 5 are very similar. 

build2a.gif (14932 bytes)  
  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

  5. Note that this will change the project location to c:\cs331\kidcalc.
  6. Select OK to begin creating the project.

Then you specify the kind of Console Application. 

build2b.gif (20252 bytes)  
  1. Select An empty project.
  2. Click Finish.
  3. Click OK to create the project workspace.

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

build2c.gif (13860 bytes)  
  1. Select Project | Add to Project | Files.
  2. Highlight the .h and .cpp files in the project: 

  3. calc.cpp, calc.h, 
    face.cpp, face.h, 
    interf.cpp, interf.h,
    and kidcalc.cpp.
  4. Select OK to add the files to the project.

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

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

 

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

build2e.gif (7497 bytes)  
  1. To build the program, select Build | Build kidcalc.exe.
  2. To execute the program, select Build | Execute kidcalc.exe

  3. Visual C++ will run the program as a separate application in its own (MS-DOS) application window.

Back to the questions


Last updated: 7/99