Macros in C++ Source Files

You can include preprocessor macros in C++ source files, exactly like you do in source files that you prepare for conventional C++ compilers.

Unlike conventional compilers, VisualAge C++ performs an orderless compile and you can specify many source files in any order. Functions, classes, and variables do not have to be declared before they are referenced, because the build process finds the declarations as it needs them. However, preprocessor macros do have intrinsic position dependencies. The order in which they are defined, undefined, or redefined affects your code. For example, constructs such as the following are common, especially in header files:

#ifndef MY_GUARD
#define MY_GUARD 1
...
#endif

Because of the ordering requirements of macros, changes to macros cause more recompilation than changes to C++ code.

How VisualAge C++ treats a macro depends on whether the source file in which it is defined is a macro source. A macro source is a C++ source file that is listed in the configuration file and to which the option macros(global, yes) applies.

If a macro is defined in a source that is not a macro source, it is local to the source file in which it is defined. The macro is visible only to code that appears after the macro definition in the same file. The macro definition is not visible from other source files.

Macros in macro sources are global. They are visible from the point of definition to the end of the file and continue to be visible in all macro source files that are listed subsequently in your configuration file. Global macros are visible to all sources that are not macro sources.

When you convert a header file to a primary source file, you should also make it a macro source if the header defines macros that are used by other source files. Apply macros(global) to the header file, so that macros defined in it are visible in other sources that previously included the header file.

You should specify source files that are not header files as primary sources and either accept the default setting of macros(global, no) or explicitly apply macros(global, no) to them. Do this to give code in these files the same behavior as in regular C++ compilers.

The only difference between macro sources and other primary sources is in the way macros are preprocessed. The order of macro sources in the configuration file controls the scope of macros defined within them. For processing C++ declarations, macro source files are treated exactly like other source files.

When you optimize your configuration file using the configuration optimizer, compile errors may be reported.  This may be due to changes the configuration optimizer makes to your configuration file. Part of the configuration optimizations made by this tool involve automatically promoting system run-time header files that are #included in your application. In some cases this may cause incorporation errors if these runtime header files contain a reference to a macro name that is defined in your application.

There are two ways you can resolve this problem:

Here is an example of the problem, followed by the two possible solutions:

Problem

t.cpp
#include "t.h"
#include <windows.h>
t.h
#define _WIN32_WINNT 0x0400
t.icc (config optimizer generated)
target "t.exe"
{
  option macros(global,yes)
  { 
    source type(cpp) "windows.h"
  }
  source type(cpp) "t.cpp"
}

Solution 1

Solution 2 (recommended)

t.icc
target "t.exe"
{
  option macros(global,yes)
  {
    source type(cpp) "t.h"
    source type(cpp) "windows.h"
  }
  source type(cpp) "t.cpp"
}
t.icc
target "t.exe"
{
  option macros(global,yes)
  {
    option define("_WIN32_WINNT","0x0400")
    {
      source type(cpp) "windows.h"
    }
  }
  source type(cpp) "t.cpp"
}

The following build options can affect how macros are processed:

Option Definition
macros(global[, yes])
macros(global, no)
Determines whether source files are macro sources. By default, source files are not macro sources.
macros(redefine[, yes])
macros(redefine, no)
Controls whether macros can be redefined without using the #undef preprocessor directive first.
define(macro_name[, string]) Defines macros that can be used in source files to which this option applies.
undefine(macro_name) Undefines a macro in the of source files to which this option applies.


Configuration Files
Codestore
C++ Source Files and Source Regions
One-Definition Rule
The Configuration Optimizer


Convert Included Source Files to Primary Source Files