Getting Started With the NetBeans C/C++ Development Pack
The following short tutorial takes you through some of the basic steps of developing a C or C++ application using the NetBeansTM C/C++ Development Pack. We will create a simple C Application project and a project with an existing makefile. And then we will use some of the advanced editing features to view and edit source files.
This tutorial is designed to get you going as quickly as possible with C and C++ development using the NetBeans IDE. The document covers the following topics:
- Creating C and C++ Projects
- Creating a C/C++ Application Project
- Creating an Application Project
- Switching Between the Logical View and the Physical View of the Project
- Adding Files and Folders to Your Project
- Adding New Files to Your Project
- Adding More New Files to Your Project
- Adding Existing Files to Your Project
- Setting Project Properties
- Managing Configurations
- Setting Source File Properties
- Setting the Main Project
- Building Your Project
- Compiling a Single File
- Running a Project
- Creating a C/C++ Project With an Existing Makefile
- Editing Source Code Files
Creating C and C++ Projects
The NetBeans C/C++ Development Pack lets you create C and C++ Application and Library projects with generated makefiles, as well as C and C++ projects that have existing makefiles.
In this tutorial, we will create a C Application project, add files and folders to the project, set project properties, and build the project. We will also create a project with an existing makefile.
Creating a C/C++ Application Project
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 an Application Project
- Open the New Project wizard by choosing File > New Project.
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.
Adding Files and Folders to Your Project
You can add logical folders to your project.
- Right-click the project node of your Application project (Application1 if you kept the default name) and choose New Folder. A new logical folder is added to the project.
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 > File/Folder.
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 > File/Folder.
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.
- 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.
Do not use New > File/Folder 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 window contains build and configuration information for your project. To open the Project Properties dialog box:
- Right-click the project node of the Application 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.
Note: Early Access 1 does not have debugging capabilities. However, we still provide a Debug configuration so you can debug from the command line.
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.
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.
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 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.
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 with Existing Makefile.
Running a Project
The args 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 New > New Project.
Creating a C/C++ Project with an Existing Makefile
With a C/C++ Project with Existing Makefile, the IDE relies on your existing makefile for instructions on how to compile and run your application.
Creating a Project With an Existing Makefile
- Open the New Project wizard by choosing File > New Project.
Working Directory: ../loki-0.1.4/src Build Command: make -f Makefile Clean Command: make -f Makefile clean Output: blankClick Next.
The project is created and opened in the Project window. You have created a project that is a thin wrapper around an existing makefile.
Building and Rebuilding Your Project
To build a project:
- Right-click the project node of the project and choose Build.
To rebuild a 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.
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 Loki application project, open the Singleton.cpp
file in the src folder.
Click the collapse icon in the left margin to fold the code of one of the
methods.
Mouse over the {...} symbol to the right of the folded block to
display the code in the block.
Using the Class View
The Class View lets you see all of the classes in your project, and the
members and fields for each class.
Click here for larger
screenshot
- Choose Window > Class View to open the Class View, which displays
the Loki project.
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:
- Type uns followed by a space. uns expands to unsigned.
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.
- Type (|. The code automatically completes to (|).
foo () {
|
}
