Editing and Navigating C/C++ Source Files - NetBeans IDE Tutorial
| This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
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.
Requirements
To follow this tutorial, you need the following software and resources.
| Software or Resource | Version Required |
|---|---|
NetBeans IDE |
xref../../../download/index.adoc[ version 7.4 or 8.0 with NetBeans C/C++ plugin] |
Java Developer Kit (JDK) |
See the NetBeans IDE 8.0 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 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
Quoteproject. 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_1application project, open the Source Files folder, then double-click thecpu.ccfile 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 Editor 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.ccfile of theQuote_1project, notice that the method names are highlighted in bold. -
Click on an occurrence of the
Customerclass. -
All of the occurrences of the
Customerclass 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 Alt+Down to go to the next occurrence, and press Alt+Up to go to the previous occurrence.
-
Open the
customer.hfile, located in the Headers folder, and notice that class 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.ccfile in theQuote_1project. -
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 theCpuandCustomerclasses. 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
callocfrom the list, and the documentation window displays the man page for that function if the man page is accessible to the IDE.
-
Select the
Customerclass and press Enter.
-
Complete the new instance of the
Customerclass by typing "andrew;". On the next line, type the letteraand press Ctrl-Space twice. The code completion box displays a list of choices starting with the lettera, such as method arguments, class fields, and global names, that are accessible from the current context.
-
Double-click the
andrewoption to accept it and type a period after it. Press Ctrl-Space and you are provided with a list of the public methods and fields of theCustomerclass.
-
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.ccfile, place your cursor on line 75, or the line above the lineint 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
readNumberOfclass.
-
Add some descriptive text to each of the @param lines and save the file.
-
Click the
readNumberOfclass to highlight it in yellow, and click one of the occurrences marks on the right to jump to a location where the class is used.
-
Click the
readNumberOfclass 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
readNumberOfclass again.
-
Choose Source > Show Documentation 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
unsfollowed by a tab andunsexpands tounsigned. -
Type
ifffollowed by a tab andiffexpands toif (exp) {}. -
Type
ifefollowed by a tab andifeexpands toif (exp) {} else {}. -
Type
forifollowed by a tab andforiexpands tofor (int i = 0; i < size; i++) { Object elem = array[i];.
To see all the available code templates, modify the code templates, create your own code templates, or select a different key to expand the code templates:
-
Choose Tools > Options.
-
In the Options dialog box, select Editor, and click the Code Templates tab.
-
Select the appropriate language from the Language drop-down list.
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_1project, place the cursor on the blank line 115 of themodule.ccfile and press Return to 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=1on the line between the brackets to complete the enumeration. -
On the line after the closing
};of the enumeration, typeif (and you should see that a closing parenthesis is added automatically and the cursor is placed between the parentheses. -
Type
v==nullbetween the parentheses. Then type{and newline after the right parenthesis. The closing bracket is added automatically. -
Delete the code you have added.
Finding Text in Project Files
You can use the Find In Projects dialog box to search projects for instances of specified text or a regular expression.
-
Open the Find In Projects dialog box by doing one of the following:
-
Choose Edit > Find In Projects.
-
Right-click a project in the Projects window and choose Find.
-
Press Ctrl+Shift+F.
-
-
In the Find In Projects dialog box, select the Default Search tab or the Grep tab. The Grep tab uses the
greputility, which provides a faster search, especially for remote projects.
-
In the Grep tab, type the text or regular expression for which you want to search, specify the search scope and file name pattern, and select the check box Open in New Tab so you can save multiple searches in separate tabs.
-
Click Find. The Search Results tab lists the files in which the text or regular expression is found.
Buttons in the left margin enable you to change your view of the search results.
-
Click the Expand/Collapse button to collapse the list of files so only the filenames are shown. Click the other buttons to show the search results as a directory tree or as a list of files. These options are useful when you perform a search across multiple projects.
-
Double-click one of the items in the list and the IDE takes you to the corresponding location in the source editor.
Navigating C and C++ Source Files
The NetBeans IDE with the C/C++ plugin provides advanced navigation features for viewing your source code. To explore these features, 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 displayed, choose Window > Classes
-
Expand the
Quote_1node in the Classes window. All classes in the project are listed. -
Expand the
Customerclass.
-
Double-click the
namevariable to open thecustomer.hheader 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 the Navigator window is not displayed, choose Window > Navigating > Navigator to open it.
-
Click anywhere in the
quote.ccfile 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.ccfile, right-click theCustomerclass on line 42, and choose Find Usages. -
In the Find Usages dialog box, click Find.
-
The Usages window opens and displays all of the usages of the
Customerclass in the source files of the project.
-
Click the arrow buttons in the left margin to step through the occurrences and show them in the Editor, or change between logical and physical view. You can also filter the information using a second column of buttons in the left margin.
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.ccfile, right-click on themainfunction and choose Show Call Graph. -
The Call Graph window opens and displays a tree and graphical view of all functions called from the
mainfunction.
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
endlnode to display the functions called by that function. Notice the graph is updated to show the functions called byendlas well.
-
Click the second button, called Bring Into Focus, on the left side of the window to focus on the
endlfunction, then click the fourth button Who Calls this Function to view all the functions that call theendlfunction.
-
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.ccfile of theQuote_1project, mouse over line 37 while pressing Ctrl. TheComputeSupportMetricfunction 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.hheader file.
-
Click the left arrow in the editor toolbar (second button from the left) and the editor jumps back to the definition in
cpu.cc.
-
Hover the mouse cursor 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.hheader 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::ComputeSupportMetricitem and you jump back to the declaration of the method in thecpu.hheader file.
Using the Includes Hierarchy
The Includes 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_1project, open themodule.ccfile 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_1project, open themodule.hfile. -
Right-click on the declaration of the
Moduleclass and choose Navigate > View Type Hierarchy. -
The Hierarchy window displays all of the subtypes of the Module class.
Next Steps
See Debugging C/C++ Projects for a tutorial on using some of the features for debugging a C or C++ project in NetBeans IDE.