5.6. Creating Multi-File Programs With Visual Studio

Time: 00:02:54 | Download: Large, Large (CC), Small | Streaming, Streaming (CC)

Review

Different integrated development environments (IDE) often have different user interfaces. Nevertheless, they typically perform the same operations and use the same or similar terminology, making the following tutorial relevant to new programmers regardless of their development tool or platform. Please note that Visual Studio is a constantly evolving product. I made the accompanying video with VS 2017 but updated the screen captures below to VS 2019. Please see the Visual Studio Demonstration referenced above.

Creating Source And Header Files

Creating a multi-file program with Visual Studio, or any IDE, follows the same basic pattern as building a single-file program, but we repeat some steps in a cycle or loop. Referring to The steps for creating, compiling, and running a program with VS 2019, we repeat steps 9 through 13. However, as illustrated below, you now choose a C++ (.cpp) or Header File (.h).

The Add New Item window. The programmer can choose between creating a new .cpp or a new .h file.
Visual Studio: creating new program files. The "Add New Item" window shows the options for creating source code and header files. When you enter the file name, you don't need to add the ".h" or the ".cpp" extension, as Visual Studio will automatically add the correct extension for the selected file type.

As you create each file, check the Solution Explorer, verifying that the file name appears in the Solution Explorer panel in the correct project or program. The panel lists all the .cpp files in "Source Files" and all the .h files in "Header Files." The program will not build or compile correctly if any of your .cpp or .h files do not appear in the solution explorer.

A picture of the tabs at the top of the editor. There is one tab for each file in the Time example: driver.cpp, Time.cpp, and Timh.h. The Solution Explorer lists all of the files in a program.
(a)(b)
Visual Studio file indicators. C++ programs often consist of multiple source code and header files. The illustration shows the Time demonstration program (the next section).
  1. The tabs at the editor's top show the open files and their complete names.
  2. Solution Explorer panel shows all the files that form a program. If a file is created incorrectly (e.g., with a control-N), its name will not appear in the Solution Explorer panel. If the name of a file does not appear here, Studio will not make it part of the program.

Compiling and Linking Multi-File Programs

One of the advantages of using an IDE for program development is the ease of compiling multi-file programs. Referring again to The steps for creating, compiling, and running a program with VS 2019, steps 15 and 16 compile and run the program - just as they do for a single-file program.