0

There is a header file that I include that only seems to exist on Linux machines, and not on MacOS machines. Although I can use a VM to compile and run the code, it would be nice to be able to do this in MacOS.

To be more specific, I am using #include <endian.h>, which compiles on Linux, and I would like to use this compatibility header for MacOS, which I would include with #include "endian.h". My code compiles and executes as expected with the former include on Linux machines, and with the latter include on MacOS machines.

Is there a way to use platform-specific includes in the header (perhaps using some sort of #if-based syntax)? Or would this be bad practice?

hitecherik
  • 442
  • 2
  • 13
  • 2
    Possible duplicate of [Macros for GCC/G++ to differentiate Linux and Mac OSX?](https://stackoverflow.com/questions/2565979/macros-for-gcc-g-to-differentiate-linux-and-mac-osx) –  Jun 01 '18 at 00:20
  • You can have a `#include` line in between `#if` and `#endif` lines. If this doesn't answer your question can you be more specific? – M.M Jun 01 '18 at 00:35
  • There's information at the link you posted that shows how to do this. – Retired Ninja Jun 01 '18 at 00:46
  • This question is far more specific than the question linked to by @AlejandroVisiedo - I am asking about platform-specific includes, whereas the other question is asking about platform-specific macros. – hitecherik Jun 01 '18 at 00:55
  • @M.M Please read the question - I fully understand that `#if` and `#endif` can be used in this way; I'm just not sure which condition, if any, `if` would take. – hitecherik Jun 01 '18 at 00:59
  • @hitecherik Have you installed `Command Line Tool`? –  Jun 01 '18 at 01:24
  • @hitecherik the condition would be a platform-specific macro – M.M Jun 01 '18 at 01:30
  • @GRC I have `Command Line Tools` installed - it doesn't seem to make a difference in my case. – hitecherik Jun 01 '18 at 01:50
  • 2
    @AlejandroVisiedo - that is a good "related" link, but the answers (and comments) reference broken links, etc.. I'm not sure it would be worth closing this one relying on that one as the authoritative answer. – David C. Rankin Jun 01 '18 at 02:00
  • Possible duplicate of [endian.h not found on mac osx](https://stackoverflow.com/questions/20813028/endian-h-not-found-on-mac-osx) –  Jun 01 '18 at 02:35
  • @hitecherik here is the exact problem you have: https://stackoverflow.com/questions/20813028/endian-h-not-found-on-mac-osx?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa –  Jun 01 '18 at 02:39

2 Answers2

3

Clang and GCC support the __has_include preprocessor condition, which you can use instead of testing platform defines:

#if __has_include(<endian.h>)
#include <endian.h>
#else
#include "endian.h"
#endif

One thing to watch for, though, is that as <endian.h> is not a standard header, it could be possible that it's present on another platform with different definitions that don't really help you.

This is related to this other answer that I wrote a few days ago.

zneak
  • 134,922
  • 42
  • 253
  • 328
2

Is there a way to use platform-specific includes in the header (perhaps using some sort of #if-based syntax)?

yes:

#ifdef __MACH__
... // Mac headers
#elif __unix__
... // these will work for Linux/Unix/BSD even for Mac in most cases
#elif _WIN32
... // windows 32 bit
#elif _WIN64
... // windows 64 bit
#endif

Or would this be bad practice?

I do not think so

The other solution if I remember correctly is installing Command Line Tool on Mac which will give you all headers for gcc in Unix like passion. Here is improvement to my answer, I knew I was forgetting something :( Oh well I only used Mac couple of times for development :S

  • install the command line tools from the Xcode Preferences->Downloads window, or
  • execute xcode-select --install from the Terminal command-line.

and here is the reference:

endian.h not found on mac osx