You must have the following software installed before you can
start C or C++ development:
NetBeans IDE 6.0
NetBeans C/C++ pack
GNU C and C++ compilers, make, and gdb (Windows platform); GNU C and C++
compilers, or Sun C and
C++ compilers, make, and gdb (Solaris and Linux Platforms)
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 C and C++ Projects
NetBeans C/C++ support lets you create C and C++ Application
and Library projects with generated makefiles, as well as C and C++ projects
from existing code.
With a C/C++ Application, Dynamic Library, or Static Library project, the
IDE controls all aspects
of how your application is built, run, and debugged. You specify project settings
when creating the project and in the Project Properties dialog box. The IDE
generates a makefile in which all of your settings are stored.
Creating a C/C++ Application Project
Open the New Project wizard by choosing File > New Project.
In the wizard, select the C/C++ category.
The wizard gives you a choice of four types of new projects:
C/C++ Project From Existing
Code, C/C++ Application,
C/C++ Dynamic Library, and C/C++ Static Library.
Select C/C++ Application and click Next.
Create a new C/C++ Application project from the wizard using the defaults.
You can choose the name of the project and the location of the project.
Click Finish to exit the wizard.
A project is created with four logical folders.
A logical folder is not a directory. It is a way for you to organize your
files and does not
reflect where the files are physically stored on disk. Files added to logical
folders are automatically part of the project and are compiled when you build
the project.
Files added to the Important Files folder are not part of the project and are
not compiled when you build the project. These files are just for reference
and are convenient when you have a project with an existing makefile.
Switching Between the Logical View and the Physical View of the Project
A project has both a logical and a physical view.
You can switch between the logical view and the physical view of your
project.
Select the Files tab. This window shows the physical view of your
project. It displays files and folders as they are stored on disk.
Select the Projects tab. This window shows the logical view of
your project.
Adding Files and Folders to Your Project
You can add logical folders to your project.
Right-click the project node of your Application project
and choose New Logical Folder. A new logical folder is added to the project.
Right-click the new logical folder and select Rename. Type the name
you would like to give the new folder.
You can add both
files and folders to an existing folder. Logical folders can be nested.
Adding New Files to Your Project
You can add new files to your project.
Right-click the Source Files folder and choose New.
Choose Main C File from the submenu.
On the Name and Location page of the New File dialog box, type main in the File Name field.
Click Finish.
The main.c file is created on disk in the
directory specified in the wizard and added to the Source Files folder. You
can add any kind of file to this folder, not only source files.
Adding More New Files to Your Project
Right-click the Header Files folder and choose New.
Choose C Header File from the submenu.
On the Name and Location page of the New File dialog box, type file in the File Name field.
Click Finish.
The file is created on disk in the directory specified in the wizard
and added to the Header Files folder.
Adding Existing Files to Your Project
You can add existing files to your project in two ways:
Right-click the Source Files folder and choose Add Existing Item.
You can point to an existing file on disk using the Select Item
dialog box and add the file to the project.
Right-click the Source Files folder and choose Add Existing Items from
Folders. Use the Add Files dialog box to add folders that contain existing
files.
Do not use New menu item to add existing items. The Name and Location panel
will tell you the file already exists.
Setting Project Properties
When the project is created, it has two configurations, Debug and Release.
A configuration is the project's way of storing the current settings.
The Debug configuration builds a version of your application that includes
debug information.
The Release configuration builds an optimized version.
The Project Properties dialog box contains build and configuration information
for your project. To open the Project Properties dialog box:
Right-click the project node of the
Application_1 project and choose Properties.
You can modify the compiler settings and other configuration settings in the
Project Properties dialog box by selecting a node in the left panel and modifying
the properties in the right panel. Select some of the nodes and property
values and notice the properties you can set. You are setting properties
in the currently selected configuration.
Managing Configurations
Properties changed in the Project Properties window are stored in the
makefile for the current configuration. You can edit the default configurations
or create new ones. To create a new configuration:
Click the Manage Configurations button in the Project Properties
dialog box.
In the Configurations dialog box, select the configuration which most
closely matches your desired configuration. In this case, select the
Release configuration and click the Copy button. Then click Rename.
In the Rename dialog box, rename the
configuration to PerformanceRelease. Click OK.
Click OK in the Configurations dialog box.
In the Project Properties dialog box, note that the PerformanceRelease
configuration is selected in the Configuration drop-down list.
In the left panel, expand the C/C++ node,
and then the GNU C Compiler node, and select the General node.
In the property sheet in the right panel, change the Development
Mode from Release to PerformanceRelease. Click OK.
You have created a new configuration that will compile the application with a
different set of options.
Setting Source File Properties
When you set the project properties for your C or C++ project, the relevant
properties apply to all files in the project. You can set some
properties for a specific file.
Right-click the main.c source file
and choose Properties. You can override the project compiler settings
and other properties on a per file basis. You can also exclude files
from a build in a specific configuration.
Cancel the Project Properties dialog box.
Setting the Main Project
When you right-click a project node in the Projects window,
you get a pop-up menu of actions you can perform on the selected project.
If you have multiple projects open at the same time, the pop-up menu
for a project node implies you are operating on that project.
But what about project-related actions on the menubar and toolbar?
Most of the project-related actions on the menubar and toolbar operate on the
main project. The main project node is displayed in bold text in the
Project window.
To change the main project in the IDE:
Right-click the desired project node and choose Set as Main Project.
This project is now the main project in the IDE
and actions in the menubar and toolbar refer to this project.
Building Your Project
To build your project:
Choose Build > Build Main Project and the project
builds. The build output is shown in the Output window.
Switch the configuration from Debug to PerformanceRelease in the configuration
drop-down list in the main toolbar. Now the project will be built using the
PerformanceRelease configuration.
Choose Build > Build Main Project and the project
builds. The build output is shown in the Output window.
You can build, clean, or both clean and build the project
by choosing actions from the Build menu. The project also keeps object
files and executables from different configurations separate, so
you do not have to worry about mixing files from multiple configurations.
Compiling a Single File
To compile a single source file:
Right-click on the main.c file and choose
Compile File. Only this file is compiled.
Note: Single file compilation is not supported for the project type
C/C++ Project From Existing Code.
Running a Project
The program prints command-line arguments. Before running the
program, we will set some arguments in the current configuration. Then we will run
the program.
To create the Args project, set some arguments, and run the project:
Choose File > New Project.
In the project wizard, expand the Samples category and the C/C++ subcategory.
Select the C/C++ subcategory, then select the Args project. Click
Next, then click Finish.
Right-click the Args_1 project node and choose Build Project. The
project builds.
Right-click the Args_1 project node and choose Properties.
In the Project Properties dialog box, select the Running node.
In the Arguments text field, type 1111 2222 3333. Click OK.
Choose Run > Run Main Project. The application runs.
Your arguments are displayed in the Output window.
Creating a C/C++ Project From Existing
Code
With a C/C++ Project From Existing Code, the IDE relies on your
existing makefile for instructions on how to compile and run your application.
Uncompress loki-0.1.6 in a directory of your choice.
Open the New Project wizard by choosing File > New Project.
Select the C/C++ category.
Select C/C++ Project From Existing Code and click Next.
On the Build Tool page, click the Browse button for
the Makefile field.
In the Select Makefile dialog box, navigate to the
directory where you saved
loki-0.1.6. Select the Makefile.
Click Select.
Click Next.
On the Build Actions page, the Working Directory text field, the Build command text field, and
the Clean command text field are filled in automatically. In the Build
Result field, type the full path to where the makefile will put the
library, libloki.a, built from the loki-0.1.6 source code.
Click Next.
On the Source File Folders page, the path to the source file folder for
the project is automatically listed. You could specify additional
folders by clicking Add and using the Source File Folders dialog box.
On the Code Assistance Configuration page, click Next.
On the Project Name and Location page, type Loki in the Project Name
field. Use the defaults for all of the other fields. Click Finish.
The project is created and opened in the Project window. You have created a
project that is a thin wrapper around existing code.
Building and Rebuilding Your Project
To build the project:
Right-click the project node of the project and choose Build Project.
To rebuild the project:
Right-click the project node of the project and choose Clean and Build
Project.
Editing Source Files
The C/C++ Development Pack provides advanced editing features to help you
in viewing and modifying your source code. To explore these features, we'll use
the Quote project:
Choose File > New Project.
In the project wizard, expand the Samples category and the C/C++ subcategory.
Select the Applications subcategory, then select the Quote
project. Click Next, then click Finish.
The Classes window lets you see all of the classes in your project, and the
members and fields for each class.
Choose Window > Classes to open the Classes window, which displays
the Quote_1 project.
Expand the Quote_1 node. All
classes in the project are listed.
Expand the Customer class.
Double-click on the
customerName 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.
To display source file in the Navigator window:
Select the file in the
Editor window, the Projects window, or the Files window.
To navigate to an element of the file displayed in the Navigator window:
Double-click the element in the Navigator window and the cursor in
the Editor window moves to that element.
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, right-click in
the left margin of the Source Editor and choose Show Line Numbers.
Mouse over line 26 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
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. 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.
To open the Type Hierarchy window, right-click on the class declaration
or the class reference in a source or header file. To display the class hierarchy in a window in the IDE, choose
View Type Hierarchy. To display the class hierarchy in a pop-up window, choose
Inspect Type Hierarchy. The default display is a tree view of all of the subtypes
of the class.
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 19 of the quote.cc,
type a capital C and press Ctrl-Space. The code completion box displays a list that includes the
Customer class.
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 92 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.
You can create and manipulate breakpoints in your code at any time.
Creating and Removing a Line Breakpoint
In the Quote_1 project, open the quote.cc file.
Set a line breakpoint by clicking in the left margin of the Source Editor
window next to line 151 (response = readChar("Enter disk module type: (S for single disks, R for RAID; Q - exit)",
'S');). The line
is highlighted in red to indicate that
the breakpoint is set.
You could remove the breakpoint by clicking on the icon in the left margin.
Choose Window > Debugging > Breakpoints to open the Breakpoints
window. Your line breakpoints are listed in the window.
Creating a Function Breakpoint
Choose Run > New Breakpoint (Ctrl+Shift+f8) to open the New Breakpoint
dialog box.
In the dialog box, make sure the Debugger drop-down list is
set to C, C++ Fortran (gdb).
In the Breakpoint Type drop-down list, set the type to Function..
Type the function name Customer::GetDiscount in the Function
Name text field. Click OK.
Your function breakpoint is set and is added to the list in the
Breakpoints window.
Debugging a Project
When you start a debugging session, the IDE starts the gdb
debugger, then runs the application inside the debugger. The IDE automatically
opens the debugger
windows and prints debugger output to the Output window.
Starting a Debugging Session
Start a debugging session for the Quote_1 project by
right-clicking the project node and choosing Debug Project. The debugger
starts and the application
runs,
and the Local Variables, Watches, and Call Stack windows open.
Open the Sessions window by choosing Window > Debugging > Sessions.
The debugging session is shown in this window.
Inspecting the State of the Application
The Quote_1 application prompts you for input in the External I/O
window.
Enter a customer name after the "Enter customer name:" prompt.
The application stops at the function breakpoint you set earlier.
Open the Breakpoints window by choosing Window > Debugging >
Breakpoints. The window lists the two breakpoints you set earlier.
The function
breakpoint is displayed in bold.
In the
customer.cc file, the green program counter error appears on top of
the breakpoint icon on the first line of the GetDiscount
function.
Click the Call Stack tab. The call stack shows three frames.
Click the Local
Variables tab and note that one variable is displayed. Click the expand icon
(small box with plus sign) to
expand the structure.
Click the Continue button. The GetDiscount
function is executed, printing the customer discount to the External I/O
window. Then you are prompted for input.
Enter the input in response to the prompts. The program stops at the next
breakpoint, the line breakpoint you set earlier. Click the
Local Variables tab and note the long list of local variables.
Click the Call
Stack tab and note that there are only two frames in the stack.
Click the Continue button and continue entering input in response to the
prompts in the External I/O window until the program is completed. When you
enter Q to exit the program, the External I/O window closes and your
debug session ends. To end the debug session before the
execution of the program was complete, you could right-click the session in
the Sessions tab, and choose Finish, or choose Run > Finish Debugger Session.