corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Editing and Navigating C/C++ Source Files 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.5.

Contents

Content on this page applies to NetBeans IDE 6.5
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.5 with NetBeans C/C++ pack
Java Developer Kit (JDK) version 6 or version 5
Windows platform:
GNU C and C++ compilers, make, gdb

www.cygwin.com
www.mingw.org
Solaris platforms:
GNU C and C++ compilers
Sun C and C++ compilers
Solaris make and gmake
gdb

3.4.3
5.9 (Sun Studio 12 release)
3.80
6.6 or 6.8
Linux platforms:
Red Hat Fedora Core:
gcc and g++
gdb
GNU make

Ubuntu:
gcc and g++
gdb
GNU make

4.1.2
6.6 or 6.8
3.81


4.1.2
6.6 or 6.8
3.81
Mac OS X platforms:
gcc and g++
gdb
GNU make
Xcode bundle

4.0.1
6.3.50
3.80
6.5

See the NetBeans IDE 6.5 Installation Instructions and the Installing and Configuring C/C++ Support for information on 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, 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:

  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 language for which you want to set formatting style from the language drop-down list.
  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 th 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 function 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.

    Screenshot of
               file with highlighted class in the Source
              Editor
  9. In the customer.h file, notice that class fields are highlighted in bold.

    Screenshot of
               file with highlighted class fields  in the Source
              Editor

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.

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

  3. Screenshot of code completion
              box

  4. Select the Customer class and press Enter.
  5. 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.

  6. Screenshot of code completion box

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

  8. Screenshot of code completion
              box

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

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

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

  1. Click the Classes tab to display the Classes window.
  2. Expand the Quote_1 node. 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.

  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.

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.

    Screenshot of the Usages window

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.

  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 view of all of the functions called from the main function.

    Screenshot of the
         Call Graph window
  3. Expand the endl node to display the functions called by that function.
  4. 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.
  5. Expand some of the nodes in the tree to see more functions.

    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.

  1. In the cpu.cc file of the Quote_1 project, mouse over line 48 while pressing Ctrl. The ComputeSupportMetricfunction is highlighted.

  2. Screenshot of the invocation of the function

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

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

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems