C/C++ Application How-Tos for Code Assistance

This tutorial needs a review. You can edit it in GitHub following these contribution guidelines.

Code assistance is a set of IDE features that help you navigate and edit source code. For a C/C++ Project With Existing Sources or a C/C++ Project From Existing Binary, you can specify how your code will be parsed to enable the code assistance features of the IDE.

Note that the tips in this article mostly apply to projects from existing source or from an existing binary. For projects that are created and managed by the IDE code assistance configuration is not necessary.

What To Do When Your Project Has a Question Mark in the Projects Window

If your project has a question mark in the Projects window, or an #include directive is underlined in red, then your project has unresolved include directives. The IDE uses an internal parser that is used by Code Assistance features (Code Completion, Classes window, Navigator window, etc). The markings mean that this parser cannot resolve some #include directives, because the IDE project has an incorrect configuration. This might happen if the project was created from existing sources without help from debugging information or if, for example, the source files included new libraries since the IDE project was created.

Here are some possible reasons (arranged from most to least probable):

  • Wrong or insufficient user include paths specified in the project, logical folder, or file properties

  • Wrong or insufficient user-defined macros specified in the project, logical folder, or file properties

  • Source file is included in the project by mistake

  • Header file is not included in any source files and hence is included in the project by mistake

If you hold the mouse cursor over the project folder, a tooltip displays some information about the problem. For more information, you can right-click the project and select Code Assistance → Failed #include/#error directives.

Try launching the Configure Code Assistance wizard by right-clicking the project and choosing Code Assistance > Configure Code Assistance to resolve the problem. If you know the location of the files that correspond to the failed include directive, then you can set up the project, logical folder, and file properties manually.

Configuring Build Analyzer for Code Assistance

Suppose your application source code is managed with a version control system (VCS) such as Mercurial, and you create an IDE project from the source code. If you open the IDE project in the IDE and pull in changes from the VCS, you should build the IDE project so it is up to date with the modifications.

After you build your project in the IDE, the project is automatically updated by the IDE in the following ways:

  • New compilation units are added to the IDE project.

  • Existing compilation units are modified with new or changed user-defined includes and macros.

  • Compilation units that are excluded from building are not excluded from code assistance.

The IDE uses a build analyzer to gather information from the build, using an interposing technique to catch system library exec calls on Oracle Solaris and Linux platforms. By default, the build analyzer is used when you create a project with existing sources and when you manually run Configure Code Assistance. If the build analyzer is not used, the output log of the build is used to capture information.

If you do not want the IDE to perform the project updates, you can disable the build analyzer as follows:

  1. Right-click the project node in the Projects window and select Properties.

  2. In the Project Properties dialog box, click the Code Assistance category.

  3. Deselect the Use Build Analyzer option.

For source code projects that set up their own environment at build time, the build analyzer might not always work well. If a build fails when the Use Build Analyzer option is selected, you should check your build script to see if you have unset the LD_LIBRARY_PATH environment variable. If LD_LIBRARY_PATH is unset, you should also unset LD_PRELOAD . When these two variables are unset, the build analyzer is automatically disabled. If you cannot modify the build script, you can disable the build analyzer as described above, and the IDE can use the output log for build information.

Configuring Code Assistance for a Multi-Platform Project

If you are developing a multi-platform project from existing code, you can use the same IDE project for different platforms. Create a configuration per platform and change the compiler collection, compiler options, and other project properties appropriately.

The Configure Code Assistance wizard also works on a per-configuration basis: it alters only the configuration that is currently active. So you can run it separately for each configuration and get different code assistance settings for different platforms.

Configuring Code Assistance When You Cannot Build the Project

The Configure Code Assistance wizard is most efficient if you built your code with debugging information (the best options are -g3 -gdwarf-2 for GNU compilers and just -g for Oracle Solaris Studio compilers.

If your project cannot be built or does not contain debugging information, the Configure Code Assistance wizard has a special mode called Search File System for C/C++ Header Files. In this mode, the NetBeans IDE tries to resolve failed include directives by searching the file system for headers. The wizard asks you to enter a path to search for headers. By default, the path searched is a project source root.

To use the wizard, right-click the project and choose Code Assistance > Configure Code Assistance. Follow the steps of the wizard to enable the IDE to update code assistance. Click the Help button to get information about each step.

Hyperlink navigation lets you jump from the invocation of a function, class, method, variable, or constant to its declaration. To use a hyperlink, do one of the following:

  • Mouse over a class, method, variable, or constant while pressing Ctrl. A hyperlink appears along with a tooltip with information about the element. Click the hyperlink and the editor jumps to the declaration. Press Alt+Left to jump back to the invocation.

  • Mouse over an identifier and press Ctrl+B. The editor jumps to the declaration.

  • Press Alt+Left to jump back to the invocation. Press Alt+Left and Alt+Right to move backward and forward through the history of the cursor position.

You can also right-click the item and select Navigate > Go to Declaration/Definition, or other options to navigate through your code.

Finding All Definitions of a Namespace

A namespace can be defined in different files of the project. To navigate between different namespace definitions, use the Classes window (Ctrl-9). Right-click the namespace you are interested in and choose All Declarations. You will see a list of all definitions sorted by file names.