Test your C++ Installation |
Simple Test of Compilation
-
To check that you're good to go, download this simple program and save it into your data structures course directory:
temperature.cpp
-
Open a terminal and change directory to navigate to your data structures directory:
cd INSERT_DATA_STRUCTURES_DIRECTORY_NAME
-
Compile the temperature program by typing:
g++ -Wall -g temperature.cpp -o temperature.out
(Alternatively, use clang++ instead of g++.) This should creates an executable named temperature.out. Type ls again to confirm the executable appeared!
-
Now run that executable:
./temperature.out
And you should be able to interact with this program at the keyboard.
Confirm you're in the right location by listing the directory contents typing:
ls
You should see the file temperature.cpp and maybe some other stuff.
Simple Test of Terminating an Infinite Loop
-
Download and save this file to your data structures directory:
infinite_loop.cpp
-
Compile and run this program. It should start printing dots and not stop.
-
Now let's confirm that your computer is spending alot of CPU resources on this infinite loop. We'll view the list of all programs running on your computer sorted by CPU usage.
-
On Mac or Linux or WSL, in another terminal type:
top -o %CPU
You should see the program name at or near the top of the list. And it should be using a large amount of CPU (depending on the number of processors on your machine).
-
On Windows:
- Search for and launch "Task Manager"
- Click "More Details"
- Click on the "CPU" column
You should see the program name at or near the top of the list. And it should be using a large amount of CPU (depending on the number of processors on your machine).
-
-
Confirm that you can terminate the runaway program by pressing Ctrl-c.
Simple Test of Memory Debugger Installation & Usage
-
Download and save this short (intentionally buggy) program:
memory_bugs.cpp
-
Follow the instructions for your system to compile and run this program for Memory Debugging.
-
Now run the program under the memory debugger and locate the report generated by the memory debugger. (It may be printed directly to the terminal and/or saved to a file.)
NOTE: The memory debugger report contains lots of data which can be intimidating the first time you see it. This tiny program has 4 different errors! We'll discuss the code, and the errors, and how to understand this report a few weeks into the term.
Simple Test of the Traditional Debugger
Launch the temperature program (or another executable) inside of the gdb debugger:
gdb ./temperature.out
At the gdb prompt, start the program:
run
If the program is still running, press Ctrl-C. To see the stack backtrace of the program (listing the functions you are in the middle of), type:
bt
Finally, close gdb:
quit
(You will have to press 'y' if the program is still running.)
If you're using LLVM's clang++ instead of g++, you'll also use LLVM's lldb instead of gdb. The above commands are the same (just substitute lldb for gdb), and many of the other commands are similar:
A handy table of the different gdb/lldb commands
NOTE: The traditional debugger has many powerful features that you should learn over the term. Follow online tutorials and ask for help in lab and office hours to most effectively debug your programming assignments.