How to Create a Project from Existing Code and Set Up Code Assistance
Contributed and maintained by
December 2007 [Revision number: V6.0-1]
Open source projects usually are built by the make utility,
which uses a makefile at build time.
This article explains how to create a NetBeans IDE project based on a makefile.
For more information about working with C/C++ applications in the NetBeans IDE, see the C/C++ Applications Learning Trail page on the
NetBeans web site.
Tutorial Requirements
Before you proceed, make sure you review the requirements in this section.
Prerequisites
This tutorial assumes that you have some programming experience with C and
C++.
Software Needed for This Tutorial
Before you begin, you need to install NetBeans 6.0.
The C/C++ option includes NetBeans IDE 6.0 and C/C++ support.
One of the IDE responsibilities is the construction of the code model.
The code model is important for IDE features, such as:
Code completion
Hyperlinks
Navigation
In order to build the model correctly, the IDE needs information
about the project such as:
The compiled files
Which compiler is used
The user include paths and macros in each compiled file
The header files used
Without this information, IDE cannot build an adequate model,
which will result in IDE features working incorrectly.
Certainly all this information is contained in the makefile.
The problem is that the necessary information is difficult to determine from
the makefile for the following reasons:
Complex open source projects usually have a hierarchical file structure
and the main makefile refers to the makefiles in the subdirectories.
Makefiles frequently use shell script commands.
Sometimes the behavior of the make utility depends on its previous
actions; for example, on the existence of the file created at the previous
step.
The Netbeans IDE provides another method to get the necessary information for the
model based on using compiler commentary or debug information in:
Object files
Static and dynamic libraries
Executable files
How Does This Work?
Let us examine the typical scenario
when creating a new Netbeans IDE project for an open source application.
The scenario begins with downloading an open source project.
We will use the
MySQL project.
The following instructions assume that we have loaded the open source project
and installed necessary software.
Now let's configure the project.
To guarantee the completeness
of compiler commentary, we provide additional compilation flags:
I believe the -g flag is well-known.
This flag tells a compiler to include debug information in object files.
The -g3 modification of this flag makes the debug information more
complete. This flag makes it possible to obtain information about the user
macros.
The -gdwarf-2 flag tells the compiler to use version 2 of the DWARF
debugging information format.
We build the application using the make command.
Basically, we must ensure that the application is compiled with the flags mentioned above.
How to tell the make utility to use the necessary flags depends on the
particular open source project.
Sometimes it is necessary to export environment variables:
For inflexible projects, it is necessary to look at the content of the makefile to
understand how to provide flags.
Now our open source project is successfully built.
So it is possible to create a NetBeans IDE project.
Start the NetBeans IDE.
Create a new project from existing code by choosing File > New Project
> C/C++ > C/C++ Project From Existing Code.
Let us look at the steps in the New Project wizard.
In the second step, the Build Tool page, we have to indicate
where the main makefile of our open source project is located.
Pointing to the correct makefile makes it possible to build the application from the IDE.
The third step deserves additional attention.
On the Build Actions page, you can specify where the executable file (Build
Result) of the
application is located, making it possible to start the application from the IDE.
You can fill in this field later.
At the fifth step, the Code Assistance Configuration page, the wizard
proposes
to setup the IDE project automatically, on the basis of the compiler commentary. Let us examine what occurs in this case.
If at the third step, you indicated where the build result is located, the
wizard uses compiler commentary from the build result.
Otherwise the wizard uses compiler commentary from all object files,
libraries, and executable files located in the folder of open source project.
Using the information obtained from these files, the wizard sets up the IDE project.
The configuration level tells the wizard
how the discovered user include paths and macros will be consolidated.
The compiler commentary contains user include paths and macros for each
compiled file. But for convenience of the further IDE project maintenance, it
is possible to set another level of configuration.
For example, if you select project level configuration, then all user include
paths and macros will be consolidated into the project properties.
As a last step, you select the location of the IDE project.
By default, the IDE proposes to place the project in your home folder,
which makes it possible to completely separate the IDE project from the open source project.
Sometimes this separation is useful due to the version control system.
But nothing prevents you from using a folder in the open source project itself.
The IDE places all information about the IDE project into a special folder
named nbproject.
At the end of the NetBeans IDE project creation, the wizard includes in the
model only the files
that were compiled or were included from the files that were compiled. Unused
files are marked as excluded from code assistance. Each compiled file has
correct user include paths and macros.
In Conclusion, the Answers to Several Questions
How can you edit user include paths and macros?
In the properties of the project, logical folder, or file
(Properties > Code Assistance > C Compiler > General, or
Properties > Code Assistance > C++ Compiler > General).
Take into account that properties can be inherited from the project and from
the logical folder to the file.
A file has properties for the C compiler or the C++ compiler,
depending on the file extension.
Projects and folders have properties for the C compiler and C++ compiler.
How can you detect that the IDE project has the wrong configuration?
Usually such IDE projects have unresolved include directives.
These are shown in the Projects window, as small question marks on a blue background in the upper right corner of the project node.
You can see the list of the unresolved include directives by right-clicking on
the project node and choosing Code Assistance > Show Failed #include Directives.
Is it possible to configure an already existing IDE project?
Yes. First, build the open source project with necessary flags mentioned above.
Then right-click the project node and choose
Code Assistance > Configure Code Assistance. The Configure Code Assistance
wizard works in two modes: simple and advanced.
The simple mode repeats the actions of the wizard of new project.
The advanced mode lets you select different methods
of obtaining the compiler commentary, and have a preliminarily view of the
analysis results.
Is it possible to have different platform/device dependent configurations in
one IDE project?
Yes. You can manage configurations in the project properties.
For each configuration you can launch the Configure Code Assistance wizard.
In the wizard, specify a different platform/device build result.
The wizard sets up the currently active configuration.