Getting Started with Visual C++ 6
This tutorial will teach you how to create a project in version six of Visual C++. This version of Microsoft's C++ IDE has probably helped millions of developers in their C++ Programming over the last 8 years. Microsoft Visual C++ has existed in many versions for over 13 years on theWin 32 platform. Version 6 is the last non .NET version and probably the most popular. It's been around since 1999 and has had six service packs.
Make sure your version has been updated with Service Pack 6. You can do this from the Microsoft website- see the "Elsewhere on the Web" link.
We'll begin by creating a new Project. Visual C++ includes the AppWizard. This is a Wizardthat does all the donkey-work of creating project files for you. You should get in the habit of using this as it saves a lot of time.
After starting the IDE, From the File menu click New and the New DIalog will popup. Select "Win 32 Console Application" ( Red Circle 1 in the image), then enter a Project name ( Red Circle 2 in the image) like Example1. Now select somewhere for the project files by clicking the location selector to the right of the Location: edit box and Press OK.
Click the third radio button which says A "Hello, World" application. then click the Finish button. Press Ok on the next page and your workspace panel will now show Example 1 Files, with folders for Source Files, Header Files, and Resource files (There will be none) and a ReadMe.txt with a summary of the project files.
Projects in Visual C++ 6 are organised in Workspaces; an application will normally have one workspace. Each workspace can hold one or more projects. In practice this means one project for each exe or dll.
If you look in the Win 32 folder where you created the project you'll see a number of files. There's the Source Files either c or cpp files (source code), at least one header file (stdafx.h) plus a .dsp file that holds the project details and a .dsw file that holds the workspace details.
Other files include the .plg which is created when you compile. It's a html file which holds the log of the compilation. Double click it and your default browser will open and display it.
The .ncb and .opt files holds information about the settings and log of Visual Studio- both are binary files so of no further interest to us.
This is what the generated example1.cpp looks like.
// ex1.cpp : Defines the entry point for the Command Line application.
int main(int argc, char* argv)
printf("Hello World!\n") ;
This is standard C++ file. In fact it's also a standard C as well though it defaults to cpp. You can mix cpp and c files but don't give them the same name as the compiler will expect to compile both example1.c and example1.cpp into example1.obj and it will object to having two files generate the same object file.
To remove files from the Project, just select each in the tree and press delete. To add a file right click on "Source Files" (for .cpp, or .c) or "Header Files" for .h and click "add files to Folder". This will open a window so you can browse to your file, select and add it.
Click on the Project name in the Workspace tree and press the F7 key. That will Make the Hello World application. You can run it by pressing F5 but you won't see much, as it's a console application and the window will open and close very quickly.
You need to get to the command line (Click the Start button, then click Run, type cmd and press enter) and navigate to the folder where the project files are located. After compiling, a debug folder is created there and this contains the debug executable which you can run.
Debug or Release Projects?
Click "Build" on the main Menu, then "Set Active Configuration" on the drop-down menu. This opens a dialog that shows you all possible build configurations. Just switch to the "Release" configuration (that's the selected configuration in the picture), press Ok and then do another build. This creates a release folder containing the release executable. For a simple "Hello World" Application, the debug executable is 169 KB in size. The release exe is 40KB.
You aren't just limited to these two configurations either. Click "Build" on the main menu then "Configurations". This is where you add extra configurations. For example, a project for one customer may include additional functionality, perhaps implemented in an extra dll. This is where you create that configuration. You can then customize it in the Settings dialog. (Click "Project" on the Main menu then "Settings" on the drop-down).
This dialog is probably the most complex in Visual C++ 6. The defaults are good enough for many applications but there will come a time when you have to modify it. Heres a few examples. For example ALT-F7 is the keyboard shortcut to open the Settings dialog.
The settings tree control lets you create settings for different configurations. Choose a Configuration in the Combo. If you have common folders for resources then Choose "All Configurations" and click the "Resources Tab". Add one or more paths, separated by semi-colons to the Additional resource include directories.
The Project Options at the bottom of the first four settings tabs (C/C++, Link, Resources, Browse Info) show a summary of the options set by the controls on that tab. You can edit these directly or select the tab controls. For example Select the Link tab and scroll down the Project Options until you see /out "Debug/example1.exe" at the bottom line. Now select the p in example and delete it. You'll see the output file name edit box update to reflect this.
Most of the time you don't need to change the settings. Those that you are most likely to do will be specifying extra paths for include files (Select Preprocessor on the category combo on the C/C++ tab) and Resources as described above. For the rest, if you don't need to change them, don't!
Before the line 'std::cout', add the following two lines of code.
for (int i=0;i < 5; i++)
std::cout << "i = " << i << "\n\n";
Select the first line (for int i...) and press F9. This puts a breakpoint there- a red circle in the margin. Now press F5 to start the debugging. You can exit the debugger at any time by pressing Shift + F5.
Without the breakpoint, the program would immediately run to completion and stop. Alternatively you can start a program by pressing either F10 or F11.
You should see the three windows numbered 1-3, in the picture above. 1 Shows local variables, 2 shows the calling stack (for functions) and 3 shows variables you decide to watch. If you can't see these windows, Click "View" on the Menu then "Debug Windows" and then "Watch", "Calling Stack" or "variables" on the sub-menu.
When the program breaks at the for (int i =0;... line, the variables windows shows that i has a nonsense value, like -858993460. This has just picked up whatever was in RAM at theaddress of i. As soon as the loop starts executing, i takes the value 0. Press F10 to step through execution line by line.
Stepping Through Program Execution
Both of these step the program forward by one line. The current line is highlighted by the yellow arrow in the picture. The red spot is a breakpoint.
The difference between F10 and F11 is how functions are dealt with. F10 will call the function and move on to the next line, whereas F11 will step into it.
When the cursor is on either of the std::cout lines, pressing F11 will enter Ostream, the library that implements cout. You should avoid system files until you are more proficient and comfortable with templates. Remember when you don't want to step into a function pressF10.
If you do step into a function by mistake, don't panic. Just press Shift+F11, to take you to the end of the function.
The "Calling Stack" shows which function called which. When you are many levels deep this can be a life saver. Press F11 on a cout statement to see this. As the debugger enters the function code, another level is added to the "Calling Stack" window.
The "watch" windows lets you watch variables, useful when those variables are no longer visible in the "variables" window. You can type or cut and paste the variable name into the window.
All Project settings are kept in a .dsp project file. This includes all configuration information and it's quite readable. Ignore the 3rd line # ** DO NOT EDIT **", but please do take care. Always close the workspace in Visual C++ and make a backup copy before editing the .dsp file.
You can edit a project file using notepad or any text editor and it can be faster to make changes than by using the Settings dialog. For instance you can see where the Debug andRelease Directories are defined, and changing those is easy. Adding extra source or headerfiles is not a hard task. Just add these three lines below to the .dsp to add newfile.cpp to your Source Files folder.
# Begin Source File
# End Source File
It's easy to copy .dsp and .dsw files into a new folder. If you are keeping the same project name then there's no need to edit the .dsw file.
If the project has another name, you'll need to edit the .dsp and change all instances of the old name to the new. Also I've found that the Precompiled headers setting can cause problems. You need to load the project, and change the "precompiled headers" setting on the C/C++ tab (choose "Precompiled Headers" on the category Combo) to Create Precompiled header file the first time you build a project. After that change it to "use precompiled header file" (.pch).
That completes this lesson on compiling with Visual C++ 6.