Editing and Navigating C/C++ Source Files - NetBeans IDE 6.9 Tutorial
Contributed by Ann
Rice and maintained by Susan Morgan
September 2010 [Revision number: V6.9-2]
The following short tutorial takes you through some of the features for navigating
and editing the source files in a C or C++ project in NetBeans IDE 6.9.
Contents
Requirements
To follow this tutorial, you need the following software and resources.
See the NetBeans IDE
6.9 Installation
Instructions and
Configuring the NetBeans IDE for C/C++/Fortran
for
information about downloading and installing the required software.
Sample Projects
Just want to play with some projects? In the IDE, choose
File > New Project, then open the Samples category
and the C/C++ subcategory. The IDE
provides several sample C and C++ projects to help you familiarize
yourself with the IDE.
Creating the Sample Project
The NetBeans IDE 6.9 with the C/C++ plugin provides advanced editing features for modifying your source code. To explore these features, we'll use
the Quote project. If you have not already created this
project, do the following:
- Choose File > New Project.
- In the project wizard, expand the Samples category and select the C/C++ subcategory.
- Select the Quote
project. Click Next, then click Finish.
Editing C and C++ Source Files
Setting the Formatting Style
You can use the Options window to configure default formatting style for
your C and C++ projects.
- Choose Tools > Options.
- Click Editor in the top pane of the window.
- Click the Formatting tab.
- Select the C++ language from the Language drop-down list because the Quote project uses
C++.
- Select the style you want to set from the Style drop-down list.
- Modify the style properties as desired.
Folding Blocks of Code in C and C++ Files
For some types of files, you can use the code folding feature to collapse
blocks of code so that only the first line of the block appears in the Source
Editor.
- In the Quote_1 application project, open the Source Files folder,
then double-click the cpu.cc file to open it in the Source Editor.
- Click the collapse icon (small box with minus sign) in the left margin to fold the code of one of the
methods.
- Mouse over the {...} symbol to the right of the folded block to
display the code in the block.
Using Semantic Highlighting
You can set an option so that when you click on a class, function, variable,
or macro, all occurrences of that class, function, variable, or macro in the
current file are highlighted.
- Choose Tools > Options.
- Click C/C++ in the top pane of the window.
- Click the Highlighting tab.
- Make sure that all of the check boxes contain checkmarks.
- Click OK.
- In the customer.cc file of the Quote_1 project,
notice that the method names are highlighted in bold.
- Click on an occurrence of the Customer class.
- All of the occurrences of the Customer class in the file are
highlighted with a yellow background. The right margin also shows markings
that indicate points where an occurrence is located in the file. The markings
let you see how many occurrences there are without scrolling through the file.
You can click the markings to jump to the occurrences that they represent.
- To easily navigate between the occurrences, press the Alt+Down keys to go to the next occurrence,
and press Alt+Up to go to the previous occurrence.
- Open the customer.h file, located in the Headers folder, and notice that
method names are again highlighted in bold.
Using Code Completion and Documentation
The IDE has a dynamic C and C++ code completion feature
that enables you to type one or more characters and then see a
list of possible classes, methods, variables, and so on that can be used
to complete the expression.
The IDE also dynamically searches for documentation for the classes, functions,
methods and so on, and displays the documentation in a popup window.
- Open the quote.cc file in the Quote_1 project.
- On the first blank line of the quote.cc,
type a capital C and press Ctrl-Space. The code completion box
displays a short list that includes the Cpu and
Customer classes. A documentation window also opens but displays
"No documentation found" because the project source does not
include documentation for its code.
- Expand the list of items by pressing
Ctrl-Space again.
- Use your arrow keys or mouse to highlight a standard library function
such as calloc, and the documentation window displays the man page for that function
if the man page is accessible to the IDE.
- Select the Customer class and press Enter.
- Complete the new instance of the Customer class by typing "
andrew;". On the next line, type the letter a and press
Ctrl-Space. The code completion box displays a list of choices starting with
the letter a, such as method
arguments, class fields, and global names, that are accessible from the
current context.
- Double-click the andrew option to accept it and type a period after it.
You are automatically provided with a list of the public methods and fields
of the Customer class.
- Delete the code you have added.
Adding Source Code Documentation
You can add comments to your code to automatically generate documentation
for your functions, classes, and methods. The IDE recognizes comments that use Doxygen
syntax and automatically generates documentation. The IDE can also automatically
generate a comment block to document the function below the comment.
- In the quote.cc file, place your cursor on line 74, or the line above the line
int readNumberOf(const char* item, int min, int max) {
- Type a slash and two asterisks and press Enter. The editor inserts a Doxygen-formatted
comment for the readNumberOf class.
- Add some descriptive text to each of the @param lines and save the file.
- While the readNumberOf class is still highlighted in yellow, click one of the
occurrences marks on the right to jump to a location where the class is used.
- Click the readNumberOf class in the line you jumped to, and press Ctrl-Shift-Space
to show the documentation that you just added for the parameters.
- Click anywhere else in the file to close the documentation window, and click on the
readNumberOf class again.
- Use the menu option Source > Show Documention to open the documentation window
for the class again.
Using Code Templates
The Source Editor has a set of customizable code templates for common snippets
of C and C++ code. You can generate the full code snippet by typing its
abbreviation and pressing the Tab key. For example, in the
quote.cc file of the Quote project:
- Type uns followed by a tab and uns expands to unsigned.
- Type iff followed by a tab and iff expands to if (exp) {}.
- Type ife followed by a tab and ife expands to if (exp) {} else {}.
- Type fori followed by a tab and fori expands to for (int i = 0; i < size; i++) {
Object elem = array[i];.
You can see all the available code templates, modify the code templates, and create
your own code templates. You can also select a different key to expand the code templates.
Choose Tools > Options > Editor > Code Templates, and select the appropriate language.
Using Pair Completion
When you edit your C and C++ source files, the Source Editor does "smart"
matching of pair characters such as brackets, parentheses, and quotation marks.
When you type one of these characters, the Source Editor automatically inserts the closing character.
- In the Quote_1 project, place the cursor after the {
on line 115 of the
module.cc file and press
Return to go open a new line.
- Type enum state { and press Return. The closing curly bracket and
semi-colon are added automatically and the cursor is placed on the line
between the brackets.
- Type invalid=0, success=1 to
complete the enumeration.
- On the line after the closing }; of the enumeration, type if
(. The closing parenthesis is added automatically and the cursor is
placed between the parentheses.
- Type v==null. Then type { and newline after the right
parenthesis. The closing bracket is added automatically.
- Delete the code you have added.
Navigating C and C++ Source Files
The NetBeans IDE 6.9 with the C/C++ plugin provides advanced navigation features for
viewing your source code. To explore these features, we'll continue
using the Quote_1 project.
Using the Classes Window
The Classes window lets you see all of the classes in your project, and the
members and fields for each class.
- Click the Classes tab to display the Classes window.
If the Classes tab is not available, select Window > Classes
- Expand the Quote_1 node in the Classes window. All
classes in the project are listed.
- Expand the Customer class.
- Double-click the
name variable to open the
customer.h header file.
Using the Navigator Window
The Navigator window provides a compact view of the file that is currently
selected, and simplifies navigation between different
parts of the file. If you do not see the Navigator window, select
Window > Navigating > Navigator to open it.
- Click anywhere in the quote.cc file in the Editor window.
- A compact view of the file is displayed in the Navigator
window.
- To navigate to an element of the file, double-click the element in the Navigator window and the cursor in
the Editor window moves to that element.
- Right-click in the Navigator to choose a different way to sort the elements, or group the items, or filter them.
To find out what the icons in the Navigator represent, open the IDE online help
by choosing Help > Help Contents, and search for "navigator icons" in the help window.
Finding Class, Method, and Field
Usages
You can use the Usages window to show you everywhere a class (structure), function, variable, macro, or
file is used in your project's source code.
- In the customer.cc file, right-click the Customer
class on line 44, and choose Find Usages.
- In the Find Usages dialog box, click Find.
- The Usages window opens and displays all of the usages of the
Customer class in the source files of the project.
Using the Call Graph
The Call Graph window displays two views of the calling
relationships between functions in the project. A tree view
shows the functions called from a selected function, or the functions that
call the selected function. A graphical view shows the calling
relationships using arrows between the called and calling functions.
- In the quote.cc file, right-click on the main
function and
choose Show Call Graph.
- The Call Graph window opens and displays a tree and graphical view of all
functions called from the main function.
If you do not see all the functions as shown here, click the third button
on the left side of the Call Graph window to show "who is called from this function."
- Expand the endl node to display the functions called
by that function. Notice the graph is updated to show the functions called by endl as well.
- Click the second button on the left side of the window to focus on
the endl function, then click the fourth button to view all the change the
functions that call the endl function.
- Expand some of the nodes in the tree to see more functions.
Using Hyperlinks
Hyperlink navigation lets you jump from the invocation of a class, method, variable,
or constant to its declaration, and from its declaration to its definition. Hyperlinks also
let you jump from a method that is overridden to the method that overrides it, and vice
versa.
- In the cpu.cc file of the Quote_1 project, mouse over line 36 while pressing Ctrl. The
ComputeSupportMetricfunction is highlighted and an annotation displays information about the
function.
-
Click the hyperlink and the
editor jumps to the definition of the function.
- Mouse over the definition while pressing Ctrl, and click the hyperlink.
The editor jumps to the declaration of the function in the cpu.h
header file.
- Click the left arrow in the editor toolbar (circled in the screenshot) and the editor jumps back to
the definition in cpu.cc.
- Hover your mouse over the green circle in the left margin and see the annotation that
indicates that this method overrides another method.
- Click the green circle to go to the overridden method and you jump to the module.h header file,
which shows a gray circle in the margin to indicate the method is overridden.
- Click the gray circle and the editor displays a list of methods that override this method.
- Click the Cpu::ComputeSupportMetric item and you jump back to the declaration
of the method in the cpu.h
header file.
Using the Include Hierarchy
The Include Hierarchy window lets you inspect all header and source files
that are directly or indirectly included in a source file, or all source and
header files that directly or indirectly include a header file.
- In the Quote_1 project, open the module.cc file in the
Source Editor.
- Right-click on the #include "module.h" line in the file and choose
Navigate > View Includes Hierarchy.
- By default, the Hierarchy window displays a plain list of files that directly
include the header file. Click the right-most button at the bottom of the
window to change the display to a tree view. Click the second
button from the right to change the display to all files that
include or are included. Expand the nodes in the tree view
to see all of the source files that include the header file.
Using the Type Hierarchy
The Type Hierarchy window lets you inspect all subtypes or supertypes
of a class.
- In the Quote_1 project, open the module.h
file.
- Right-click on the declaration of the Module class and
choose Navigate > View Type Hierarchy.
- The Hierarchy window displays all of the subtypes of the Module
class.
See Debugging C/C++
Projects for a tutorial on using some of the features for
debugging a C or C++ project in NetBeans IDE.