Scope of C preprocessor definitions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    Scope of C preprocessor definitions

    In VC++ 2005, I noticed the scope of any #define's is confined to a single translation unit. In particular, this means that standard inclusion guards of the type
    [code=cpp]
    #ifndef INCLUDED
    #define INCLUDED

    [header code...]

    #endif
    [/code]
    would not work. Is there any way to extend the scope of preprocessor definitions beyond a single translation unit and without using compiler switches or unportable #pragmas?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Of course the inclusion guards woirk over multiple files.

    The C/C++ build process is a compile of multiple source files that are then combined into a result by the linker.

    The preprocessor operates on one file at a time. The translation unit is passed to the compiler. Done.

    The next file needs to have ALL of the #defines required. There is no memory of "the other file". That is, nothing carries over from one file to the next.

    It is this structure that allows you to mix C files, C++ files, assembly files, etc in the same build.

    Comment

    Working...