corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

C/C++ Projects Quick Start Tutorial

The following short tutorial takes you through the basic steps of creating and configuring a C or C++ project in NetBeans IDE 6.5.

Contents

Content on this page applies to NetBeans IDE 6.5

Requirements

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE version 6.5 with NetBeans C/C++ pack
Java Developer Kit (JDK) version 6 or version 5
Windows platform:
GNU C and C++ compilers, make, gdb

www.cygwin.com
www.mingw.org
Solaris platforms:
GNU C and C++ compilers
Sun C and C++ compilers
Solaris make and gmake
gdb

3.4.3
5.9 (Sun Studio 12 release)
3.81
6.6 or 6.8
Linux platforms:
Red Hat Fedora Core:
gcc and g++
gdb
GNU make

Ubuntu:
gcc and g++
gdb
GNU make

4.1.2
6.6 or 6.8
3.81


4.1.2
6.6 or 6.8
3.81
Mac OS X platforms:
gcc and g++
gdb
GNU make
Xcode bundle

4.0.1
6.3.50
3.80
6.5

See the NetBeans IDE 6.5 Installation Instructions and the Installing and Configuring C/C++ Support for information on 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 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.

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

  1. Open the New Project wizard by choosing File > New Project.
  2. In the wizard, select the C/C++ category.
  3. 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.

  4. Screenshot of the New Project
           Wizard

  5. 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.
  6. 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.

  1. Select the Files tab. This window shows the physical view of your project. It displays files and folders as they are stored on disk.

  2. Screenshot of the Files tab

  3. Select the Projects tab. This window shows the logical view of your project.

  4. Screenshot of the Projects tab

Adding Files and Folders to Your Project

You can add logical folders to your project.

  1. Right-click the project node of your Application project and choose New Logical Folder. A new logical folder is added to the project.
  2. 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.

  1. Right-click the Source Files folder and choose New > Main C File.
  2. On the Name and Location page of the New File dialog box, type main in the File Name field.

    Screenshot of the New File dialog
              box

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

  1. Right-click the Header Files folder and choose New > C Header File.
  2. On the Name and Location page of the New File dialog box, type file in the File Name field.
  3. 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.

  • Screenshot of the Project
              Properties dialog box

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:

  1. Click the Manage Configurations button in the Project Properties dialog box.
  2. 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 Copy button. Then click Rename.
  3. In the Rename dialog box, rename the configuration to PerformanceRelease. Click OK.
  4. Click OK in the Configurations dialog box.
  5. In the Project Properties dialog box, note that the PerformanceRelease configuration is selected in the Configuration drop-down list.
  6. In the left panel, select the C Compiler node.
  7. 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.

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

  1. Choose Run > Build Main Project and the project builds. The build output is shown in the Output window.
  2. 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.
  3. Choose Run > 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 Arguments project, set some arguments, and run the project:

  1. Choose File > New Project.
  2. In the project wizard, expand the Samples category.
  3. Select the C/C++ subcategory, then select the Arguments project. Click Next, then click Finish.
  4. Right-click the Arguments_1 project node and choose Build. The project builds.
  5. Right-click the Arguments_1 project node and choose Properties.
  6. In the Project Properties dialog box, select the Run node.
  7. In the Arguments text field, type 1111 2222 3333. Click OK.

  8. Screenshot of the Project Properties
           dialog box

  9. Choose Run > Run Main Project. The application runs. Your arguments are displayed in an external terminal 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.

Creating a Project From Existing Code

  1. Download the loki-0.1.6 library from http://sourceforge.net/projects/loki-lib.
  2. Uncompress loki-0.1.6 in a directory of your choice.
  3. Open the New Project wizard by choosing File > New Project.
  4. Select the C/C++ category.
  5. Select C/C++ Project From Existing Code and click Next.
  6. 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.

    Screen shot of Makefile and
           Build Tool page of Project Wizard

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

    Screen shot of Makefile and
           Build Actions page of Project Wizard

  8. 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. Click Next.

  9. Screen shot of Source File
           Folders page of Project Wizard

  10. On the Code Assistance Configuration page, click Next.

  11. Screen shot of
           Code Assistance Configuration page of Project Wizard

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

  13. Screen shot of Project Name
           and Location page of Project Wizard

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.

To rebuild the project:

  • Right-click the project node of the project and choose Clean and Build.

Next Steps

See Editing and Navigating C/C++ Source Files for a tutorial on using the advanced navigation and editing features in NetBeans IDE 6.5 to view and modify your source code.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems