Source code file content

Revision: 2

import
» Project Revision History

» Checkout URL

web-content / trunk / cnd-tutorial.html

Size: 19082 bytes, 1 line
<h1>Getting Started With the NetBeans C/C++ Development Pack</h1>
<p>The following short tutorial takes you through some of the basic steps of 
developing a C or C++ application using the NetBeans<small><sup>TM</sup></small> 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.
</p>
<p>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:
<ul>
<li><a href="#CreatingProjects">Creating C and C++ Projects</a></li>
<li><a href="#CreatingApplication">Creating a C/C++ Application Project</a>
<ul>
<li><a href="#CreatingApp">Creating an Application Project</a></li>
<li><a href="#SwitchingViews">Switching Between the Logical View and the Physical View of the
Project</a></li>
<li><a href="#AddingFilesandFolders">Adding Files and Folders to Your Project</a></li>
<li><a href="#AddingNewFiles">Adding New Files to Your Project</a></li>
<li><a href="#MoreNewFiles">Adding More New Files to Your Project</a></li>
<li><a href="#AddingExistingFiles">Adding Existing Files to Your Project</a></li>
<li><a href="#ProjectProperties">Setting Project Properties</a></li>
<li><a href="#Configurations">Managing Configurations</a></li>
<li><a href="#FileProperties">Setting Source File Properties</a></li>
<li><a href="#SettingMainProject">Setting the Main Project</a></li>
<li><a href="#BuildingProject">Building Your Project</a></li>
<li><a href="#CompilingFile">Compiling a Single File</a></li>
</ul>
</li>
<li><a href="#Running">Running a Project</a></li>
<li><a href="#ExistingMakefile">Creating a C/C++ Project With an Existing
Makefile</a>
<ul>
<li><a href="#CreateMakefileProject">Creating a Project With an Existing
Makefile</a></li>
<li><a href="#BuildingRebuilding">Building and Rebuilding Your Project</a></li>
</ul>
</li>
<li><a href="#Editing">Editing Source Code Files</a>
<ul>
<li><a href="#CodeFolding">Folding Blocks of Code in C and C++ Files</a></li>
<li><a href="#ClassView">Using the Class View</a></li>
<li><a href="#CodeTemplates">Using Code Templates</a></li>
<li><a href="#PairCompletion">Using Pair Completion</a></li>
</ul>
</li>
</ul>

</p>

<h2><a name="CreatingProjects"></a>Creating C and C++ Projects</h2>
<p>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.
</p>
<p>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.  
</p>

<h2><a name="CreatingApplication"></a>Creating a C/C++ Application Project</h2>
<p>
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.
</p>
<div class="indent">
<h3 class="tutorial"><a name="CreatingApp"></a>Creating an Application Project</h3>
<ol>
    <li>Open the New Project wizard by choosing File &gt; New Project.</li><br>
    <li>In the wizard, select the C/C++ Development category.</li><br>
    <li>
	The wizard gives you a choice of four types of new projects: 
	C/C++ Application,
	C/C++ Dynamic Library, C/C++ Static Library, and C/C++ Project with Existing Makefile. 
	Select C/C++ Application and click Next.
    </li><br>
    <li>
	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.
    </li><br>
    <li>Click Finish to exit the wizard.</li>
</ol>
<p>
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.
</p>
<p>
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. 
</p>
<div class="indent">
<h3 class="tutorial"><a name="SwitchingViews"></a>Switching Between the Logical View and the Physical View of the Project</h3>
<p>A project has both a logical and a physical view.
You can switch between the logical view and the physical view of your
project.
</p>
<ol>
    <li>
	Select the Files tab. This window shows the physical view of your
	project. It displays files and folders as they are stored on disk.
    </li><br>
    <li>
	Select the Project tab. This window shows the logical view of 
	your project. 
    </li>
    </ol>

<h3 class="tutorial"><a name="AddingFilesandFolders"></a>Adding Files and Folders to Your Project</h3>
<p>You can add logical folders to your project.
</p>
<ol>
    <li>
	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.
    </li><br>
    <li>
	Right-click the new logical folder and select Rename. Type the name
	you would like to give the new folder.
    </li>

</ol>
<p>You can add both
files and folders to an existing folder. Logical folders can be nested.

<h3 class="tutorial"><a name="AddingNewFiles"></a>Adding New Files to Your Project</h3>
<p>You can add new files to your project.
</p>
<ol>
    <li>Right-click the Source Files folder and choose New &gt; File/Folder.</li><br>
    <li>
	On the Choose File Type page of 
	the New File dialog box, select the C Files category.
	and the Main C File file type. Click Next.
    </li><br>

    <li>On the Name and Location page, type <tt>main</tt> in the File Name field.</li><br>
    <li>Click Finish.</li>
</ol>
<p>
The <tt>main.c</tt> 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. 
</p>
<h3 class="tutorial"><a name="MoreNewFiles"></a>Adding More New Files to Your Project</h3>
<ol>
    <li>Right-click the Header Files folder and choose New &gt; File/Folder.</li><br>
    <li>
	On the Choose File Type page of the New File dialog box, select the C Files
	category and the C Header File file type. Click Next.
    </li><br>
    <li>On the Name and Location page, type <tt>file</tt> in the File Name field.</li><br>

    <li>Click Finish.</li>
</ol>
<p>
The file is created on disk in the directory specified in the wizard 
and added to the Header Files folder.
</p> 
<h3 class="tutorial"><a name="AddingExistingFiles"></a>Adding Existing Files to Your Project</h3>
<p>
You can add existing files to your project.
</p>
<ul>
    <li>
	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.
    </li>
</ul>

<p>
Do not use New &gt; File/Folder to add existing items. The Name and Location panel
will tell you the file already exists.
</p>
<h3 class="tutorial"><a name="ProjectProperties"></a>Setting Project Properties</h3>
<p>
<p>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.
</p>
<p>The Project Properties window contains build and configuration information
for your project. To open the Project Properties dialog box:
</p>
<ul>
    <li>
	Right-click the project node of the 
	Application project and choose Properties.
    </li>
</ul>

<p>
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.
<p><font color="red">
Note: Early Access 1 does not have debugging capabilities. However, we still provide
a Debug configuration so you can debug from the command line.</font>
</p>
<h3 class="tutorial"><a name="Configurations"></a>Managing Configurations</h3>
<p>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:
</p>
<ol>
    <li>
	Click the Manage Configurations button in the Project Properties
	dialog box.
    </li>
    <br>
    <li> 
	In the Configurations dialog box, select the configuration which most
	closely matches your desired configuration. In this case, select the
	Release configuration and click the Copy button. Then click Rename.
    </li>
    <br>
    <li>
	In the Rename dialog box, rename the 
	configuration to PerformanceRelease. Click OK. 
    </li>
    <br>
    <li>Click OK in the Configurations dialog box.</li>
    <br>
    <li>
	In the Project Properties dialog box, note that the PerformanceRelease
	configuration is selected in the Configuration drop-down list.
    </li>
    <br>
    <li>
	In the left panel, expand the C/C++ node, 
	and then the GNU C Compiler node, and select the General node. 
    </li>
    <br>
    <li>
	In the property sheet in the right panel, change the Development
	Mode from Release to PerformanceRelease. Click OK.
    </li>
</ol>
<p>
You have created a new configuration that will compile the application with a 
different set of options.
</p>

<h3 class="tutorial"><a name="FileProperties"></a>Setting Source File Properties</h4>
<p>
<p>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.
</p>
<ol>
    <li>
	Right-click the <tt>main.c</tt> 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.
    </li><br>

    <li>Cancel the Project Properties dialog box.</li>
</ol>

<h3 class="tutorial"><a name="SettingMainProject"></a>Setting the Main Project</h3>
<p>
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?
</p>
<p>
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.
<p>
To change the main project in the IDE:
<ul>
    <li>
	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.
    </li>

</ul>

<h3 class="tutorial"><a name="BuildingProject"></a>Building Your Project</h3>
<p>To build your project:
<ol>
    <li>
	Choose Build &gt; Build Main Project and the project  
	builds. The build output is shown in the Output window.
    </li><br>
    <li>
	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.
    </li><br>

    <li>
	Choose Build &gt; Build Main Project and the project 
	builds. The build output is shown in the Output window.
    </li>
</ol>
<p>
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.
</p>
<h3 class="tutorial"><a name="CompilingFile"></a>Compiling a Single File</h3>
<p>
To compile a single source file:
</p>
<ul>
    <li>

	Right-click on the <tt>main.c</tt> file and choose 
	Compile File. Only this file is compiled.
    </li>
</ul>
<p>
Note: Single file compilation is not supported for the project type
C/C++ Project with Existing Makefile.
</p>
</div>
<h2"><a name="Running"></a>Running a Project</h2>
<p>
The <tt>args</tt> program prints command-line arguments. Before running the
program, we will set some arguments in the current configuration. Then we will run
the program.
</p>
<p>
To create the <tt>Args</tt> project, set some arguments, and run the project:
</p>
<ol>
<li>Choose New &gt; New Project.</li>
<br>
<li>In the project wizard, expand the Samples category and the C/C++
Development subcategory.</li>
<br>
<li>Select the C/C++ subcategory, then select the <tt>Args</tt> project. Click
Next, then click Finish.
</li>
<br>
<li>Right-click the <tt>Args</tt> project node and choose Build Project. The
project builds.</li>
<br>
    <li>Right-click the <tt>Args</tt> project node and choose Properties.</li><br>
    <li>In the Project Properties dialog box, select the Running node.</li>
    <br>
    <li>
	In the Arguments text field, type 1111 2222 3333. Click OK.
    </li><br>
    <li>
	Choose Run &gt; Run Main Project. The application runs.
	Your arguments are displayed in the Output window.
    </li>
</ol>
<h2><a name="ExistingMakefile"></a>Creating a C/C++ Project with an Existing Makefile</h2>
<p>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. 
</p>
<div class="indent">
<h3 class="tutorial"><a name="CreateMakefileProject"></a>Creating a Project With an Existing Makefile</h3>
<ol>
    <li>Open the New Project wizard by choosing File &gt; New Project.
    </li>
    <br>
    <li>Select the C/C++ Development category.</li>
    <br>
    <li>Select C/C++ Project with Existing Makefile and click Next.</li>
    <br>
    <li>
	On the Project Name and Location page, type <tt>Loki<i><b>n</b></i></tt> in the Project Name 
	field. Use the defaults for all of the other fields. Click Next.
    </li>
    <br>
    <li>
	On the Build Actions page, click the Browse button for the Working Directory 
	field. In the Select Working Directory dialog box, navigate to the
	<tt>loki-0.1.4</tt>. Select the <tt>src</tt>
         directory (be sure not to double click the <tt>src</tt> directory as that will
	descend into the <tt>src</tt> directory rather than select it). 
	Click Select.
    </li>
    <br>
    <li>
	Before you click Next in the wizard, make sure the fields on the Build Actions
	page show the following:
	<pre>

	    Working Directory: ../loki-0.1.4/src
	    Build Command: make -f Makefile
	    Clean Command: make -f Makefile clean
	    Output: blank
	</pre>
	Click Next.
    </li>
    <br>
    <li>
	On the Source File Folders page, click Add. Navigate one level up, 
	select the <tt>loki-0.1.4</tt> directory and click Select.
	Click Finish and the directory is added to the Important Files
	folder in the Project window.
    </li>
    <br>
    <li>In the Projects tab, right-click the <tt>test</tt> folder in the project and
    choose Remove to delete unneeded files.
    </li>

</ol>
<p>
The project is created and opened in the Project window. You have created a
project that is a thin wrapper around an existing makefile.
</p>
<h3><a name="BuildingRebuilding"></a>Building and Rebuilding Your Project</h3>
<p>To build a project:
<ul>
    <li>Right-click the project node of the project and choose Build.</li>
</ul>
<p>To rebuild a project:
<ul>
    <li>Right-click the project node of the project and choose Clean and Build 
    Project.</li>
</ul>


</div>
<h2><a name="Editing"></a>Editing Source Files</h2>
<p>The C/C++ Development Pack provides advanced editing features to help you 
in viewing and modifying your source code.</p>
<div class="indent">
<h3 class="tutorial"><a name="CodeFolding"</a>Folding Blocks of Code in C and C++ Files</h2>
<p>
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.
</p>
<ol>
<li>In the <tt>Loki</tt> application project, open the <tt>Singleton.cpp</tt>
file in the <tt>src</tt> folder.
</li>
<br>
<li>Click the collapse icon in the left margin to fold the code of one of the
methods.</li>
<br>
<li>Mouse over the <tt>{...}</tt> symbol to the right of the folded block to
display the code in the block.</li>
</ol>
<h3 class="tutorial"><a name="ClassView"></a>Using the Class View</h3>
<p>
The Class View lets you see all of the classes in your project, and the 
members and fields for each class.</p>
<img src="cndscreenshotsmall.png">
<a href="largescreenshot.html" target="newWindow">Click here for larger
screenshot</a>
<ol>
  <li>Choose Window &gt; Class View to open the Class View, which displays 
  the <tt>Loki</tt> project.</li>
  <br>
  <li>Expand the <tt>Loki</tt> node, then expand the <tt>Random</tt> node. All 
  classes in the project are listed.
  </li>
  <br>
  <li>Double-click on the <tt>seed</tt> class to open the 
<tt>Random</tt> header file.</li>
</ol>
<h3 class="tutorial"><a name="CodeTemplates"></a>Using Code Templates</h3>
<p>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
<tt>Singleton.cpp</tt> file:
</p>
<ol>
  <li> Type <tt>uns</tt> followed by a
space. <tt>uns</tt> expands to <tt>unsigned</tt>.</li>
  <br>
   <li>Type <tt>iff</tt> followed by a space.
<tt>iff</tt> expands to <tt>if () {}</tt>.</li>
   <br>
   <li>Type <tt>ife,</tt> followed by a space.
<tt>ife</tt> expands to <tt>if () {} else {}</tt>.
  </li>
  <br>
  <li>Type <tt>fori</tt> followed by a
space. <tt>fori</tt> expands to <tt>for (int i = 0; i &lt;
; i++) {}</tt>.</li>
</ol>
<h3 class="tutorial"><a name="PairCompletion"></a>Using Pair Completion</h3>
<p>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.
</p>
<ol>
  <li>Type <tt>(|</tt>. The code automatically completes to <tt>(|)</tt>. 
  </li>
  <br>
  <li>Type <tt>foo() {</tt> and press Return. The code automatically 
  completes to :
    <pre>foo () {<br>   |<br>}<br></pre>
  </li>
  <li>Place the cursor  behind a {. The matching character is highlighted.
This technique works with the following paired characters: {} () []. </li>
</ol>
</div>
</body>
</html>


Project Features

About this Project

CND was started in November 2009, is owned by DimaZh, and has 197 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close