CSC 306 Introduction to Programming with C++

Expanding your Programming Environment:
Dev C++ Debugger and The Linux Environment

Chapter --- None


Objectives

Important Definitions


C++ Standards

The C language was developed on and for the Unix operating system. Bjarne Stroustrup at AT&T Bell Labs had used a language called Simula in his research, and in 1979, he decided to add some Simula features to the C language, most notably classes, and thus "C with Classes'' was born. C++ was based upon C, and there are two ways to increment a value in a variable var in either language:

Regular WayProgramming Shorthand
    var = var + 1;

    var++;
Because C++ was considered a step beyond C, it was called C++ (Programmers have an odd sense of humor). After Stroustrup published The C++ Programming Language in 1985, the language became very popular, and multiple implementations of both C and C++ appeared. Because each version had its own special features, this caused problems for users who moved code (called porting) from one implementation to another. Programs that worked using one version failed to work in another.

To address this problem, the very first standards for the C programming language were created that officially and permanently separated C from the UNIX operating system by:

  1. American National Standards Institute (ANSI) 1989
  2. International Organization for Standardization (ISO) 1990
High-level functions provided by C were standardized in the C standard library and is still a part of the C++ libraries today. UNIX operating system functions that are not unique to C and C++ (i.e. can be used equally by other languages) were removed from the C language. This division of the C and C++ languages from any specific operating system or developing environments enabled these to become truly universal; a program created using one environment worked in another.
Odd Note: A similar standards problem currently occurs with JavaScript between Microsoft and Sun Microsystems.

Because of this, it is important to become comfortable with multiple C++ environments so that you can do testing of your code in a variety of settings and be assured that what worked using one compiler works for another.

Windows: Using the Dev C++ Debugger

All decent implementations of C++ developing environements include debugging tools, which can be helpful in correcting coding errors, and Dev C++ is no exception. Other good compilers also have debuggers but they will all differ somewhat in the way they operate. In Dev C++, you need to go under the "Debug" Menu to "Debug" (or press Cntl-F8), which will display the debug options at the bottom of the screen. The project must be compiled if debugging is to work---i.e. if the program has syntax or compile errors and will not run, then solve those problems first! You can then control program execution using the following commands, where "shortcut" refers to the keys you press to perform the action:
Action Shortcut What It Does
Next Step F7 - Steps through statements in the source code one at a time. If the statement is a function call, it will execute the function without going into it.
Step Into Shift-F7 - Steps through each statement in the source, and if it a function call, it will start executing the function statements one at a time.
Continue Ctrl-F7 - Execute code from the current statement until one of the following occurs:
  1. the debugger reaches a breakpoint
  2. the program finishes
  3. an exception
Run to Cursor Shift-F4 - Execute the code up to the line in the source code that contains the cursor. This command is equivalent to setting a temporary breakpoint at cursor.
Debug F8 - Restarts the execution from the first line of the application.
Stop Execution Cntl-Alt-F2 - Terminates the debugging session and returns to a normal editing session.

Breakpoints and Watches

A breakpoint is an instruction in the program where the debugger will stop executing so that the programmer can view the contents of variables to make sure they have their expected values. You can set a breakpoint by clicking on the margin to the left of the line of code where you would like the debugger to stop, or by placing the cursor on that line and having the debugger "Run to Cursor". A watch is the name of the identifier that the programmer wants to see what their value is. Clicking on "Add Watch" will add a variable to the watch list and "Remove Watch" will take the one you indicated off.


Assignment Details

The goal of this assignment is to expand your exposure to different programming environments. You will learn to use both the Dev C++ debugger in Windows and the compiler in Linux.

This assignment is to be done individually, and you will submit your answers to the following questions in a Microsoft Word document called YourLastName_306A09.doc in the dropbox.

Part A: Use the Dev C++ Debugger

In this part of the assignment, the goal is to try out all of the primary features of the Dev C++ debugger so that you are more aware of the power available to you for future programming. Download the program DebugExample.cpp onto your source code folder. It contains a single error, which you may be able to locate on your own, but remember that the point of this part of the assignment is for you to explore the capabilities of the debugger. Try the following debugger commands on this program:
  1. step into: to step into the get_grade() function, and use it again to step into the cout call.
  2. step out to get back out of the library or function call.
Note: You can see the value of the variable inputg after you enter a value.
After trying each of the debugging commands, write the following in your Word document:
  1. a couple of sentences describing what kind of programming error you think this debugging command might be useful for tracking down.

  2. describe what kind of programming error you think seeing the value of a variable might be useful for tracking down.

  3. describe whether or not the debugger helped you to find the programming error. If you found the programming error, describe what it was.

Part B: Use a Linux shell for Compilation:

Download the communications program, PuTTY.exe (372 KB). This program will allow us to login remotely to another system using the ssh (secure shell) protocol that provides secure encrypted communication between two host computers.

Every student has been given an account on cs.berea.edu, which is running Linux. Some of you may already have your own distribution of Linux running on your laptop, but because there is no guarantee how well what you have complies with with ISO standards please use cs.berea.edu.

Your account name on cs.berea.edu is modeled after Berea's email account names; your last name followed by the first letter of your first name (thus John Doe's login name is "doej"). Your initial password on your cs.berea.edu account is "4" followed by your login name. Open PuTTY and login to cs.berea.edu.

The interactive screen that opens with a command line in Linux is called a shell. Using the passwd command, please change your password on cs.berea.edu to something else that is easy for your to remember but difficult for others to guess.
WARNING: Do NOT share your password with ANYONE!

As is the case with Dev C++, the first thing is to find the text editor in which to write the source code. There are several options available, but the most common and ancient one is called vi (short for VIsual editor) which is included in every distribution of LINUX and UNIX. Most systems also have emacs and pico. The emacs editor is more intuitive than vi for some people, and the pico editor is certainly easier to learn to use than vi but not as powerful nor universally available. For common commands on emacs, try this website. Once the source code is complete, you can try to compile the program using the compiler for LINUX called g++.

To do all this, we will start with the first program we created using Dev C++, "hello.cpp".

  1. Type vi hello.cpp . This starts the vi program that will save the text you edit into a file named "hello.cpp".

  2. When the window comes up, type i to get into insert mode, now you may type and move around the window using the arrow keys. When you are comfortable moving around, type in the following:
    // File Name: hello.cpp
    #include <iostream>
    using namespace std;
    
    int main(){
        cout << "hello world" << endl;
    
        return(0);
    }
    
    Hint You can also copy the code from the screen and paste the code into vi by selecting the code, pressing Crtl-C for copy and right-clicking your mouse on the screen.

  3. Type ESC to get out of insert mode and type :wq to write to the file and quit from vi (w for write, and q for quit). You could have just typed :w to write the file and then a i to continue editing. To quit the vi editor without saving use :q!.

    In your Word file, describe your initial reaction to the vi editor.

  4. To compile and link this code, type g++ hello.cpp -o hello. The "-o" is called a flag option that tells the compiler to call the compiled executable file the name after the flag --- in this case, the program is called "hello" rather than to the default file "a.out". Note that unlike in Windows, files do not need an extension, such as the ".doc" for a Word document.

  5. To run this program type ./hello. The "./" here tells the Linux operating system to look in the current folder to find the "hello" program.

    In your Word file, describe whether or not you got your "Hello World" program to run.

  6. Now you are ready to compile and run a more complicated program. However, unlike on your laptop where you are the only one running anything, in cs.berea.edu you are programming in what is called a multiuser environment. This means that other people may be logged in at the same time you are and running programs, so you need to follow certain rules so other users do not become angry with you for various types of unfriendly behavior.

    General RULES:

    1. Never, never, never, never exit LINUX by clicking on the "X" at the on the top right icon of the shell window. Although this seems like the same as logging off (closing your connection and freeing the computer resources so that others can use them), this will leave your user process running, hogging up resources. ALWAYS use the command exit or logout at the command line to close you connection.
      WARNING: LINUX is case-sensitive, so please make sure all letters are lowercase.

    2. Infinite loops should be terminated by typing Ctrl-C. However, you may need to be patient because you have to wait for your command to get through the buffer---just because nothing is happening does not mean that you should cancel what you are doing and log out.
      When you think about logging out, remember again to never, never, never, never exit Linux by closing the "X" on the ssh Window. ALWAYS use the command exit or logout at the command line to close you connection.

    In your Word file, describe these two Linux rules in your own words.

  7. Try to compile the problematic program DebugExample.cpp in the Linux environment. Use vi to copy and paste the code into a file with that name, and then compile and link using g++ DebugExample.cpp -o DebugExample.
    WARNING: Remember the Linux rules!! You must exit ANY and ALL infinite loops using Ctrl-C!!!
    The compiler may take some time, but it will eventually create the executable file "DebugExample", which you can run by typing ./DebugExample.

    In your Word file, describe what happens when you run this code in Linux.

  8. When you have seen this program run in Linux and you have terminated the execution in a good way, type exit to exit the Linux system.

    In your Word file, describe what you think about working in Linux at the command line.

For some final helpful hints, you can get some information from LINUX commands using with man to open the MANual, info, and help. They should be tried in this order because they all give different information, and the man page is usually the most helpful. To scan through the man page on g++, type man g++, noting the many flags that are available for various standards compliances and for debugging tools. (In the man pager, you can use the space bar and the b-key to move back and forward in the document)

When you are finished writing and testing your assignment, drop your Word document, YourLastName_306A9.doc, into the CSC306_A09 dropbox on the Academic server.


Back to Introduction to Computer Programming with C++ Homepage