Exploring Macros in C/C++ Projects - NetBeans IDE Tutorial

This tutorial shows you how to use new NetBeans IDE features for investigating and debugging code that includes macros.

Contents

Content on this page applies to NetBeans IDE 7.3 and 7.4

Requirements

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

Software or Resource Version Required
NetBeans IDE version 7.3 or 7.4 with NetBeans C/C++ plugin
Java Developer Kit (JDK) version 6 or 7

See the NetBeans IDE Installation Instructions and Configuring the NetBeans IDE for C/C++/Fortran
for information about downloading and installing the required software.

Investigating Code That Uses Macros

Macros are hyperlinked so that when you Ctrl-click them, the IDE opens the header file where the macro is defined. However, sometimes for simple macros such as numerical constants or string literals you might only want a quick look at the macro definition.

NetBeans IDE provides two features for quickly exploring source code that contains macros:

  • Macro tooltips, useful for short and simple macros
  • Macro Expansion window, useful for more complex macros

Working with Simple Macros Using Macro Tooltips

The tooltips for macros allow you to view a small popup without opening another file.

To activate a macro tooltip so you can see its expansion:

  1. Press and hold Ctrl+Alt.
  2. Place the mouse cursor on the macro you want to expand, and a tooltip opens to display the expanded information, as shown in the figure below.

Macro tooltip expansion

Working with Complex Macros Using Macro Expansion Window

The Macro Expansion view enables you to investigate source code with complex multiple-line macros. Macros are often used to define constants, but can also be used to define code fragments that can be used like functions. When you work with code that uses macros in this way, it might be difficult to understand what is happening in the code. You may need to examine the macros in context with the source code that is calling the macro.

To view the source code with expanded macro code:

  • Right-click on the macro and select Navigate->View Macro Expansion from the pop-up menu.
    The Macro Expansion window opens to display the expanded macro in context with the code that calls it.
  • You can also mouse over the code of interest while pressing Ctrl+Alt, and click the hyperlink to open the Macro Expansion window.

The following figure shows the Macro Expansion window.

Macro expansion view

In the Macro Expansions window, the macro contents are located inside guarded blocks. Semantic highlighting is provided for expanded code. You can use hyperlinks in the Macro Expansion window to go to other source files. Also there is highlighting for macro parameter usages.

The left column of the Macro Expansion window includes several toolbar buttons:

  • Synchronize caret, content and context
    This option synchronizes caret position and content in the source file in the Editor window and in the Macro Expansion window.
    It also changes context to make it relevant to caret position (if expand local context only selected).
  • Expand local context only Only the macros in the code block where the caret is located are expanded in the Macro Expansion window.
  • Expand whole file
    All macros in the source file are expanded in the Macro Expansion window.
  • Up and down arrows
    These buttons are for quick navigation to go to the next macro or go to the previous macro.

Debugging Code That Uses Macros

Sometimes it is not enough to understand the meaning of a macro.
For example, suppose you are debugging the following code:

#include <stdlib.h>
#include <iostream>

#define ID_FIRST (0)
#define ID_ALICE (ID_FIRST)
#define ID_BOB (ID_ALICE + 1)
#define ID_TOM (ID_BOB + 1)
#define ID_FRINDS_NUMBER (ID_TOM + 1)

#define NEXT_PERSON(id) (id + 1)
#define PREV_PERSON(id) (id - 1)

using namespace std;

/*
* Main function
*/
int main(int argc, char** argv) {
int person = ID_TOM;
if (NEXT_PERSON(person) == ID_FRINDS_NUMBER) {
cout << "last person";
}
return (EXIT_SUCCESS);
}

Imagine that you stopped the debugger on the if statement and you want to know if the program will print "last person" or not.

Macro expansion tooltip says that ID_FRINDS_NUMBER is "(((((0))+ 1)+ 1)+ 1)".
How much is ID_FRINDS_NUMBER? In this simple situation it might be easy to say that the value of ID_FRINDS_NUMBER is 3, but it might not be as easy if you have two thousand persons.

You might find it useful to evaluate the expression in the macro when debugging, as explained below.

Evaluating Macros Using Tooltips

You can use expression evaluation on expressions with macros in the debugger.
Hover the mouse pointer over ID_FRINDS_NUMBER and you see what is shown in the following figure:

Macro expression evaluation

You can also select the whole expression and mouse over it to evaluate the expression in a tooltip, as shown in the following figure:

Macro expression evaluation

Be careful evaluating macros in tooltips, because such evaluation can change the state of a program. For example evaluating of macro that expands to i++ will increment i.

Using Watches for Evaluating Macros

It is possible to use macros in debugger watches, as shown in the following figure:

Macro debugging watches

Be careful evaluating macros in watches, because such evaluation can change the state of a program. For example evaluating of macro that expands to i++ will increment i.

See Also

Please see the C/C++ Learning Trail for more articles about developing with C/C++/Fortran in NetBeans IDE.

get support for the NetBeans

Support


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2015, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo