Figure 4.11 shows a memory monitor of the area allocated for temp
just after the first fscanf()
call in read_file()
. The Memory view is split into two panes, one that lists the currently active monitors and another that displays the renderings for the selected monitor. A monitor may have multiple renderings enabled, and these will be tabbed in the renderings pane.
Figure 4.11: Memory view.
The first four bytes hold a pointer to another area allocated for the name. This also happens to be visible. Remember that the x86 is a “little endian” machine. Consequently, when memory is displayed as shown here, most entries appear to be “backwards.”
Each of the panes has a small, fairly intuitive set of buttons for managing the pane. These allow you to either add or remove monitors or renderings, and in the case of monitors, remove all of them.
4.6.6 Finish Debugging
With this background on the principal debugging features of Eclipse, you should be able to find the two runtime errors that have been inserted in sort_utils.c
. Good luck.
4.7 Linking Projects
Eclipse allows projects to refer to, and to depend on, other projects. Consider, for example, that you have a family of similar application projects that all make use of a common library. We can have each of the application projects refer to the library project so that any time a change is made in the library, the applications are rebuilt. The library project becomes a dependency of the applications.
To illustrate this idea, we’ll take the five functions in sort_utils.c
and turn them into a static library that we’ll link with record_sort.c
. Switch back to the C/C++ perspective, select File→New… C Project and select Static Library as the project type. Call it “sort.” Click Finish. Right-click on the sort project in Project Explorer and select Import→General→File System. Browse to EclipseSamples/sort/
. Click Select All and Finish. Five C files and one header file are imported into the project. Build the project. The result is libsort.a
.
Now go back to the record_sort project and select Properties from the Project Explorer context menu. Select Project References and check sort
, which may be the only entry (Figure 4.12). Delete sort_utils.c
from the file list.
Figure 4.12: Project References.
Now expand the C/C++ General entry in the left-hand pane and select Paths and symbols. Click the References tab and check sort. This will expand the sort entry and you’ll see that the Active build configuration is selected. Select the Library paths tab and you’ll see that /sort/Debug has been added (assuming that Debug is the active configuration for sort).
Expand the C/C++ Build entry and select Settings. Then select GCC C Linker→General and check No shared libraries. We’ll be using static libraries in this example. Now select Libraries (Figure 4.13). The Library search path lists the path to the sort project.
Figure 4.13: Library selection.
Click the Add icon in the Libraries section and enter “sort.”[3] In the context menu for record_sort in the Project Explorer, clean the project. This will rebuild it using the sort library. To prove that record_sort is now dependent on sort, clean the sort project.
This causes it to be rebuilt with a later timestamp on the library than that of the record_sort executable. Now build record_sort. It will be relinked with the new library.
4.8 Refactoring
Refactoring is generally described as a process of restructuring code for the purpose of readability, performance improvements, reuse, or simply for the “regular evolutionary quest for elegance.”[4] Most programmers engage in a process of refactoring without consciously realizing it. As a project progresses we often realize that the approach westarted with isn’t quite working and it’s time to do some prudent restructuring and reorganization.
Eclipse supports an automated approach to refactoring that makes sure that changes are propagated properly throughout a project. The nature of refactoring support is somewhat language-dependent. Eclipse offers extensive refactoring support for Java involving operations on classes, interfaces, and methods. For the moment, anyway, C/C++ refactoring is limited to renaming. While this may seem trivial, propagating a name change accurately throughout a large project can be burdensome if done by hand.
We’ll use the record_sort project to illustrate the refactoring support in CDT. Go back to the C/C++ perspective if you’re not already there and open record_sort.c
if it isn’t already. In line 23 select read_file
. Now from the main menu, select Refactor→Rename…. This brings up the dialog box in Figure 4.14. Let’s change the name read_file
to file_read
by changing it in the Rename to: field. The default scope is related projects. This is what we want, because we do in fact have a related project that includes this symbol name. The rename dialog is also accessible from the editor’s context menu.
Figure 4.14: Renaming dialog.
With the Rename to: field modified, the Preview button is now active. Click it to bring up the dialog in Figure 4.15. This shows all of the places in both projects where read_file
can be changed to file_read
and offers the option to selectively change any or all of them. Click OK and read_file
will be magically changed to file_read
throughout both projects.
Figure 4.15: Rename change dialog.
Clearly, this example is trivial, but in a large project involving hundreds, perhaps thousands of files, the ability to automatically rename a symbol throughout the project can be quite powerful.
Summary
This chapter has introduced the basic concepts and operation of the C/C++ Development Tools, CDT. We saw how to create a project and looked at the many tools the C Editor offers to ease the task of coding. We examined many of the views in the C/C++ Perspective in detail, to see how they contribute to the development process.
After building the project, we looked at the Debug perspective in detail, with particular emphasis on the many capabilities of breakpoints. We also looked at other views in the Debug perspective to see how they help you gain insight into what’s happening in your program.
Finally, we explored a couple of nifty features of Eclipse that can make life easier for developers. Projects can refer to, and can be dependent on, other projects. When changes are made to the referenced project, the dependent project will be rebuilt. Refactoring offers a way to automatically rename a symbol throughout a project or a set of related projects.
3
I find it a little odd that Eclipse can’t automatically insert the sort library here, since it apparently figured out that sort is a library project.
4
http://wiki.eclipse.org/FAQ_How_do_I_support_refactoring_for_my_own_language?www.newnespress.com