Search code examples
cconditional-compilation

Why should #ifdef be avoided in .c files?


A programmer I respect said that in C code, #if and #ifdef should be avoided at all costs, except possibly in header files. Why would it be considered bad programming practice to use #ifdef in a .c file?


Solution

  • Hard to maintain. Better use interfaces to abstract platform specific code than abusing conditional compilation by scattering #ifdefs all over your implementation.

    E.g.

    void foo() {
    #ifdef WIN32
       // do Windows stuff
    #else
       // do Posix stuff
    #endif
       // do general stuff
    }
    

    Is not nice. Instead have files foo_w32.c and foo_psx.c with

    foo_w32.c:

    void foo() {
        // windows implementation
    }
    

    foo_psx.c:

    void foo() {
        // posix implementation
    }
    

    foo.h:

    void foo();  // common interface
    

    Then have 2 makefiles1: Makefile.win, Makefile.psx, with each compiling the appropriate .c file and linking against the right object.

    Minor amendment:

    If foo()'s implementation depends on some code that appears in all platforms, E.g. common_stuff()2, simply call that in your foo() implementations.

    E.g.

    common.h:

    void common_stuff();  // May be implemented in common.c, or maybe has multiple
                          // implementations in common_{A, B, ...} for platforms
                          // { A, B, ... }. Irrelevant.
    

    foo_{w32, psx}.c:

    void foo()  {  // Win32/Posix implementation
       // Stuff
       ...
       if (bar) {
         common_stuff();
       }
    }
    

    While you may be repeating a function call to common_stuff(), you can't parameterize your definition of foo() per platform unless it follows a very specific pattern. Generally, platform differences require completely different implementations and don't follow such patterns.


    1. Makefiles are used here illustratively. Your build system may not use make at all, such as if you use Visual Studio, CMake, Scons, etc.
    2. Even if common_stuff() actually has multiple implementations, varying per platform.