Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start →...

16
Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 1 of 16 Visual C++ 6.0 Guide Part I 1 Introduction Microsoft Visual C++ is a software application used to write other applications in C++/C. It is a member of the Microsoft Visual Studio development tools suite, and enables developers to create Windows applications and components, written in C and C++. 2 Starting Visual C++ To start Visual C++, click on start Programs Microsoft Visual C++ 6.0 group Microsoft Visual C++ 6.0 icon. A Visual C++ screen will be displayed briefly before the development environment comes up. The first thing you might encounter is the Tip of the Day window which you could safely close. The first screen you will see after that is the empty development window, called the IDE (Integrated Development Environment) 3 IDE (Integrated Development Environment) Visual C++ 6.0 is an Integrated Development Environment (IDE) and is primarily used to create programs with graphic user interfaces (GUI) to run under MS Windows. The first, almost empty screen that appears is the IDE, where programs are created (editor), compiled, linked, and tested in one environment. Both Windows type programs and non-Windows programs could be developed using Visual C++. 4 The Visual C++ Environment 4.1 Overview The Visual C++ IDE consists of several windows, drop-down menus, toolbars, tools, and other elements like many other programs designed for Windows. It could also be customized by the user to show different windows and toolbars. First, Right click in the center of the windows and make sure that the Output and Workspace items are checked or selected. Workspace Window Text Editor Window Output Window Menu Bar Standard Bar

Transcript of Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start →...

Page 1: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 1 of 16

Visual C++ 6.0 Guide Part I

1 Introduction Microsoft Visual C++ is a software application used to write other applications in C++/C. It is a member of the Microsoft Visual Studio development tools suite, and enables developers to create Windows applications and components, written in C and C++.

2 Starting Visual C++ To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon. A Visual C++ screen will be displayed briefly before the development environment comes up. The first thing you might encounter is the Tip of the Day window which you could safely close. The first screen you will see after that is the empty development window, called the IDE (Integrated Development Environment)

3 IDE (Integrated Development Environment) Visual C++ 6.0 is an Integrated Development Environment (IDE) and is primarily used to create programs with graphic user interfaces (GUI) to run under MS Windows. The first, almost empty screen that appears is the IDE, where programs are created (editor), compiled, linked, and tested in one environment. Both Windows type programs and non-Windows programs could be developed using Visual C++.

4 The Visual C++ Environment

4.1 Overview The Visual C++ IDE consists of several windows, drop-down menus, toolbars, tools, and other elements like many other programs designed for Windows. It could also be customized by the user to show different windows and toolbars. First, Right click in the center of the windows and make sure that the Output and Workspace items are checked or selected.

Workspace Window

Text Editor Window

Output Window

Menu Bar Standard Bar

Page 2: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 2 of 16

You can choose whether or not to display each of these windows, and there are several ways to control the display of the Output and Workspace windows; each window display can be toggled on or off. Please note that as you develop programs in Visual C++, other toolbars may appear automatically.

4.2 Customizing Visual C++ You can customize Visual C++ to suit your needs. Among other customizations, you can add buttons to toolbar, move the toolbars to different location, and create custom shortcut keys.

4.3 Projects and Workspaces The following diagram shows a typical situation, and how Visual C++ represents the structure in the workspace window.

Visual C++ organizes the environment and files in a hierarchical structure. The top level is called a workspace. Only one workspace can be open at any time. A workspace can contain multiple projects. A project usually contains multiple files, grouped by file type. Always ensure that in the workspace window, the FileView tab is selected in order to see the workspace hierarchy.

4.3.1 Workspace A workspace keeps track of which projects are part of the workspace, as well as some settings such as the arrangements of windows. When the workspace contains multiple projects, only one project is active at any moment in time. The active project is indicated in boldface.

One Workspace

Two Projects

FileView Tab

Page 3: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 3 of 16

Every project must belong to a workspace. Therefore, if you attempt to create a project without first creating a workspace, a workspace is automatically created. Workspace information is kept in a workspace file with the filename extension "dsw".

4.3.2 Project A project keeps track of all the files needed to build the final executable program, as well as some settings such as where to place temporary files. Project information is kept in a project file with the filename extension "dsp".

4.3.3 Source Files The source files contain the source code for the program. For a large program, the source code is usually split into multiple files.

4.3.4 Header Files Header files contain information that needs to be shared among multiple source files.

4.3.5 Object files Object files contain machine code from each corresponding source file. The object files are linked together to create the final executable program.

4.4 Create a Sample Project When you create a new project, a project workspace is created for you automatically. You use the Project Workspace window to view and access the various elements of your projects. For this example, we will only be creating one project in the workspace, but you could create or include several projects in one workspace.

On the Menu Bar, Click of File → New, and the following window will open

6. Click

1. Click

2. Click

3. Type orNavigate

4. Typing a project name creates a corresponding folder

5. Check

Next

Page 4: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 4 of 16

4.4.1 Project Types Each project has a Project Type, which you choose when you create the project. The project type specifies to Visual C++ what to generate and also specifies some default settings required in order to build that output type. Remember that for our sample project, we have chosen the Win32 Console Application type.

2. Click

Click

Next

1. Select

Page 5: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 5 of 16

This project type provides character-mode support in console windows. In other words a program having this project type will run in a console window (DOS-like window) that supports standard I/O C functions, such as printf() and scanf().

4.4.2 Workspace FileView The FileView displays all the files associated with a project or projects that you have created. These can include Source, Header and Resource files. When you click on the FileView tab, the FileView pane shows the relationships among the projects and files included in the project workspace. The relationships in the FileView are logical relationships and not physical relationships. Moreover, these relationships do not reflect the organization of files on your hard disk.

4.4.3 Closing the Workspace In the Menu Bar, click on File → Close Workspace; this will close your project workspace. Choosing File → Exit will exit the Visual C++ package and return to the main Windows screen.

4.4.4 Workspace Files You have just set up a new project; although you have not yet created any C files, your project already contains four special files called Project Workspace Files.

• Sample Project.dsp - how your program is to be created from files in the project workspace

FileView Tab

Project Tree

Page 6: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 6 of 16

• Sample Project.opt - stores settings for the project workspace, created when the project is closed

• Sample Project.dsw - stores additional information about the workspace • Sample Project.ncb - stores browse information about your program for Visual

C++ You may want to check your project directory content (d:\Project Sample) and verify that you have these 4 files.

5 Writing your First Program We will be using the Sample Project you have created to write a simple Hello World C program. But first, we need to open the Sample Project workspace you have create earlier.

5.1 Retrieving a Project The first step to writing a program using the Visual C++ IDE is to create a project. Since we have created a project earlier in section 4.??, let us retrieve the Sample Project by opening its workspace. An existing workspace or project can be opened either from within Visual C++, or by double-clicking the corresponding workspace or project file from Windows Explorer. In the Menu Bar, click on File → Open; browse through your disk and locate the Sample Project folder. The folder should contain a “Sample Project.dsw file. Select it then click on Open. Alternatively, you could have opened the most recently saved workspace in one shot. Click on the File drop-down menu, and go over Recent Workspaces menu item towards the bottom and click on Sample Project.

Page 7: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 7 of 16

5.2 Adding Source Files Select File → New from the Main Bar. Since Visual C++ knows that we have created a project, the Files tab is selected by default. Make sure that your source file has the extension .c and NOT .cpp (.c for C files, and .cpp for C++ files) This will open a new editor window to display an empty file, which will have the file name you assigned (HelloWorld.c)

5.3 Entering (Typing and Editing) the Program Enter the following code in the Editor Window. This program purpose of this program is to display the text message Hello World! in a console window.

4. Type filename

3. Check

2. Click

5. Click

1. Click

Page 8: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 8 of 16

/*HelloWorld sample program*/ #include<stdio.h> main() { printf("Hello World!"); }

Save the file by choosing File → Save from the Menu Bar or by pressing Ctrl-S on the keyboard.

5.3.1 The Editor The Visual C++ editor is optimized for writing C/C++ code, though most of the procedures for using this editor should be familiar to you if you have used other Windows-based text editors.

Notice the text indentation and that some text in shown with different colors. This is because automatic indenting and syntax color highlighting are parts of the editor. Syntax coloring uses different colors for various code elements, such as keywords or comments. While using the Text editor, and depending on the text that your mouse pointer is pointed to, you may right-click the mouse button to display a shortcut menu of frequently used commands available for that text.

Page 9: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 9 of 16

5.3.1.1 Text Selection The editor allows you to select words, lines, multiple lines, and column blocks of text to copy, cut, delete, or indent. The left most light-gray column of your editor screen is called the selection margin; when the mouse cursor reaches this light–grey area, the cursor will change into a pointer pointing to the right.

• To select a word/words: Position your cursor before or after the text, click

the left-mouse button and drag over the desired text word/s.

• To select a line of text : In the selection margin, point to the beginning of the text you want to select and click the left mouse button.

Page 10: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 10 of 16

• To select multiple lines of text: In the selection margin, point to the beginning of the text you want to select. Drag either up or down to select the lines of text.

• To select a column block of text: Point to the beginning of the text you want

to select, then, hold down the ALT key and click the left mouse button (do not release). Move the mouse to point at the end of the text you want to select and release the ALT key. To cancel column-select mode, click the left mouse button.

After you have made your selection, the selected text could be copied, deleted, cut or indented .

5.3.1.2 Copy, Paste, Indent After you have selected your text, you could: Copy:

• By pressing Ctrl-C on the keyboard, or • By right-clicking your mouse and select Copy from the pull down menu,

or • Select Edit → Copy from the Menu Bar.

Paste: To paste your text, move the insertion point to any source window where you want to insert the text, and:

• Press Ctrl-V on the keyboard, or • Right-click your mouse and select Paste from the pull down menu, or • Select Edit → Paste from the Menu Bar.

Indent: To indent your text selection, press the Tab key as desired.

Page 11: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 11 of 16

5.3.1.3 Keyboard Shortcuts The text editor many editing commands which are accessible in different ways: by accessing the Edit pull down menu (on the Menu Bar) or from the shortcut menu. The editor supports many text editing commands, but not all of them appear on the menus and toolbars. However, you can learn the most used keyboard shortcut commands and use them when needed.

To access the complete list of keyboard shortcut commands, choose Keyboard Map from the Help menu, and choose Edit in the drop-down list. All of the Text editor commands are listed, along with the current key binding and a brief description. Some helpful keyboard shortcuts are:

Command Keyboard Shortcut Description

SelectAll Ctrl+A Selects the entire document Copy Ctrl+C Copies the selection to the Clipboard Cut Ctrl+X Cuts the selection and moves it to the Clipboard Paste Ctrl+V Inserts the Clipboard contents at the insertion point Delete Del Deletes the selection Redo Ctrl+Y Redoes the previously undone action Undo Ctrl+Z Undoes the last action GoToMatchBrace Ctrl+] Finds the matching brace or parenthesis DocumentEnd Ctrl+End Moves to the end of the document DocumentStart Ctrl+Home Moves to the beginning of the file Find Ctrl+F Finds the specified text

5.3.1.4 Searching for Text The text editor provides advanced find and replace capabilities where, you can search for text in a single source file or in multiple files.

To find a text string in a single file:

• Move the insertion point to where you want to begin your search. • The editor uses the location of the insertion point to select a default

search string. • From Menu Bar, Edit → Find (or Ctrl-F)

3. Click

1. Type the text to find

2. Select options

Page 12: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 12 of 16

To continue your search, use the Find Next or Find Previous shortcut keys. The default shortcut key for Find Next is F3; the default key combination for Find Previous is SHIFT-F3.

To find a text string in multiple source files:

• From the Menu Bar, File → Find In Files

5.4 Building a Project Building a project in Visual C++ involves compiling the source files (HelloWorld.c is a source file) in a project to produce object code and then link these files together to produce the executable file (Sample Project.exe). There are alternative ways to building a project in Visual C++ such as clicking the Build button on the Build Toolbar or pressing the F7 key.

After the build operation starts, the Output Window (will appear if it was not previously showing on the workspace) will provide the user with the status and error information which might occur during the build process. If the build operation is successful, a message will appear showing that there are no errors or warnings encountered. Note that the output window has several view tabs towards the bottom. To see the build messages in the output window, make sure you select the Build tab.

7. Click

1. Type the text to find

6. Click

2. File Type

4. Check options

5. Select or browse

3. Type or browse

Page 13: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 13 of 16

5.4.1 Rebuild All A typical project is composed many files (source and header) and for some situations, the use of Build ( or F7) would not compile all of your project files. For that reason, you could use the Rebuild All menu option from the Build menu instead of Build Sample Project.exe. This would always force Visual C++ to recompile all of your project files and builds the executable again.

5.4.2 Exclude from the Build When Build or Rebuild All operation is invoked, it first attempts to recompile your files located in the tree structure of your project. Moreover, these files have some settings whereby you could instruct the build operation to exclude a particular file from the build. So, a file visible in the tree structure does not guarantee its inclusion in the build process. To exclude a file from the build, select this file and right-click it with the mouse → Settings

Output Window

Build Tab

Page 14: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 14 of 16

Notice that the icon referring to the file will change from to when excluded from the build.

5.5 Error Messages Let us introduce an error in the Hello World program you have typed previously. In the Editor Windows, go to the following line of code and delete the “;” from its right end.

printf("Hello World!") Now build again by choosing any of the above mentioned build methods.

5.5.1 Correcting Errors If the build operation is not successful, error message/s will be reported in the Output Window (it should fail since we introduced an error). For each displayed error, click on the error number in the Output Window and an arrow will appear in the Text Editor window next to the line of code where the error has been detected. For more help with compile and link errors, click on the error line with the error number and press F1.

For the Hello World example, the following error should be reported.

2. Select file

1. Click

3. Check option to exclude

4. Click

Page 15: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 15 of 16

When Visual C++ compiler encounters errors, it would report them along with a “guess” about the potential source of error and the best way to correct it. In a lot of cases, the suspected source of error and the way to fix it are not correct. You should be careful when reading the errors in the output window; do not totally rely on the compilers suggestion. Read your code again and check for errors.

5.6 Executing the Program A program can be executed after it has been successfully built. To execute the program, select Execute Sample Project.exe from the Build menu or click on the Execute button on the Build Toolbar. The output of your program will be a DOS-based window appearing in the foreground of the Text Editor window and will look like the one below. Notice that the line (Press any key to continue) will be appended automatically and will freeze the screen; the screen will only close after the user has pressed any key on the keyboard.

DOS-based Window

Error description and number

The potential location where the error has been detected

Page 16: Visual C++ 6.0 Guide Part Immead/www/docs/VisC-Guide.pdf · To start Visual C++, click on start → Programs → Microsoft Visual C++ 6.0 group → Microsoft Visual C++ 6.0 icon.

Visual C++ 6.0 Guide – Part I Copyright © 2003 DigiPen Institute Of Technology Sep. 8, 2003 Page 16 of 16

5.7 Deleting Source Files To delete a source file from the workspace, go to the workspace windows and locate the project tree structure where your file reside (all source files should be located under your Project files → Source Files folder. With your mouse, select the file you want to delete by left-clicking on the filename, then press the Delete key on the keyboard. Remember that your project tree structure is not an actual Windows Explorer disk representation; so deleting a source file does not actually delete the file from disk. What gets deleted is the logical relationship that links your workspace to the physical file.

6 Visual C++ Resources The Internet provides many Visual C++ resources and references such as the Microsoft Visual C++ User's Guide. For more information, go to the following web page http://msdn.microsoft.com/visualc/previous/vc6/documentation/ and click on the User’s Guide link.