in reply to Why does order of #include's matter in XS file for C++ extensions under Windows?

I don't know the specific headers involved, but the generall problem is one of dependencies. Headers are used to tell the compiler about stuff it needs to know so it can use library code that is not part of the current compilation unit. (Note my use of "library" is pretty loose here - I just mean stuff compiled outside the current compilation unit.)

When different librarys are being used you can run into naming issues where different headers use the same identifier for different purposes. Often there is trickery in the headers to avoid some of the problems caused by that and often that trickery fails if headers are not included in the right order. This is most likely the issue that you were experiencing.

Often headers for one compilation unit require other headers in order to compile. The header writer has a number of options:

  1. Include the required header
  2. Include the required header only if it hasn't been included already
  3. Assume the required header has already been included
  4. Define the stuff it needs so the other header is not required

Depending on how the other header has been written, and how stable it is, all of those options can cause trouble! Probably the worst is 3 and possibly the best option is 2 closely followed by option 1.

Writing headers can be pretty tricky stuff. Occasionally you get into a situation where two headers seem to depend on each other for example. Generally that is more likely to be a problem relating to C++ classes than to C headers and is generally related to classes that need to reference each other. Very often in that case you are seeing the result of poor design.

Many headers still use the C preprocessor macro facility to provid named types. There is no way to manage such names and collisions between them to avoid "issues". In fact compile failures are much the best outcome when macros start wreaking havoc. It is much worse to have code that looks fine, but is behaving completely differently than you expect because some stupid macro has altered the meaning of an inocuous line of code into something that any obfusicated Perl hacker would be proud of.

In the case of using other people's headers maybe cargo cult is best. :)


DWIM is Perl's answer to Gödel
  • Comment on Re: Why does order of #include's matter in XS file for C++ extensions under Windows?