Search code examples
cdelphiconditional-compilationifndef

Conditional compilation in C and Delphi


The next pattern is common in C code:

#ifndef SOMETHING
#define SOMETHING
#endif

The pattern is possible in Delphi code too:

{$IFNDEF SOMETHING}
{$DEFINE SOMETHING}
{$ENDIF}

but it is not common - I have never seen it at all. If a Delphi code requires a conditional define, it just defines it without IFNDEF check.

Why so? What is the difference in conditional compilation between C and Delphi so that ifndef check is needed for former and not needed for latter?


Solution

  • That's because this is not only common but mandatory in C:

    #include <something.h>
    

    While this is rarely used in Delphi. And when used, it's actually used to set up those {$DEFINE} 's:

    {$INCLUDE 'something.inc'}
    

    This matters because DEFINES are only valid while compiling one object (may it be a .PAS file or a .C file). Delphi uses the uses clause to include other units, while C uses the include to include it's headers. In C headers might themselves include other headers. The pattern you're asking about is used to prevent recursively re-including the same header.

    To make maters crystal-clear, here's a sample of what one might use in C, and the equivalent in Delphi. Let's say we've got a 3 files setup, where A needs to include both B and C, and B only needs to include C. The "C" files would look like this:

    // ----------------------- A.h
    #ifndef A
    #define A
    
    #include "B.h"
    #include "C.h"
    
    // Stuff that goes in A
    
    #endif
    
    // ------------------------ B.h
    #ifndef B
    #define B
    
    #include "C.h"
    
    // Stuff that goes in B
    
    #endif
    
    // ----------------------- C.h
    #ifndef C
    #define C
    
    // Stuff that goes in C
    
    #endif
    

    Without the conditional defines in C.h, the C.h file would end up being included twice in A.h. This is how the code would look like in Delphi:

    // --------------------- A.pas
    unit A;
    
    interface
    
    uses B, C;
    
    implementation
    
    end.
    
    // --------------------- B.pas
    unit B
    
    interface
    
    uses C;
    
    implementation
    
    end.
    
    // --------------------- C.pas
    
    unit C
    
    interface
    
    implementation
    
    end.
    

    The Delphi/Pascal version doesn't need to protect "C" from being included twice in "A" because it doesn't use the {$INCLUDE} to achieve this goal, it use the uses statement. The compiler would get the exported symbols from the B.dcu file and the C.dcu files with no risk of including symbols from C.dcu twice.


    Other reasons to see a lot more precompiler directives in C code:

    • The precompiler is a lot more powerful then Delphi's. A {$DEFINE} in Delphi code only deals with conditional compilation, while the C variant can be used for both conditional compilation and as a form of word substitution.
    • The mandatory use of #include for headers means you can have a header that defines macros. Or you can have a header that's configured by specifying some #define statements before the actual #include <header.h>