C/C++ Projects Quick Start Tutorial
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
- Requirements
- Creating C and C++ Projects
- Creating a C/C++ 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
- Building and Rebuilding Your Project
- Compiling a Single File
- Running a Project
- Creating a C/C++ Project With Existing Sources
- Next Steps
The following short tutorial takes you through the basic steps of creating and configuring a C or C++ project in NetBeans IDE.
Requirements
To follow this tutorial, you need the following software and resources.
Software or Resource | Version Required |
---|---|
NetBeans IDE |
|
Java Developer Kit (JDK) |
|
C and C++ compilers, |
See the NetBeans IDE Installation Instructions and Configuring the NetBeans IDE for C/C++/Fortran for information about downloading and installing the required software.
Sample Projects
If you want to just play with some sample projects provided in the IDE, choose File > New Project, then open the Samples category 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 with existing sources.
You can build, run, and debug your project on the local host (the system from which you started the IDE) or on a remote host running a UNIX® operating system. For information on specifying the tool collection for your project and on defining and using remote hosts, see Defining Your C/C++ Project’s Development Environment.
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 several types of new projects. 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 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.
Most commands that you run on a project are available in the Projects tab, not the Files tab.
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 > C++ Source File.
-
On the Name and Location page of the New File dialog box, type
newfile
in the File Name field.
-
Click Finish.
The newfile.cpp
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 > C++ Header File.
-
On the Name and Location page of the New File dialog box, type
newfile
in the File Name field. -
Click Finish.
The newfile.h
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 a collection of settings used for the project, which allows you to easily switch many settings at once when you select a configuration. 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
CppApplication_1
project and choose Properties.
You can modify the build tool defaults, 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. When you set General properties, you are setting them in all configurations of the project. When you set Build, Run, or Debug properties, 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 that most closely matches your desired configuration. In this case, select the Release configuration and click the Duplicate 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 left panel, select the C Compiler node.
-
In the Project Properties dialog box, note that the PerformanceRelease configuration is selected in the Configuration drop-down list.
-
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 also set some properties on individual files.
-
Right-click the
newfile.cpp
source file in the Projects tab and choose Properties. -
Click the General category and see that you can specify a different compiler or other tool to build this file. You can also use a checkbox to exclude the file from the build of the currently selected project configuration.
-
Click the C++ Compiler category and see that you can override the project compiler settings and other properties for this file.
-
Cancel the File Properties dialog box.
Building and Rebuilding Your Project
To build your project:
-
Right-click the project node and choose Build, 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.
-
Right-click the project node and choose Build, and the project builds. The build output is shown in the Output window.
To rebuild your project:
-
Right-click the project node and choose Clean and Build to do a complete rebuild after removing previous build products.
-
Right-click the project node and choose Build to do an incremental build. Previous build products are retained if their source files have not changed.
You can build, clean, or both clean and build the project by choosing actions from the Run menu or using tool bar buttons. 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.
Running a Project
To see how to run a project, you will use the IDE’s sample Arguments
project. The Arguments
program prints command-line arguments. Before running the program, you will set some arguments in the current configuration. Then you will run the program.
To create the Arguments
project, set some arguments, and run the project:
-
Choose File > New Project.
-
In the project wizard, expand the Samples category.
-
Select the C/C++ subcategory, then select the
Arguments
project. Click Next, then click Finish. -
Right-click the
Arguments_1
project node and choose Build. The project builds. -
Right-click the
Arguments_1
project node and choose Properties. -
In the Project Properties dialog box, select the Run node.
-
In the Run Command text field, type 1111 2222 3333. Click OK.
-
Right-click the project node and choose Run.The application runs. Your arguments are displayed in the Output window.
Creating a C/C++ Project With Existing Sources
When creating a C/C++ Project With Existing Sources, the IDE relies on your existing makefile for instructions on how to compile and run your application.
In this exercise, you download and install the sources for the open source Loki C++ library. Loki requires the Pthreads library to build, which is available by default on Linux, Solaris, and Mac OS X. If you are using Windows, you must download the Pthreads library before you can create a project using the Loki source files.
Creating a Project With Existing Sources
-
If you are running the IDE on Windows, install the Pthreads library from http://sourceware.org/pthreads-win32.
For convenience, you can use this direct link to the pthreads-2005-03-08.exe installer.
Extract the pthreads library in your Windows user directory.
-
Download the
loki-0.1.7
library from http://sourceforge.net/projects/loki-lib.
-
Uncompress
loki-0.1.7
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 With Existing Sources and click Next.
-
On the Select Mode page, click the Browse button. In the Select Project Folder dialog box, navigate to the directory where you saved
loki-0.1.7
. Select theloki-0.1.7
directory. Click Select.
-
Use the default Configuration Mode, Automatic. Click Finish.
The project is created and opened in the Projects tab, and the IDE automatically runs the Clean and Build actions specified in the existing Makefile. The project is also automatically configured for code assistance.
Creating a C/C++ Project from a Binary File
You can create a C/C++ project from an existing binary file.
-
Choose File > New Project.
-
Select the C/C++ category.
-
Select C/C++ Project from Binary File and click Next. 4. On the Select Binary File page of the New Project wizard, click the Browse button. In the Select Binary File dialog box, navigate to the binary file from which you want to create a project.
The root directory for the source files from which the binary was built is filled in automatically. By default, only the source files from which the binary was built are included in the project. By default, dependencies are included in the project. The shared libraries required by the project are automatically listed.
-
Click Next.
-
On the Project Name and Location page, you can choose the name and location of the project. Click Finish.
Next Steps
See Editing and Navigating C/C++ Source Files for a tutorial on using the advanced navigation and editing features in NetBeans IDE to view and modify your source code.
See the C/C++ Learning Trail for more articles about developing with C/C++/Fortran in NetBeans IDE.