Contributed and maintained by Ann
Rice
October 2007 [Revision number: V6.0-2]
The following short tutorial takes you through some of the basic steps of developing a C or C++ application using NetBeans IDE 6.0 with C/C++ support.
Contents
|
You must have the following software installed before you can start C or C++ development:
See the NetBeans IDE 6.0 Installation Instructions and the Installing and Configuring C/C++ Support for information on downloading and installing the required software.
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.
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.
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.
A project has both a logical and a physical view. You can switch between the logical view and the physical view of your project.
You can add logical folders to your project.
You can add both files and folders to an existing folder. Logical folders can be nested.
You can add new files to your project.
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.
The file is created on disk in the directory specified in the wizard and added to the Header Files folder.
You can add existing files to your project in two ways:
Do not use New menu item to add existing items. The Name and Location panel will tell you the file already exists.
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:
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.
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:
You have created a new configuration that will compile the application with a different set of options.
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.
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:
To build your project:
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.
To compile a single source file:
Note: Single file compilation is not supported for the project type C/C++ Project From Existing Code.
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:
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.
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.
The project is created and opened in the Project window. You have created a project that is a thin wrapper around existing code.
To build the project:
To rebuild the project:
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:
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.
The Classes window lets you see all of the classes in your project, and the members and fields for each class.
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:
To navigate to an element of the file displayed in the Navigator window:
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.
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.
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.
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 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:
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.
You can create and manipulate breakpoints in your code at any time.