Using and debugging C++
In this tutorial, C++ is used for creating a C++ class. The class is then used for creating independent objects, and the application is built and debugged.
During execution in C-SPY, various features that are specifically useful for debugging certain C++ constructs are demonstrated.
Note that the screenshots in this tutorial do not always correspond exactly to the counterparts in your product.
This tutorial assumes that you are familiar with basic use of IAR Embedded Workbench; see the tutorial Getting Started using IAR Embedded Workbench.
Tutorial overview
Files used in this tutorial
The project in this tutorial uses these files:
*
It defines a class Fibonacci that can be used for extracting a series of Fibonacci numbers. The result is printed on stdout.
*
Contains required definitions and declarations.
*
Creates fib objects from the class Fibonacci and manipulates them before it extracts three sequences of Fibonacci numbers using the Fibonacci class.
Opening the tutorial project
1
2
Click the Product explorer symbol.
3
Under the Language support heading, click Open the tutorial workspace. Choose a destination for your project in the browse dialog box that appears. We recommend that you create a specific directory (with a subdirectory) where you store all your tutorial files, if you have not done that already. In this tutorial we call the directory MyTutorials and the subdirectory LanguageSupport.
After you have chosen a destination directory, the LanguageSupport workspace is opened in the IDE.
4
In the Workspace window, select the UsingAndDebuggingCpp project, right-click and choose Set as Active.
Debugging various C++ constructs
In this example procedure, you will work through these steps:
*
*
*
*
*
*
Setting up the project
The UsingAndDebuggingCpp project is preconfigured with appropriate option settings for this tutorial. However, this procedure guides you through the options required for this tutorial.
1
In the Workspace window, select UsingAndDebuggingCpp - Debug, right-click, and choose Options from the context menu.
2
 
C++ dialect (Only for some products)
Extended Embedded C++ (only for some products)
To read more about DLIB and support for C++, see the compiler documentation.
C++ might require some extra heap; for information about any settings required for heap usage, see the header of the UsingClasses.cpp file.
3
4
Choose Project>Make to compile and link your application.
Monitoring constructors
Static objects are created before main. In C++ there are constructors which are executed when the object is created. Thus, constructors for static objects are executed before main.
1
In the file UsingClasses.cpp, notice the static variable fibStat:
2
To be able to monitor the construction, set a breakpoint on cout << "A Fibonacci object that starts at Fibonacci number" which you find in the file FibonacciByClass.h.
3
Stopping execution before main means that the option setting Run to main is disabled. Therefore, you should also set a breakpoint close to main, for example on Fibonacci fib1; in the file UsingClasses.cpp.
4
Choose Project>Download and Debug to start C-SPY.
5
Click on the red dot to remove the breakpoint, which is no longer needed.
6
Click the Go button () to resume the execution.
7
8
Click Step Into () to step into fib1 and fib2 to see the various constructors.
Monitoring an unintuitive program flow
In C++ it is not always intuitive what the next function call will be. In this example procedure you will use various features that assist you tracking the program flow.
1
In practice, this statement consists of three various function calls:
*
*
*
The Fibonacci destructor.
2
Choose View>Call Stack. This window shows the chain of function calls. At the top line you can see that Fibonacci:Fibonacci(uint_fast8_t) is the first of the three functions to be called.
3
There are various step commands to choose between to continue executing your application, for example Step Into and Step Over. However, because you in this case want to track the program flow on function call level, the best command to use is Step Over because this will take you directly to the next function call (in contrast to Step Into which will step into the function).
Click Step Over () and notice that the next call is Fibonacci::operator=(Fibonacci const &).
4
Click Step Over again and notice that the next call now is the final of the three calls, in other words Fibonacci::~Fibonacci().
Backtracing function calls and their parameters
It can be interesting to backtrace the function calls a few levels down and examine the value of the parameter for each function call. In this example procedure you will locate the function nth and monitor parts of its call chain.
1
A couple of rows further down (still in UsingClasses.cpp), select fib1.next(), right-click, and choose Toggle Breakpoint (Code) from the context menu.
Note:  
In this case, when you want to set a breakpoint on a specific function call, you cannot set a breakpoint by clicking in the left margin (as you might usually do when you toggle a breakpoint) because that would set a breakpoint on the first statement of that line.
2
Click Go (); the execution stops at the breakpoint.
3
Click Step Into (); you are now in the function next in the file FibonacciByClass.cpp.
4
To locate the function nth, click Go to function () in the upper right corner of the editor window and choose nth.
5
In the function nth, set a breakpoint on value = nth(n-1u) + nth(n-2u);.
6
Click Go () repeatedly to execute the application until the breakpoint is triggered.
7
In the Call Stack window, right-click and choose Show Arguments from the context menu. You can now see an instance of the function nth displayed on the call stack.
Click Go () a few times. Because the Call Stack window displays the values of the function parameters, you can see the different values of n in the different function instances.
Monitoring STL containers
In C-SPY, you can view the contents of STL containers (like for example msFib, which is a vector) in the same way as arrays.
1
Choose View>Watch>Watch 1.
2
In the <click to add> cell, type msFib.
Expand msFib to view its content and expand Raw to view the implementation.
Printing the Fibonacci numbers
1
Choose View>Terminal I/O to open the Terminal I/O window.
2
3