Search code examples
c++ffmpeg

Sharing #define in non header file


To the best of my knowledge I thought you can only share a #define in header files or only in the source file it was declared in. Stack overflow answers like these seem to support my belief that defines can only be shared through header files.

In the FFmpeg libavutil v6.0 source code, tx_priv.h uses 3 defines TX_FLOAT, TX_DOUBLE, and TX_INT32. These 3 defines are declared in tx_float.c, tx_double.c, and tx_int32.c respectively. I'm wondering how can tx_priv.h access the defines declared in source files? Shouldn't the default #else always resolve?

This is mostly in the context of other compilers/build tools than the included make. I want to build FFmpeg but I'm trying to understand this issue.

I was expecting for the file tx_priv.h to never be able to check if the defines TX_FLOAT, TX_DOUBLE, and TX_INT32 are actually ever defined.


Solution

  • Header files are not compiled themselves, the code within is when they are included in source files that get compiled.

    This way the code within a header can see any symbols that have been #defined before the header has been included, e.g. for tx_int32.c that might look as:

    #include <stdint.h>
    
    #define TX_INT32 int32_t
    
    #include "tx_priv.h"
    

    On compiling tx_int32.c the pre-processor will replace the #include directive with the entire contents of the file to be included (and most likely continue pre-processing these right on the fly, but that's not the point here…), so after the inclusions the source file might look like:

    typedef int int32_t;
    // all the other typedefs of stdint.h
    
    #define TX_INT32 int32_t
    
    #ifdef TX_INT32
    // whatever...
    #endif
    

    Side note: The name of the header sounds like being intended to never be (directly?) included by the user...