3.5.1 C/C++ Preferences
The CDT text editor incorporates “smart typing” features, that include things like autoindentation and formatting, that are controlled by the Code Style preference shown in Figure 3.20. You can select from among four built-in code styles that include:
• KR
• BSD/Allman
• GNU
• Whitesmiths
Figure 3.20: Code Style preference.
The primary difference among the four seems to be the location and indentation of opening and closing braces. The default is KR, where the opening brace is on the same line as the expression or key word that introduces the block. This seems to be the preferred style among Linux programmers.
For what it’s worth, my personal preference is BSD/Allman, where the opening brace is on the next line and indented to line up with the introductory expression. There are options to edit the built-in styles, import a style, and to create a completely new one.
Among the features you can edit in the built-in styles are Tab policy and Tab size. Tab policy specifies whether tabs are represented in the file by tab characters, 0x9, or spaces.
General editor preferences include the ability to change various colors. I find the default color for highlighting matching braces to be a bit “subtle.” To change it, select Matching brackets highlight under Appearance color options: and then click on the Color: button.
A few of the more interesting editor preference subcategories are described here.
Content Assist With Content Assist, the C/C++ editor can offer suggestions about key words and phrases commonly used in C. Type part of a keyword followed by Ctrl+Space to bring up a list of suggestions. When the list has been reduced to one item, the editor can automatically insert that item into your code.
The related auto-completion feature can use “.”, “->”, and “::” as triggers to invoke auto-completion on structures and class definitions.
Content Assist is based on templates that can be specified under Template preferences (see below).
Folding Folding hides the detailed contents of selected regions in a source file. This can be a useful strategy for browsing through large files. Figure 3.21 is an example of a file with functions and macros folded. Click on the “+” icon next to the name to see the contents of a specific function or macro.
Figure 3.21: Folded source file.
Folding preferences let you select if folding is enabled when a new editor is opened, and which sections of code will be folded.
Syntax Coloring There are a large number of semantic elements to which specific coloring and font styles can be assigned. Many of these are not enabled by default and among those that are enabled many share the same coloring. Here’s your chance to “go wild,” if you’re so inclined, and create some exciting color schemes.
Templates Templates are the basis for Content Assist. There’s a default set of templates representing common C/C++ code snippets. You can add your own templates with New, and Edit existing templates.
Typing Typing preferences are another way in which the editor provides assistance. You can tell the editor to automatically close quoted strings, parentheses, brackets, and braces. When you type an opening parenthesis or bracket, the editor automatically inserts the matching close and positions the cursor between them. Type what should be enclosed, then hit Enter to position the cursor just beyond the matching close.
Summary
This chapter has been a quick tour of basic Eclipse concepts and operation with an emphasis on the C/C++ Development Tools (CDT) environment. We began by examining the relationship among Perspectives, Views, and Editors. A Perspective is a collection of Views and Editors organized to accomplish a specific function. Editors allow files to be opened, modified, and saved. Views support Editors by providing additional information and functionality. The selection and arrangement of Views in a Perspective can be changed at will.
The basic functionality of Eclipse, like all windowing programs, is embodied in a set of menus. We looked in detail at many menu, or action, items that are specific to Eclipse. Many of these action items also appear in the tool bar and in Context menus. Items in the tool bar may change or become active or inactive depending on which view has the focus.
Finally, we looked at some of the many configuration and customization options available in Eclipse with particular attention to some of the interesting features of the CDT editor.
The next chapter goes into more detail about the CDT environment and the nature of projects.
CHAPTER 4
C/C++ Developers’ Toolkit (CDT)
With a good basic understanding of Eclipse concepts as a background, this chapter looks at the C Developers’ Toolkit in more detail. It covers how to create, configure, and build projects.
4.1 Obtaining the Sample Source Code
Source code and data files for all the examples in this book are available from http://www.intellimetrix.us/downloads.html in the file EclipseSamples.tar.gz
. Download this file to your home directory and untar it. You’ll find a new directory called EclipseSamples/
with subdirectories for each of the projects described in the book.
4.2 Creating a New Project
For this exercise we’ll create a fairly straightforward record sorting application. The records to be sorted consist of a name and an ID number. To simplify the code a bit, we’ll replace any spaces in the name field with underscores. The program will sort a file of records in ascending order, either by name or ID number, as specified on the command line thusly:
record_sort <datafile> [1 | 2]
Where “1” means sort by name and “2” means sort by ID. Sort by name is the default if no sorting argument is given.
In the Eclipse C/C++ perspective, create a new C Project and call it “record_sort.” The project type is Executable and we’ll use the default workspace location. Clicking Next brings up the Select Configurations dialog where you can select either or both of the Debug and Release configurations. Later you’ll have the choice of building either of these configurations. The primary difference between them is that the Debug configuration is built with the compiler’s debug flag, “-g”, turned on to provide information to GDB. The Release configuration leaves out the debug flag.
There’s also an Advanced settings… button to delve deeper into project configuration. We’ll look at that later.
When you click Finish in the New Project Wizard, the workbench looks like Figure 4.1. The only item under the record_sort
project in the Project Explorer view is Includes, which is a list of paths to search for header files. Eclipse attempted to build the project, but of course there’s nothing to build since there are no source files.
Figure 4.1: Empty project.