corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Editing and Navigating C/C++ Source Files - NetBeans IDE 6.9 Tutorial

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

Content on this page applies to NetBeans IDE 6.9
Requirements
Creating the Sample Project
Editing C and C++ Source Files
Navigating C and C++ Source Files
Next Steps

Requirements

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE version 6.9 with NetBeans C/C++ plugin
Java Developer Kit (JDK) version 6
C and C++ compilers, make, gdb C/C++ Tool Collections Tested with NetBeans IDE

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:

  1. Choose File > New Project.
  2. In the project wizard, expand the Samples category and select the C/C++ subcategory.
  3. 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.
  1. Choose Tools > Options.
  2. Click Editor in the top pane of the window.
  3. Click the Formatting tab.
  4. Select the C++ language from the Language drop-down list because the Quote project uses C++.
  5. Select the style you want to set from the Style drop-down list.

  6. Screenshot of the Formatting Styles
tab

  7. 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.

  1. 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.
  2. Click the collapse icon (small box with minus sign) in the left margin to fold the code of one of the methods.
  3. Mouse over the {...} symbol to the right of the folded block to display the code in the block.

  4. Screenshot of folded code in the Source
              Editor

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.

  1. Choose Tools > Options.
  2. Click C/C++ in the top pane of the window.
  3. Click the Highlighting tab.
  4. Make sure that all of the check boxes contain checkmarks.
  5. Click OK.
  6. In the customer.cc file of the Quote_1 project, notice that the method names are highlighted in bold.
  7. Click on an occurrence of the Customer class.
  8. 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.
  9. 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.
  10. Screenshot of
               file with highlighted class in the Source
              Editor
  11. Open the customer.h file, located in the Headers folder, and notice that method names are again highlighted in bold.
  12. Screenshot of
               file with highlighted class fields  in the Source
              Editor

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.

  1. Open the quote.cc file in the Quote_1 project.
  2. 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.
  3. Expand the list of items by pressing Ctrl-Space again.

  4. Screenshot of code completion
              box
  5. 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.

  6. Screenshot of code completion
              documentation
  7. Select the Customer class and press Enter.
  8. 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.
  9. Screenshot of code completion box
  10. 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.
  11. Screenshot of code completion
              box
  12. 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.

  1. 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) {
  2. Type a slash and two asterisks and press Enter. The editor inserts a Doxygen-formatted comment for the readNumberOf class. Screenshot of
              automatically generated Doxygen comments
  3. Add some descriptive text to each of the @param lines and save the file.
  4. 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.
  5. Screenshot of
              edited Doxygen comments
  6. 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.
  7. Screenshot of
              displayed Doxygen documentation
  8. Click anywhere else in the file to close the documentation window, and click on the readNumberOf class again.
  9. 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:

  1. Type uns followed by a tab and uns expands to unsigned.
  2. Type iff followed by a tab and iff expands to if (exp) {}.
  3. Type ife followed by a tab and ife expands to if (exp) {} else {}.
  4. 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.

  1. 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.
  2. 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.
  3. Type invalid=0, success=1 to complete the enumeration.
  4. 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.
  5. Type v==null. Then type { and newline after the right parenthesis. The closing bracket is added automatically.
  6. 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.

  1. Click the Classes tab to display the Classes window. If the Classes tab is not available, select Window > Classes
  2. Expand the Quote_1 node in the Classes window. All classes in the project are listed.
  3. Expand the Customer class.

  4. Screenshot of the Class View tab

  5. 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.

  1. Click anywhere in the quote.cc file in the Editor window.
  2. A compact view of the file is displayed in the Navigator window.
  3. Screenshot of the Navigator window
  4. 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.
  5. Right-click in the Navigator to choose a different way to sort the elements, or group the items, or filter them.
  6. 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.

  1. In the customer.cc file, right-click the Customer class on line 44, and choose Find Usages.
  2. In the Find Usages dialog box, click Find.
  3. The Usages window opens and displays all of the usages of the Customer class in the source files of the project.
  4. Screenshot of the Usages window

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.

  1. In the quote.cc file, right-click on the main function and choose Show Call Graph.
  2. The Call Graph window opens and displays a tree and graphical view of all functions called from the main function.
  3. Screenshot of the
         Call Graph window

    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."

  4. 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.
  5. 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.
  6. Expand some of the nodes in the tree to see more functions.
  7. Screenshot of the
         Call Graph window

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.

  1. 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.
  2. Screenshot of the invocation of the function with annotation
  3. Click the hyperlink and the editor jumps to the definition of the function.
  4. Screenshot of the definition of the function
  5. 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.
  6. Screenshot of the declaration of the function
  7. Click the left arrow in the editor toolbar (circled in the screenshot) and the editor jumps back to the definition in cpu.cc.
  8. Hover your mouse over the green circle in the left margin and see the annotation that indicates that this method overrides another method.
  9. Screenshot of override annotation
  10. 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.
  11. Click the gray circle and the editor displays a list of methods that override this method.
  12. Screenshot of overriding methods
  13. 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.

  1. In the Quote_1 project, open the module.cc file in the Source Editor.
  2. Right-click on the #include "module.h" line in the file and choose Navigate > View Includes Hierarchy.
  3. 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.
  4. Screenshot of the Include hierarchy

Using the Type Hierarchy

The Type Hierarchy window lets you inspect all subtypes or supertypes of a class.

  1. In the Quote_1 project, open the module.h file.
  2. Right-click on the declaration of the Module class and choose Navigate > View Type Hierarchy.
  3. The Hierarchy window displays all of the subtypes of the Module class.
  4. Screenshot of the Type hierarchy

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.