Fundamentals of C + + development

Source: Internet
Author: User

Hardware configuration:
More than 586 PC compatible machine or brand machine, equipped with color display, mouse, keyboard, memory is not less than 20MB, hard
Disk free space not less than 60MB. Recommended configuration for Memory 32MB or 64MB (or above), free disk space 500MB
Above.
Software configuration:
1. Operating system: Windows98,windows 2000,windows Xp,linux,unix
2. Integrated development Environment:
(1) under the Windows98,windows 2000,windows XP system, the main development of the compilation environment has turbo
C + + 3.0, Borland C + + 3.1, Microsoft Visual c++6.0, DJGPP, where DJGPP is GCC
Dos/windows the migration under the operating system. The development environment used in this lab guide is Microsoft Visual c++6.0
English version. The Chinese version is just a partial menu of the Chinese language and the use of words is very inaccurate.
(2) under the Linux and UNIX systems, the GCC compilation environment is used. The instruction Book of this experiment does not involve the related content,
Interested students can learn by themselves.
1.2 Use of Visual C + + 6.0
1.2.1 Visual C + + Introduction
Visual C + + is a C + + program development package in the Visual Studio Development Toolbox of Microsoft Corporation. Visual
Studio provides a complete set of tools for developing Internet and Windows applications, including Visual c++,visual
Basic,visual foxpro,visual Interdev,visual J + + and other ancillary tools, such as code management tools, Visual
SourceSafe and online Help system MSDN. In addition to the C + + compiler included in the Visual C + + package, all libraries,
Examples and documents required for creating Windows applications.
From the earliest 1.0 release to the latest version of the. NET version, Visual C + + has changed a lot
The interface, function, and library support are many enhancements. Version 6.0 in the compiler, MFC class library, editor to
And on-line help system, and so on more than the previous version made a great improvement.
Visual C + + is generally divided into three versions: learning, professional, and enterprise, with different versions suitable for different types of
Application development. Any of these three versions can be used in an experiment.
1.2.2 Installation and startup of Visual C + +
If your computer does not have visual c++6.0 installed, you should first install visual c++6.0. Visual C + + is a visual
Part of studio, so you need the visual Studio CD, perform the setup.exe in it, and press the on-screen prompts
Installation can be done. When you are finished, you will receive the Microsoft visual in the Program submenu of the Start menu in Windows
C + + programming experiment instruction Book
2
Studio Sub-menu, shown in 1-1
Figure 1-1 Microsoft Visual Studio Launch submenu
When you need to use Visual C + +, simply select the Start program from the desktop, Microsoft visual Studio
Visual c++6.0 command, which appears on the screen after a short display of the visualc++6.0 copyright page, the visual
c++6.0 a main interface consisting of Windows, toolbars, menus, tools, and other parts. Through this interface, use
Users can create, test, and debug applications.
Figure 1-2 Visual c++6.0 Interface
1.2.3 Common function keys and their meanings
To enable programmers to quickly and easily complete program development, the development environment provides a number of shortcuts to simplify some
Steps for common operations. Keyboard operation is straightforward, simple, and very convenient, so programmers like to use keyboard commands
To control the operation. Here are some of the most commonly used function keys, I hope in the experiment gradually mastered.
Action type function key corresponding menu meaning
File Operations Ctrl + N file| New Create a file, project, etc.
Ctrl+o file| Open project, file, etc.
Ctrl+s file| Save saves the current file
Edit Operation Ctrl+x Edit| Cut cut
CTRL + C edit| Copy replication
Ctrl + V edit| Paste paste
CTRL + Z edit| Undo undoing the last action
Ctrl+y edit| Redo Repeat Last Action
CTRL + A edit| Select All Selects all
Del edit| Del Delete a character after the cursor
Establish program Operation Ctrl+f7 build| Compiler current file to compile source files
Ctrl+f5 build| Run EXE runs the current project
C + + programming experiment instruction Book
3
F7 build| Build EXE to build executable program
F5 build| Start debugging starting the Debug program
Debug F5 debug| Go to continue running
F11 debug| Step into enters function body interior
Shift+f11 debug| Step out runs from inside the function body
F10 debug| Step over executes a line statement
F9 setting/clearing breakpoints
CTRL+F10 debug| Run to cursor runs to cursor location
Shift+f9 debug| QuickWatch quickly view the value of a variable or an expression
Shift + F5 Debug| Stop Debugging Stops debugging
Note: These shortcut commands have the corresponding menu and toolbar buttons, 1.3:
Figure 1-3 Shortcut key Commands menu and button
1.2.4 Entering and editing the source program
First, the simplest case is that the program consists of only one source program file, a single-file program (about multiple files
The operation of the program is described later in this chapter).
1. How to create a new C + + source program
If you want to create a new C + + source program, you can take the following steps:
Select the file command in the main menu bar of the main Visual C + + window and select the new command, as
Figure 1-4.
Figure 1-4 Selecting the new action in the File menu
Compile
(CTRL+F7)
Rebuild All
Build (F7)
Stopbuild
(Ctrl+break)
Execute Program
(Ctrl+f5
Go (F5
Insert Remove
Breakpoint (F9)
C + + programming experiment instruction Book
4
Figure 1-5 Creating a C + + source file
At this point, a new dialog appears on the screen, clicking the files (file) in the top of the dialog box
, select the "C + + source file" entry in the list to create a new C + + source program file, and then in the dialog
In the right half of the box, enter the storage path of the source program file you want to edit, assuming
D:\ Experiment one), indicating that the source program files that will be prepared for editing are stored in the
Zhang San \ Experiment One "sub-directory. Enter the source for editing in the file text box above it
The name of the sequence file (input MyFirstCpp.cpp), 1-5. In this way, the source program that will be entered and edited will
The file name in MyFirstCpp.cpp is stored in the "\ Computer Zhang San \ Experiment One" directory on D-disk, as
Figure 1-6. Of course, the reader can specify a different path name and file name entirely.
Figure 1-6 C + + file established successfully
After clicking the OK button, go back to the Visual C + + main window because the path was specified earlier (d:\ computer
\ experiment one) and file name (MyFirstCpp.cpp), so the window's title bar displays
MyFirstC.cpp. You can see that the cursor flashes in the Program edit window, indicating that the program editing window is activated, and you can enter and
Edit the source program. Here, enter the program shown in the 1-7 window. After the check is correct, save the source program in the previous
Specify the file by selecting the file command from the main menu bar and selecting Save from its drop-down menu
(save) command. You can also use the shortcut key Ctrl+s to save the file. If you do not want to store the source program in a previously specified
File, you can choose the Save command instead of the Save as, and the Save As (save As) command pops up in the
Save As dialog box to specify the file path and file name.
Computer -4070105-Zhang San \ "D:\ Computer
-4070105--4070105-Zhang San-4070105-
C + + programming experiment instruction Book
5
Figure 1-7 C + + program input
2. Open an existing program
If you have already edited and saved a C + + source program, and want to open the source program file you need, and
Modify the method by:
(1) In Explorer or My computer, hit the existing C + + program name (such as C1-1.cpp) by path.
(2) Double-click the file name to enter the Visual C + + integration environment and open the file, the program has been displayed in the
window. You can also use Ctrl+o or click the small icon in the toolbar to open the file.
(3) If you still want to save in the original file after the modification, you can select File →save (save) Life
, or use the shortcut key Ctrl+s, or click the small icon in the toolbar to save the file.
3. Ways to establish a new program through existing procedures
If you have edited and saved a C + + source program (instead of using Visual C + + on the computer for the first time),
You can create a new program from an existing program, which is easier than re-entering a new file because it can be
To take advantage of parts of the original program. The method is:
(1) Open any existing source file (example Cl-1.cpp)
(2) Use the base of the file to modify the new file and then →save as (Save As) by selecting file
command to save it with another file name (for example, save as Test.cpp name), thus generating a new file test.cpp.
It is convenient to use this method, but it should be noted that:
(1) When saving a new file, do not use File→save (save) operation, otherwise the original file (Cl-1.cpp)
The content has been modified.
(2) before compiling a new file, you should first select the file →close Workspace (Close workspace) command
Close the original workspace so that the new file is not compiled in the original workspace.
1.2.5 compiling, connecting, and running
1, the compilation of the program
After you edit and save the source file, you need to compile the source file. In the main menu bar, click Build (Edit
Select the Compile MyFirstC.cpp (compile MyFirstC.cpp) command from its drop-down menu. Due to the establishment (or
Save) file, the source file name MyFirstC.cpp has been specified, so the compile command in the Build menu
The source file name MyFirstC.cpp is now being compiled. See Figure 1-8:
C + + programming experiment instruction Book
6
Figure 1-8 Compiling a C + + program
After you select the Compile command, a dialog box appears that contains the "This build command repuires
An active project workspace. Would creat a default project workspace? " (This compilation command is to
To find a valid project workspace. If you agree to create a default project workspace), see Figure 1-9. Click Yes
(yes) button, which indicates that the default project workspace is established by the system and then starts compiling. Or you don't have to choose a menu
and use CTRL+F7 or small icons to complete the compilation.
Figure 1-9 Creating a default workspace hint
At compile time, the compilation system checks the source program for syntax errors, and then the Debug Information window in the lower part of the main window
Port output compiled information, if wrong, will indicate the location and nature of the error, see figure 1-10.
C + + programming experiment instruction Book
7
Figure 1-10 Compilation error message hint
2, the program debugging
The task of the program debugging is to find and correct errors in the program, so that the program can run normally. The compilation system can check out the process
Syntax errors in the sequence. Syntax errors are divided into two categories: one is a fatal error, and the error indicates that if there is such a mistake in the program
Error, it can not be compiled, can not form a target program, not to mention running. The other is a slight error to warning
(warning) indicates that such errors do not affect the generation of target programs and executable programs, but may affect the results of the operation. Because
This should also be corrected as far as possible, so that the program has no error, and no warning.
In the Debug Information window in Figure 1-10, you can see the compiled information, indicating that the source program has an error and a
Warning The 7th line has a fatal error, the type of error is: "}" is missing before ";". Check the process in Figure 1-10
The semicolon was found at the end of line 6th. Students may want to ask: clearly is the 6th line is wrong, how to say in error
Is it the 7th line of fault? This is because C + + allows a statement to be divided into several lines, so there is no point at the end of the 6th line after checking
It is not possible to determine the error of the statement, it is necessary to check the next line until the 7th line of "}" is found;
To determine an error. Therefore, in the 7th line error. When you compile the error message Checker, you should check the upstream and downstream of the procedure. This
The outside compilation information indicates that line 7th also has a warning, indicating that the main function needs to have a return value, the program is missing a
A "return 0;" statement.
When you make a change, double-click the 1th line of an error message in the Debug Information window and the cursor is automatically moved to the program window
The line with the error in the program, and a thick arrow pointing to the row. See Figure 1-11.
C + + programming experiment instruction Book
8
Figure 1-11 Finding the wrong location
Add a semicolon at the end of line 6th. Add a new line return 0; 1-12:
Figure 1-12 Error Modification
Then select the compile MyFirstC.cpp command to recompile, at which point the compile message says: "0 error (s), 0
Warning (s) ", which does not cause errors (error) and warning errors (warning), compiles successfully, resulting in a
Myfirstc.obj file. See the Lower Debug Information window in Figure 1-13.
C + + programming experiment instruction Book
9
Figure 1-13 Syntax compilation by
3, the connection of the program
After the target program is obtained, the program can be connected. At this point you should choose Build (build) →build
MyFirstCpp.exe (build) command, see Figure 1-14. Indicates that a connection is required and an executable file is created

Figure 1-14 Connecting to create an executable file
After the connection is performed, the Debug Output window displays the connection information, stating that no error was found and a possible
Execute the file. See Figure 1-15.
MyFirstCpp.exe
MyFirstCpp.exe
MyFirstCpp.exe
C + + programming experiment instruction Book
10
Figure 1-15 Executable Setup succeeded
The above describes the program is compiled and connected separately, you can also choose the build→ command (or press F7 key) once
Complete the compilation and connection. For beginners, it is recommended that the program be compiled and connected in step, which is helpful to
Understanding of the process of translation, interpretation and connection. After accumulating some experience in the future, we can complete the compilation and connection in one step.
4, the implementation of the program
Once you have the executable file, you can execute it directly. Select Build
→ (execute) command. See Figure 1-16.
Figure 1-16 Running the program executable file
After the command is selected, execution begins. You can also choose a dish without
Single command, and Ctrl+f5 to implement the program. After the program executes, the screen switches to the output Results window, which displays
The result of the operation, see figure 1-17.
MyFirstCpp.exe MyFirstCpp.exe
! Execute MyFirstCpp.exe MyFirstCpp.exe
"! Execute MyFirstCpp.exe "MyFirstCpp.exe
C + + programming experiment instruction Book
11
Figure 1-17 Program Run results
As you can see, the 1th line in the Output window is the output of the program:
This is a C + + program.
The 2nd line of "Press any key to continue" is not the output specified by the program, but Visual C + + after the output is shipped
A line of information that is automatically added by the visual v++6.0 system after the result of the row informs the user to "press any key to continue." When
Once you press any button, the Output window disappears and you can go back to the main Visual C + + window and you will be able to continue to repair the source program.
To supplement or perform other work.
If you have completed the operation of a program and no longer handle it, you should select File →close
The Workspace (Close window) command to end the operation of the program.
1.2.6 ways to build and run programs that contain multiple files
The simplest scenario described above is that a program contains only one source program file. If a program contains multiple
Source program file, you need to create a project file that contains multiple files in the project file (package
including source files and header files). Project files are placed in the project workspace (Workspace) and managed in the project workspace
Project Workspace, and a project workspace can contain more than one project. At compile time,
Compile each file separately, then connect the files in the project file as a whole, then the system
Resource connection, generate an executable file, and finally execute the file.
There are two ways to do this: one is to create a Project workspace and project file for the user, and the other is to
The project workspace is created automatically by the system by creating a project file without creating a project workspace.
1, the user set up the project workspace and project documents
(1), first use the methods described above to edit the same program in each source program files, and stored in their own
The specified directory, for example, has a program that includes Filel.cpp and file2.cpp two source files, and has saved them in the "d:\
Computer -4070105-Zhang San \ Experiment One "sub-directory.
(2), establish a project workspace. Select File in the main Visual C + + window shown in 1-4
→new (New) command, select the upper Selection property page in the new dialog box that pops up workspace name
(workspace name), which indicates that you want to create a new project workspace. In the dialog box, right workspace name (work
Type the name of the workspace you specified (such as WSL) in the text box, as shown in Figure 1-18.
C + + programming experiment instruction Book
12
Figure 1-18 Creating a workspace
Then click the OK button in the lower right. This returns the Visual C + + main window, as shown in Figure 1-19.
Figure 1-19 an empty workspace
You can see that "Workspace ws1:0 project (s)" is displayed in the workspace window on the left, indicating that the current work
The zone name is WSL, which has 0 projects (no project files are placed in it).
(3), the establishment of project documents. Select the file →new (new) command, in the popup new (new)
dialog box, select the upper Selection property page Projects (Project, Chinese Visual C + + translates it as "project"), indicating
To create a project file. See Figure 1-20.
C + + programming experiment instruction Book
13
Figure 1-20 Creating a new project
In the left-hand list in the dialog box, select the "Win32 Console Application" item, and at the right location (bit
text box, enter the location of the project file (that is, the file path), in the project name (Chinese
text box, enter the name of the project file you specified, assuming input projectl. At the right of the window, select Add to Current
Workspace (Add to existing Workspace) radio button to indicate that the newly created project file is placed on the current job you just established
In the area. Then click the OK button. A dialog box with 1-21 appears, in which you select
An empty Project radio button, which indicates that the newly created is a blank item.
Figure 1-21 Project Type selection
Click the Finish button. The system pops up a new project information (New project information) dialog
Box (Figure 1-22), showing information about the project that was just created.
Figure 1-22 Information for a new project
Below it you can see the location of the project file (file path). Click the OK button when you are sure. At this time
Back to the main Visual C + + window, you can see that the left window shows "Workspace ' WSL ': 1project (s)"
The next behavior, "Project1 files", indicates that the project file Project1 added to the project workspace WSL, as shown in Figure 1-23.
C + + programming experiment instruction Book
14
Figure 1-23 Adding a new project to the workspace
(4), the source program files are placed in the project file. In the Visual C + + main window, select project
(engineering) →add to project (added to projects, displayed as "Add Project" in the Chinese language) → "Files ..."
command, see figure 1-24.
Figure 1-24 file added to project
After you select the "Files ..." command, the Insert Files into Project dialog box appears on the screen. In the upper list box
Find the subdirectory of source file File1.cpp and file2.cpp by Path, and select File1.cpp and file2.cpp, see figure
1-25.
C + + programming experiment instruction Book
15
Figure 1-25 Adding a file selection
Click the OK button to add the two files to the project file Project1.
(5), compile and connect project files. Since the file1.cpp and file2.cpp two files have been added to the project file
Project1, so only the project file Project1 must be compiled and connected uniformly. The method is: in VISUA1 C + +
Select the build (Compile) →build Project1.exe (build project1.exe) command in the main window.
After selecting the build Project1.exe command, the system compiles and connects the entire project file, in the lower part of the window
The compilation and connection information is displayed. If the program is wrong, an error message is displayed, and if there is no error, the executable file is generated
Project1.exe. See Figure 1-26.
Figure 1-26 Compilation and connection of the project
Executes the executable file. Select the build (Compile) →execute Project1.exe (execute project1.exe) command,
Can.
2, the user only set up project files
The method described above is to establish the project workspace first, and then build the project file, the steps are more. can take a simplified
method, that is, the user only establishes the project file, but does not establish a Project workspace, the system automatically establishes the project workspace.
In this method, you retain steps (1), (4), (5), (6), as described in the previous section, and Cancel step (2) and modify
Step (3). The specific steps are as follows:
(1) Edit each source program file in the same program separately. Step (1) in the previous section.
(2) Create a project file (you do not have to create a project workspace first).
In the Visual C + + main window, select the file →new (new) command in the popup new
dialog box, select the Upper tab Projects (Project), which indicates that you want to create a project file. See Figure 1-27. In the dialog
box, select the "Win32 Console Application" item in the left-hand list in Project name (Chinese Visual C + +
C + + programming experiment instruction Book
16
Enter the project file name (Project1) that you specified in the text box that appears in the project. You can see: In the right-hand
The Create new workspace is selected by default in the radio button in the room, because the user does not specify a work
area, the system will automatically open up a new work area.
Figure 1-27 Creating a new Win32 Console application project
Click the OK button and the Win32 Console application-step 1 of 1 dialog box appears, select the right
An empty Project radio button in the department, and when you click the Finish button, the "New Project Information" appears
(New project information) message box. The path to the project file can be seen from the bottom of it. Click the OK button,
Click the File View button below the left window of the main Visual C + + window that pops up, and the window displays
"Workspace ' Project1 ': 1 Project (s)", see Figure 1-28. Indicates that the system has automatically established a workspace because
When the user does not specify a workspace name, the system Project1 the project file name as the workspace name.
Figure 1-28 Establishing a new project success
Add content to this project file. The steps are identical to step (4) in method one.
Compile and connect the project file. The steps are identical to step (4) in method one.
Executes the executable file. The steps are identical to step (6) in method one.
Obviously, this method is a little simpler than the previous method. In introducing a single-file program, in order to minimize formalities, this
Does not create a workspace, nor does it create a project file, but instead builds the source file directly, in fact, each process is compiled
Requires a workspace, and if the user is not specified, the workspace is automatically created and given a default name (this
File name as the workspace name
C + + programming experiment instruction Book
17
1.3 Visual C + + 6.0 Engineering related documents
Ordinal filename Description
1 *.dsw VC workspace file
2 *.DSP (Developer Studio Project) project file, text format. If you are unfamiliar, do not modify it manually.
3 *.NCB
No compiled browse files. When the auto-complete function is faulty, you can delete this file, the compiler
Automatically generated after the process.
4 *.opt Project about the development environment of the parameter files, such as the VC tool strip location information.
5 *.h/C + + program header file, open with text editor
6 *.cpp C + + source program file, open with text editor
7 *.exe Program executable file
8 *.PLG
Compile the information file. Compile-time error and warning information file (actually an HTML
Document), is generally of little use. In Tools? Options? The options inside the build control this
The creation of a file.
9 *.pch (pre-compiled file) is precompiled. Can speed up compilation, but the file is very large
Ten *.pdb
(program database) records some data and debugging information about programs, while debugging
Waiting for useful
11 *. The target file of the obj source program file, which is generated after compilation
12 *. Ilk the connection file of the source program file, which is generated after connection
Note
After establishing a project under VC, the 1~4 type of file will be generated automatically. Join *.h,
*.cpp files that produce 7~12 types after they are compiled. where 8~12 types of files are located in the project
Directory under the Debug directory.
1-4 types of files are project-related files, generally do not delete, because after deletion,
You must manually re-establish the project (1.2.6 details how to build a project). For large
Project, the consequences of deleting these files are serious.
5~6 types of files There is nothing to say, not only can not delete these files, but also to back up
These files. The loss of these documents is catastrophic.
All files of the 7~12 type can be deleted because the connection is run again by compiling the program
, these files are automatically generated again.
1.4 Visual C + + 6.0 error message
1.4.1 Type of program error
1, Critical Error (fatal error)
Rarely occurs, usually an internal compiler error. Causes the compilation to stop immediately.
2. Syntax errors (Error)
A syntax error is a statement in the source program that does not conform to the syntax of a C + + language, such as an int, including
Number mismatch, and so on. These errors are not corrected and cannot be compiled. 1-29
C + + programming experiment instruction Book
18
Figure 1-29 Syntax errors
3. Warning Error (warning)
For some minor grammatical problems that do not affect the program's operation (for example, variables are defined but not used at all times),
A warning message is issued at compile time, although the program can compile, connect, and run, but the error of the warning class often brings the program
Illegal operation, operation error, and so on. Therefore, try to correct the warning error. 1-30
Figure 1-30 Warning Error
4. Connection errors (link error)
There is no problem with the program syntax, but an error occurred while connecting. This type of problem is often the result of program-dependent functions, library
caused by a mismatch. 1-31
Figure 1-31 Connection Errors
5. Logic Error
A logic error is a program that does not have a syntax error and can run correctly, but the result is not correct. This type of error is often a design algorithm
Error, the computer cannot be checked out. Logic errors are one of the hardest mistakes to correct, and the cause of the errors can often be
Can be very inconspicuous, for example, a variable is not initialized, etc., so it is often necessary to correct such errors to devote a lot of effort.
6. Operation Error
Sometimes the program has no syntax or logic errors, but the program is not functioning properly. In most cases, the input data and
The data mismatch required by the program may also be a system support issue.
In C + +, syntax, connection errors are relatively easy to correct. And logic error is the most covert error, more difficult to
Correct. Operational errors are mainly in the program robustness, compatibility may be problematic, can be improved by the program to adapt
Ability to correct. The easiest thing for a program developer to ignore is a warning error, because a warning error does not necessarily affect
program, but it is this uncertainty that is passed on to the execution of the program, you do not know when the program will ask
Questions, maybe there will never be a problem, maybe the problem appears immediately, maybe the problem n years later, who knows. So, be sure
Pay attention to warning errors.
Table 1-1 is a prefix description for C + + errors, which can be prefixed to the error level when an error is encountered.
Error level Error prefix error number range example
Fatal error C1 001~999
Compilation Error C2 001~999
C2065:the specified identifier is not
Declared. (identifier, such as a variable, not stated).
1-29,coutt not stated.
C + + programming experiment instruction Book
19
Warning Error C4 001~999
C4101: ' main ': unreferenced local variable
(local variable declarations have never been used). 1-30,
The I variable has never been used.
Link Error lnk 1000~6026
lnk2001:unresolved external symbol
"Symbol". (Connect the specified function or library exception). Such as
Figure 1-31, the program cannot find the main function.
1.4.2 Query for error messages
Correct errors can be corrected only by knowing the cause of the error in detail. Microsoft's MSDN Library
(Microsoft Development Help document Library, 1-32) provides the ability to query for error messages. For those who don't understand
Errors, students should learn to use the MSDN Help document library for error message queries to get an explanation of the error message
and possible methods of correction. Here's how:
(1) Select the Index property page of the MSDN Library (1-32), and in the "type keywords to find:"
Enter the error number (which consists of the error prefix and number), as shown in c2065,1-33 input.
Figure 1-32 MSDN Library
(2), in the Index list box, double-click to select a matching error, the type of error, possible causes, correction methods and other information
is displayed in the right part of the window. 1-33 is shown below:
C + + programming experiment instruction Book
20
Figure 1-33 Finding the error message in the MSDN Library
1.5 Visual C + + 6.0 Debugging Tools use
In the process of developing the program, you need to constantly debug your own programs, or check for errors, or test the stability of the program,
This needs to be done with debugging tools. Selecting a good debugging tool and being able to use it skillfully will make the program open
Significantly improve the efficiency of program development.
There are many debugging tools available, and debugging tools that are integrated in Visual C + + have quite powerful features.
1.5.1 setting debug properties for a project
When you build a project (project) in Visual C + +, Visual C + + automatically creates a debug directory,
In this directory, the files that are generated when the development process is debugged and the resulting executables (Files of different types
EXPLANATION See section 1.3 of the Visual C + + 6.0 engineering-related documentation), you might want to make the resulting executable file called the Debug
Version. In the debug version, debug information is included in Microsoft format without any code optimizations.
In general, debug mode is used during the development of the program.
After application development is complete, the program needs to be compiled to form another version of the executable when it is released.
Called release version, the binary code of the executable is optimized in release release, which does not contain
Any debugging information. To create a release version of the executable file, you can select the setting in the menu project
The Project Settings dialog box appears above the screen, as shown in 1-34. In the settings for drop-down list
Select release and press the OK button to exit.
If you are debugging a program in Visual C + +, the project should be set to the debug version. General Visual C + + establishes a
Project (project), the debug version is used by default.
C + + programming experiment instruction Book
21st
Figure 1-34 Setting debug properties for a project
1.5.2 Learning to set breakpoints for single-step debugging
Usually, an application is run continuously, but in the process of program debugging, it is often necessary to run the program
A stage of the process to observe the state of the application, so the program must stop at a certain point. In Visual C + +
, you can set breakpoints to achieve this. After a breakpoint is set, when the program runs to the breakpoint where it was established
Stop running, you can use a variety of tools to observe the state of the program, you can also set various conditions for the program to
Operation, so that the flow of the program can be further observed.
In Visual C + +, you can set multiple types of breakpoints that work differently and can
They fall into three categories: positional breakpoints, logical breakpoints, and breakpoints related to Windows messages. Below we only refer to
Simple location Breakpoint Setting method, other advanced breakpoint settings can be learned in the future.
1. Set the location breakpoint
Position breakpoint, which is the simplest type of breakpoint, is the easiest to set, just move the cursor to the
The location of the breakpoint (this line, of course, must contain a valid statement), and then press Add/remove on the toolbar breakpoint
button or press the shortcut key f9;1-35:
Figure 1-35 Setting the breakpoint button
You will see a red dot on the left side of the line indicating that there is a
Breakpoint, shown in 1-36. If you want to cancel the breakpoint, move the cursor to the line of code that sets the breakpoint, and press F9.
C + + programming experiment instruction Book
22
Figure 1-36 Setting a breakpoint
2, start debugging
You can start the debugger by pressing F5 or by selecting Start Debug and then go on the Build menu. Program
will run until the code that requires user input or has a breakpoint. A small yellow arrow at 1-37 indicates that the program is running
Stop here and wait for the next debug command.
Figure 1-37 Start debugging
3. View variable values
There are several ways to view variable values, which you can choose the way you like.
(1) You can move the mouse to the program variable name, the system will automatically show you the value of the variable;
C + + programming experiment instruction Book
23
(2) complex variables (such as objects) can be viewed through QuickWatch by positioning the cursor to the value you want to view
Variable, you can see the value of the variable by pressing the right mouse button and selecting the QuickWatch menu.
(3) After starting the debugger, there will be two output windows at the bottom of the screen, one is watch and the other is variable.
The Watch window displays variable names and variable values, which you can add to the Watch window with the name of the variable you want to observe, or
Select the variable name directly from the source code and drag it to the Watch window. Variable window shows that the program is currently running
The value of the variable involved in the context. 1-38:
Figure 1-38 Viewing variable data
4. Control program execution
You can control the execution of a single step (F10), trace to a function inside (F11), run from a function
(SHIFT+F11), run to the cursor location (CTRL+F10), to easily debug your program. These commands are used in a
A local scope to debug the program in detail. You can also set a breakpoint (F9) and then run it directly (go or F5)
To the control program to run directly to the breakpoint location. If you set multiple breakpoints, the program will be at the first breakpoint encountered
Stop it. To continue running from a breakpoint, you can use the various commands described above (F5,F10,F11,SHIFT+F11,
CTRL+F10).
Figure 1-39 Debug Control toolbar
5. End Commissioning
To end debugging, you can press SHIFT+F5 or select Menu Debug| Stop Debugging. When you finish debugging, the
A debug window will automatically close and the Debug menu will automatically revert to the Build menu.

Fundamentals of C + + development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.