Using C++
Creating a C++ application
Compiling and linking the C++ application
Setting a breakpoint and executing to it
Printing the Fibonacci numbers
In this tutorial, C++ is used to create a C++ class. The class is then used for creating two independent objects, and the application is built and debugged. We also show an example of how to set a conditional breakpoint.
This tutorial assumes that you are familiar with the basics of the IAR Embedded Workbench® IDE described in the previous tutorials.
Note that, depending on what IAR Systems product package you have installed, support for C++ might or might not be included. This tutorial assumes that the product supports C++.
Creating a C++ application
This tutorial demonstrates how to use the C++ features. The tutorial consists of two files:
*
Fibonacci.h and Fibonacci.cpp define a class Fibonacci that can be used to extract a series of Fibonacci numbers
*
CppTutor.cpp creates two objects, fib1 and fib2, from the class Fibonacci and extracts two sequences of Fibonacci numbers using the Fibonacci class.
To demonstrate that the two objects are independent of each other, the numbers are extracted at different speeds. A number is extracted from fib1 each turn in the loop while a number is extracted from fib2 only every second turn.
The object fib1 is created using the default constructor while the definition of fib2 uses the constructor that takes an integer as its argument.
Compiling and linking the C++ application
1
In the workspace tutorials used in the previous chapters, create a new project, project3.
2
Add the files Fibonacci.cpp and CppTutor.cpp to project3.
3
Choose Project>Options and make sure the default factory settings are used.
Note: For this application, the default stack size might be too small. For further information about the required settings, see the CppTutor.cpp file.
In addition to the default settings, you must switch to the C++ programming language, which is supported by the IAR DLIB Library. To use a DLIB library, choose the General Options category and click the Library Configuration tab. From the Library drop-down list, choose Normal DLIB (for some product packages DLIB).
To switch to the C++ programming language, choose the C/C++ Compiler category and click the Language tab. Choose Language>C++, C++ dialect>Embedded C++ and press Ok.
To read more about the IAR DLIB Library and the C++ support, see the compiler documentation.
4
Choose Project>Make to compile and link your application.
Alternatively, click the Make button on the toolbar. The Make command compiles and links those files that have been modified.
5
Choose Project>Debug to start C-SPY.
Setting a breakpoint and executing to it
1
2
To see how the object is constructed, set a breakpoint on the C++ object fib1 on this line:
Fibonacci fib1;
3
Choose Debug>Go, or click the Go button on the toolbar.
The cursor should now be placed at the breakpoint.
4
To step into the constructor, choose Debug>Step Into or click the Step Into button in the toolbar. Then click Step Out again.
5
Step Over until the line:
cout << fib1.next();
Step Into until you are in the function next in the file Fibonacci.cpp.
6
Use the Go to function button in the lower left corner of the editor window and double-click the function name nth to find and go to the function. Set a breakpoint on the function call nth(n-1)at the line
value = nth(n-1) + nth(n-2);
7
It can be interesting to backtrace the function calls a few levels down and to examine the value of the parameter for each function call. If you add a condition to the breakpoint, the break will not be triggered until the condition is true, and you will be able to see each function call in the Call Stack window.
To open the Breakpoints window, choose View>Breakpoints. Select the breakpoint in the Breakpoints window, right-click to open the context menu, and choose Edit to open the Edit Breakpoint dialog box.
Set the value in the Skip count text box to 4 and click OK.
Looking at the function calls
8
Choose Debug>Go to execute the application until the breakpoint condition is fulfilled.
9
When C-SPY stops at the breakpoint, choose View>Call Stack to open the Call Stack window.
Five instances of the function nth are displayed on the call stack. 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.
You can also open the Register window to see how it is updated as you trace the function calls by double-clicking on the function instances.
Printing the Fibonacci numbers
1
2