Editing and Navigating C/C++ Source Files Tutorial
Contributed and maintained by
September 2008 [Revision number: V6.5-1]
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.5.
Just want to play with some projects? In the IDE, choose
File > New Project, then open the Samples category, the
C/C++ subcategory, 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 C/C++ Pack 6.1 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 language for which you want to set formatting style from the
language drop-down list.
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 th 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 function 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.
In the customer.h file, notice that class fields are
highlighted in bold.
Using Code Completion
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.
Open the quote.cc file in the Quote_1 project, and
right-click in the left margin of the Source Editor and choose Show Line
Numbers.
On line 41 of the quote.cc,
type a capital C and press Ctrl-Space. The code completion box
displays a short list that includes the
Customer class. You can expand the list by pressing
Ctrl-Space again.
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.
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 spacebar. For example, in the
Singleton.cpp file of the Loki project:
Type uns followed by a
space. uns expands to unsigned.
Type iff followed by a space.
iff expands to if () {}.
Type ife, followed by a space.
ife expands to if () {} else {}.
Type fori followed by a
space. fori expands to for (int i = 0; i <
; i++) {}.
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 114 of the
module.cc file and press
Return to go to 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 C/C++ Pack 6.5 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.
Expand the Quote_1 node. 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.
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.
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 a tree view of either the
functions called from a selected function, or the functions that
call that function.
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 view of all of the
functions called from the main function.
Expand the endl node to display the functions called
by that function.
Click the second button on the left side of the window to focus on
the endl function, then click the bottom button to change the
graph to a tree view of all of the functions that call the 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.
In the cpu.cc file of the Quote_1 project, mouse over line 48 while pressing Ctrl. The
ComputeSupportMetricfunction is highlighted.
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 and the editor jumps back to the definition.
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 ar 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.