Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

 Apache NetBeans

Latest release

Apache NetBeans 12.4


Find out more (/download/nb124/nb124.html)

C/C++ Projects Quick Start Tutorial


This tutorial needs a review. You can open a JIRA issue (https://issues.apache.org/jira/projects/NETBEANS/issues), or
edit it in GitHub (https://github.com/apache/netbeans-
 website/blob/master/netbeans.apache.org/src/content/kb/docs/cnd/quickstart.asciidoc) following these contribution
guidelines. (/kb/docs/contributing.html)

Contributed by Ann Rice and maintained by Susan Morgan March 2014 Requirements
[Revision number: V8.0-1]
Creating C and C++ Projects
The following short tutorial takes you through the basic steps of creating and
Creating a C/C++ Application
con guring a C or C++ project in NetBeans IDE.
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


Figure 1. Content on this page applies to the NetBeans IDE 7.2 and more recent
Project
Requirements Adding More New Files to

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

Adding Existing Files to Your


Project

Setting Project Properties

Managing Con gurations

Setting Source File Properties

Building and Rebuilding Your


Project

Compiling a Single File

Running a Project

Creating a C/C++ Project With


Existing Sources

Creating a Project With


Existing Sources

Creating a C/C++ Project from


a Binary File
Building and Rebuilding Your
Project

[[Next Steps]]

Software or Resource Version Required

NetBeans IDE + version 7.2 or more recent with NetBeans C/C++ plugin+
(https://netbeans.org/downloads/index.html)

Java Developer Kit (JDK) version 6, 7, or 8


(http://www.oracle.com/technetwork/java/javase/downloads/index.html)

C and C++ compilers, make , gdb Con guring the NetBeans IDE for C/C+/Fortran+
(../../../community/releases/80/cpp-setup-instructions.html)

See the NetBeans IDE Installation Instructions (../../../community/releases/80/install.html) and + Con guring the
NetBeans IDE for C/C+/Fortran (../../../community/releases/80/cpp-setup-instructions.html) 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 make les, 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 de ning and using remote hosts, see De ning Your C/C+ Project’s Development Environment+
(development-environment.html).

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 make le 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 several types of new projects. Select C/C++ Application and click Next.
4. 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.

5. 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 les
and does not re ect where the les 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 les are just for reference and are convenient when you have a project with an existing make le.

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 les and folders as they are
stored on disk.

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

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 les and folders to an existing folder. Logical folders can be nested.

Adding New Files to Your Project


You can add new les to your project.

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

3. Click Finish.

The newfile.cpp le is created on disk in the directory speci ed in the wizard and added to the Source Files folder.
You can add any kind of le to this folder, not only source les.

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 newfile in the File Name eld.
3. Click Finish.
The newfile.h le is created on disk in the directory speci ed in the wizard and added to the Header Files folder.

Adding Existing Files to Your Project


You can add existing les to your project in two ways:

Right-click the Source Files folder and choose Add Existing Item. You can point to an existing le on disk using the
Select Item dialog box and add the le 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 les.

Do not use New menu item to add existing items. The Name and Location panel will tell you the le already exists.

Setting Project Properties


When the project is created, it has two con gurations: Debug and Release. A con guration is a collection of settings
used for the project, which allows you to easily switch many settings at once when you select a con guration. The
Debug con guration builds a version of your application that includes debug information. The Release con guration
builds an optimized version.

The Project Properties dialog box contains build and con guration 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 con guration 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 con gurations of the project. When you set Build, Run, or Debug properties, you are setting properties in
the currently selected con guration.

Managing Con gurations


Properties changed in the Project Properties window are stored in the make le for the current con guration. You
can edit the default con gurations or create new ones. To create a new con guration:

1. Click the Manage Con gurations button in the Project Properties dialog box.
2. In the Con gurations dialog box, select the con guration that most closely matches your desired con guration.
In this case, select the Release con guration and click the Duplicate button. Then click Rename.
3. In the Rename dialog box, rename the con guration to PerformanceRelease. Click OK.
4. Click OK in the Con gurations dialog box.
5. In the left panel, select the C Compiler node.
6. In the Project Properties dialog box, note that the PerformanceRelease con guration is selected in the
Con guration drop-down list.
7. In the property sheet in the right panel, change the Development Mode from Release to PerformanceRelease.
Click OK.

You have created a new con guration that will compile the application with a di erent 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 les in the project.
You can also set some properties on individual les.

1. Right-click the newfile.cpp source le in the Projects tab and choose Properties.
2. Click the General category and see that you can specify a di erent compiler or other tool to build this le. You
can also use a checkbox to exclude the le from the build of the currently selected project con guration.
3. Click the C++ Compiler category and see that you can override the project compiler settings and other properties
for this le.
4. Cancel the File Properties dialog box.

Building and Rebuilding Your Project


To build your project:

1. Right-click the project node and choose Build, and the project builds. The build output is shown in the Output
window.
2. Switch the con guration from Debug to PerformanceRelease in the con guration drop-down list in the main
toolbar. Now the project will be built using the PerformanceRelease con guration.
3. 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 les 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 les and executables from di erent con gurations separate, so you do not
have to worry about mixing les from multiple con gurations.

Compiling a Single File


To compile a single source le:

Right-click on the main.cpp le and choose Compile File. Only this le is compiled.

Single le compilation is not supported for the project type C/C++ Project With Existing Sources.

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 con guration.
Then you 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 Run Command text eld, type 1111 2222 3333. Click OK.

8. 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 make le 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 les.

Creating a Project With Existing Sources


1. If you are running the IDE on Windows, install the Pthreads library from + http://sourceware.org/pthreads-
win32 + (http://sourceware.org/pthreads-win32).

For convenience, you can use this direct link (ftp://sourceware.org/pub/pthreads-win32/pthreads-2005-03-08.exe) to


the pthreads-2005-03-08.exe installer.

Extract the pthreads library in your Windows user directory.

2. Download the loki-0.1.7 library from + http://sourceforge.net/projects/loki-lib +


(http://sourceforge.net/projects/loki-lib).

3. Uncompress loki-0.1.7 in a directory of your choice.

4. Open the New Project wizard by choosing File > New Project.
5. Select the C/C++ category.

6. Select C/C++ Project With Existing Sources and click Next.

7. 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 the loki-0.1.7 directory. Click Select.

8. Use the default Con guration Mode, Automatic. Click Finish.

The project is created and opened in the Projects tab, and the IDE automatically runs the Clean and Build actions
speci ed in the existing Make le. The project is also automatically con gured for code assistance.

Creating a C/C++ Project from a Binary File


You can create a C/C++ project from an existing binary le.

1. Choose File > New Project.


2. Select the C/C++ category.
3. 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 le from which you want to
create a project.
The root directory for the source les from which the binary was built is lled in automatically. By default, only the
source les 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.

5. Click Next.

6. On the Project Name and Location page, you can choose the name and location of the project. Click Finish.

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+ (navigating-editing.html) for a tutorial on using the advanced
navigation and editing features in NetBeans IDE to view and modify your source code.

See the link:https://netbeans.org/kb/trails/cnd.html[+C/C Learning Trail+] for more articles about developing with
C/C/Fortran in NetBeans IDE.

link:mailto:[email protected]?subject=Feedback:%20C/C+%20Projects%20Quick%20Start%20-
%20NetBeans%20IDE%207.4%20Tutorial[+Send Feedback on This Tutorial]

 (https://www.facebook.com/NetBeans)  (https://twitter.com/netbeans)

 (https://github.com/apache/netbeans)  (https://www.youtube.com/user/netbeansvideos)

 (https://tinyurl.com/netbeans-slack-signup/)

 (https://issues.apache.org/jira/projects/NETBEANS/summary)

 See this page in GitHub. (https://github.com/apache/netbeans-


website/blob/master/netbeans.apache.org/src/content/kb/docs/cnd/quickstart.asciidoc)
(https://www.apache.org/) (https://www.apache.org/events/current-event.html)

ABOUT COMMUNITY PARTICIPATE GET HELP DOWNLOAD


(/ABOUT/INDEX.HTML)
(/COMMUNITY/INDEX.HTML)
(/PARTICIPATE/INDEX.HTML)
(/HELP/INDEX.HTML)
(/DOWNLOAD/NB
Who's Who Mailing lists Submitting Pull Documentation Releases
(https://netbeans.apache.org/community/who.html)
(/community/mailing- Requests (/help/index.html#documentation)
(/download/index.htm
lists.html) (/participate/submit-
Thanks Wiki Plugins
pr.html)
(https://www.apache.org/foundation/thanks.html)
Becoming a (/wiki/index.asciidoc) (https://plugins.netbea
committer Reporting Issues
Sponsorship Community Building from
(/community/committer.html)
(/participate/report-
(https://www.apache.org/foundation/sponsorship.html) Support source
NetBeans Events issue.html) (/help/index.html#support)
(/download/index.htm
Security
(/community/events.html)
Improving the
(https://www.apache.org/security/) Commercial Previous releases
Apache Events documentation Support (/download/index.htm
(/participate/index.html#documentation)
(https://www.apache.org/events/current- (/help/commercial-
event.html) support.html)

Copyright © 2017-2020 The Apache Software Foundation (https://www.apache.org).

Licensed under the Apache license (https://www.apache.org/licenses/), version 2.0

Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache
NetBeans logo are trademarks of The Apache Software Foundation
(https://www.apache.org).

Oracle and Java are registered trademarks of Oracle and/or its a liates.

You might also like