16
Editing and Navigating C/C++ Source Files Tutorial Contributed by Ann Rice and maintained by Susan Morgan June 2009 [Revision number: V6.7-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.7. - 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.7 with NetBeans C/C++ plugin Java Developer Kit (JDK) version 6 or version 5 C and C++ compilers, make, gdb C/C++ Tool Collections Tested with NetBeans IDE See the NetBeans IDE 6.7 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, 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 IDE 6.7 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. 1. In the project wizard, expand the Samples category and select the C/C++ subcategory. 2. Select the Quote project. Click Next, then click Finish. 3. 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. 1. 1 of 16

Netbeans - editing C & C++

  • Upload
    henry

  • View
    581

  • Download
    0

Embed Size (px)

DESCRIPTION

Netbeans - editing C & C++

Citation preview

Page 1: Netbeans - editing C & C++

Editing and Navigating C/C++ Source Files Tutorial

Contributed by Ann Rice and maintained by Susan Morgan

June 2009 [Revision number: V6.7-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.7.

- 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.7 with NetBeans C/C++ plugin

Java Developer Kit (JDK) version 6 or version 5

C and C++ compilers, make, gdb C/C++ Tool Collections Tested with NetBeans IDE

See the NetBeans IDE 6.7 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, 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 IDE 6.7 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.1.

In the project wizard, expand the Samples category and select the C/C++ subcategory.2.

Select the Quote project. Click Next, then click Finish.3.

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

1 of 16

Page 2: Netbeans - editing C & C++

Click Editor in the top pane of the window.2.

Click the Formatting tab.3.

Select the language for which you want to set formatting style from the language drop-down list.4.

Select the style you want to set from the Style drop-down list.5.

Modify the style properties as desired.6.

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.

1.

Click the collapse icon (small box with minus sign) in the left margin to fold the code of one of the

methods.

2.

Mouse over the {...} symbol to the right of the folded block to display the code in the block.3.

2 of 16

Page 3: Netbeans - editing C & C++

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

Click C/C++ in the top pane of the window.2.

Click the Highlighting tab.3.

Make sure that all of the check boxes contain checkmarks.4.

Click OK.5.

In the customer.cc file of the Quote_1 project, notice that the function names are highlighted in bold.6.

Click on an occurrence of the Customer class.7.

All of the occurrences of the Customer class in the file are highlighted with a yellow background.8.

3 of 16

Page 4: Netbeans - editing C & C++

In the customer.h file, notice that class fields are highlighted in bold.9.

4 of 16

Page 5: Netbeans - editing C & C++

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.

1.

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 Customer class. You can expand the list by pressing Ctrl-Space

again.

2.

5 of 16

Page 6: Netbeans - editing C & C++

Select the Customer class and press Enter.3.

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.

4.

6 of 16

Page 7: Netbeans - editing C & C++

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.

5.

Delete the code you have added.6.

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

Type iff followed by a space. iff expands to if () {}.2.

7 of 16

Page 8: Netbeans - editing C & C++

Type ife, followed by a space. ife expands to if () {} else {}.3.

Type fori followed by a space. fori expands to for (int i = 0; i < ; i++) {}.4.

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.

1.

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.

2.

Type invalid=0, success=1 to complete the enumeration.3.

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.

4.

Type v==null. Then type { and newline after the right parenthesis. The closing bracket is added

automatically.

5.

Delete the code you have added.6.

Navigating C and C++ Source Files

The NetBeans IDE 6.7 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.1.

Expand the Quote_1 node. All classes in the project are listed.2.

Expand the Customer class.3.

8 of 16

Page 9: Netbeans - editing C & C++

Double-click the name variable to open the customer.h header file.4.

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

A compact view of the file is displayed in the Navigator window.2.

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.

3.

9 of 16

Page 10: Netbeans - editing C & C++

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

In the Find Usages dialog box, click Find.2.

The Usages window opens and displays all of the usages of the Customer class in the source files of the

project.

3.

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 the selected function.

In the quote.cc file, right-click on the main function and choose Show Call Graph.1.

The Call Graph window opens and displays a tree view of all of the functions called from the main

function.

2.

10 of 16

Page 11: Netbeans - editing C & C++

Expand the endl node to display the functions called by that function.3.

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.

4.

Expand some of the nodes in the tree to see more functions.5.

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 36 while pressing Ctrl. The

ComputeSupportMetricfunction is highlighted.

1.

11 of 16

Page 12: Netbeans - editing C & C++

Click the hyperlink and the editor jumps to the definition of the function.2.

12 of 16

Page 13: Netbeans - editing C & C++

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.

3.

13 of 16

Page 14: Netbeans - editing C & C++

Click the left arrow in the editor toolbar and the editor jumps back to the definition.4.

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.

14 of 16

Page 15: Netbeans - editing C & C++

In the Quote_1 project, open the module.cc file in the Source Editor.1.

Right-click on the #include "module.h" line in the file and choose Navigate > View Includes

Hierarchy.

2.

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.

3.

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

Right-click on the declaration of the Module class and choose Navigate > View Type Hierarchy.2.

The Hierarchy window displays all of the subtypes of the Module class.3.

15 of 16

Page 16: Netbeans - editing C & C++

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.

IMG

16 of 16