The C++ Preprocessor

By: Max_Power 8/20/2004

This tutorial gives very brief information about the C++ Preprocessor, it is meant to be used as a reference. It also lists and gives a brief description of pragma directives that are specific to popular compilers.

Before your C++ code is compiled it gets run through the C++ preprocessor, which is used to perform processing functions to prepare the code for compiling. The preprocessor's job can be broken down into three steps:

  1. Removing the comments.
  2. Including any files.
  3. Replacing names with their macros introduced using #define.

The preprocessor does the majority of its job by following the explicit instructions given to it, also called directives. A preprocessor directive should always be prepended with a #. As dictated by the American National Standards Institute (ANSI) standard, comments are the only text that should follow a preprocessor directive. Here are examples of valid preprocess directives:

#    define speed 1
    #define speed 1
#define speed 1 //The speed of a turtal

The following table details the 12 standard ANSI compliant preprocessor directives. Examples are only given for the directives the author feels are not self explanatory.

#define Used to define a macro.
#elif Combination of the #else and #if directives.
#else Used to define an else condition for a conditional preprocessor block.
#endif Used to end a conditional preprocessor block.
#error Used to report errors, usually used in accordance with conditional directives.
#if

Makes it so you can have certain pieces of code only be compiled under a certain condition. Ex.
#if sizeof(int) = 4
      typedef int Integer
#endif

#ifdef

Makes is so you can have a certian piece of code only be compiled if a macro with the given name is defined. Ex.
#define debug 1
#ifdef debug
      ...
#endif

#ifndef Makes is so you can have a certian piece of code only be compiled if a macro with the given name is not defined.
#include Used to include the contents of another file. The contents of the included file are copied to the location were the include directive appears.
#line Allows you to make the compiler believe the current line is a specified line in a specified file. This directive is normally used to make syntax error messages provided by the compiler more meaningful. Ex. #line 90 "blah.h"
#pragma Used by compilers to implement custom directives.
#undef Used to undefine a previously defined macro. Useful if you need to redefine a macro.

The following table briefly details Visual C++ specific preprocessor directives, which are used in accordance with the #pragma directive. Consult your Visual C++ documentation for more details. Some descriptions are word for word what they are in the Microsoft documentation. You will need to visit the documentation for help on actually using these.

alloc_text Used to name the code section a the given function is to reside in.
auto_inline Used to turn on or off consideration for inline functions. If you use this to turn of inline candidate checking explicitly defined inline functions will still be made inline.
bss_seg Used to specify the section for uninitialized data.
check_stack Used to turn on or off probes used to check the stack.
code_seg Used to specify the section for your programs code.
const_seg Used to specify the section for your programs constant data.
comment Places a comment record in an object file or executable file.
component Turns on or off the collecting of dependency or browser information from within your source files.
data_seg Used to specify the section for your data.
function Specifies that calls to functions specified in the pragma’s argument list be generated.
hdrstop Used to stop header precompilation for a specific header.
include_alias See documentation.
init_seg Used to affect the way startup code is executed.
inline_depth Used to limit inline expansion.
inline_recursion Used to control the inline expansion of recursive function calls.
intrinsic See documentation.
message Used to display messages in the output window during a compile.
once Used to specify that a file should only be included once by the compiler.
optimize Used to specify a specific type of optimization for a specific function, see documentation.
pack See documentation.
pointers_to_members See documentation.
setlocale Used to define the country and language used during translation of literal strings and wide-character constants.
vtordisp See documentation.
warning Used to modify compiler warning messages.

The following table briefly details Borland C++ specific preprocessor directives, which are used in accordance with the #pragma directive. Consult your Borland C++ documentation for more details. Some descriptions are word for word what they are in the Borland documentation. You will need to visit the documentation for help on actually using these.

alignment Used to display a message in the status window giving the current alignment and enum size.
anon_struct Allows you to compile anonymous structures embedded in classes.
argsused Allows you to disable the compiler warning message stating "Parameter name is never used in function func-name" for a specific function.
checkoption See documentation.
codeseg Allows you to name the the segment, class, or group where functions are allocated.
comment Allows you to write a comment record into an output file.
defineonoption Allows you to alias a command line option to a name.
exit Allows you to specify a function that should be called before the program exits.
hdrfile Lets you sets the name of the file in which to store precompiled headers.
hdrstop Lets you terminate the list of header files eligible for precompilation.
inline See documentation.
intrinsic Used to override command-line switches or IDE options to control the inlining of functions.
link Instructs the linker to link the file into an executable file.
message Used to specify messages to display during compilation.
nopushoptwarn Used to to include command-line options within program code.
obsolete See documentation.
option See nopushoptwarn.
pack See documentation.
package Used to assure that packaged units are initialized in the order determined by their dependencies.
resource Causes the file to be marked as a form unit and requires matching .dfm and header files.
startup Allows you to specify functions to be called before the main function.
undefineonoption
Used to undefine aliases defined using defineonoption.
warn Used to override compiler warnings.

The following table details predefined constants used by the preprocessor.

__DATE__ Gives the date the source file was compiled.
__FILE__ Gives the name of the source file.
__LINE__ Gives the line number of the current source file.
__TIME__ Gives the time the source file was compiled.
__STDDC__ Used to indication the implementation is ANSI C compliant .