1

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.

JParra
  • 13
  • 2
  • 1
    `#include` directive is very simple - it reads the file and pastes entire content in place of this directive. If you copy and paste content of `tx_priv.h` into `tx_float.c`, can you see what happens? – Yksisarvinen Aug 04 '23 at 09:13
  • As I can see in the repo, ```tx_priv.h``` is included juste after the ```#define TX_FLOAT``` directive, in this case you're sure that this particular inclusion of ```tx_priv.h``` will see ```TX_FLOAT```. The other way around, ```tx_priv.h``` is included in many places where the various macro may or may not be defined. It seems a way to use different sections of ```tx_priv.h``` according to the context of inclusion. So yes it's "working" but I'm not sure if it is the best way to do so (perhaps...). – Oersted Aug 04 '23 at 09:21
  • Thanks that makes sense. – JParra Aug 04 '23 at 18:41

1 Answers1

2

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...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • I see, so they are including the header file into the different tx_ types, like templated creation. – JParra Aug 04 '23 at 18:41