[C++] "standardized" macro's to detect OS?

Started by
19 comments, last by Nitage 14 years, 5 months ago
I find the macro's to detect operating system with an #ifdef confusing, because I see different things being used. For Windows, is it _WIN32, WIN32, _MSDOS or MSDOS? What about 64-bit Windows, does "WIN32" still work there? For Linux, is it _GNUC_, UNIX, LINUX, POSIX, or what? And what is it for Mac? I don't have a Mac to develop on to try it but I might need it one day. I assume there doesn't really exist any reliable standard for it, but which combination would work best? Also, do there exist ways to detect with macro's what you're adding to the linker parameters of the compiler? For example, if I link to "boost_filesystem", it could use a boost implementation for browsing files, if not it could use a native one like win32 or posix code or a dummy implementation that says "not supported".
Advertisement
This site has a good listing of predefined macros. As to what the best subsets are to maximize detection reliability, but minimize size, I'll leave that an open question :)
Quote:Original post by mattd
This site has a good listing of predefined macros. As to what the best subsets are to maximize detection reliability, but minimize size, I'll leave that an open question :)


Nice! They list more than enough operating systems in there :)
Very handy.
Actually, you could get some guidance for that 'but which macros' open question by looking at how something like boost does it.
Quote:Original post by mattd
Actually, you could get some guidance for that 'but which macros' open question by looking at how something like boost does it.
I am not sure that is such a good idea - boost has some of the most convoluted platform and feature detection on the planet. Granted, they need it, but it should be complete overkill for a game project.

I usually stick with a simple (and so far reliable):
#if defined( WIN32 )#elif defined( __APPLE__ )#else /* some flavour of unix */#endif

Of course, if you want to detect minor platforms, then you need to do a bit more work.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
I am not sure that is such a good idea - boost has some of the most convoluted platform and feature detection on the planet.

Quote:I usually stick with a simple (and so far reliable):

Did you actually look at the header file that he linked to? What boost does isn't very different from what you do, except that it handles more platforms than you do.
Quote:Original post by SiCrane
Did you actually look at the header file that he linked to? What boost does isn't very different from what you do, except that it handles more platforms than you do.
Yes, but blindly copying from that header without familiarity for the platforms involved is dangerous. For instance, their Mac path supports classic-mac as well as OS X, which is something that one won't notice without mac experience, and almost certainly isn't what one intends.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

That's seems like a very different statement than "the most convoluted platform and feature detection on the planet."
Quote:Original post by SiCrane
That's seems like a very different statement than "the most convoluted platform and feature detection on the planet."
That header file is just the tip of the iceberg. There are detection headers for each platform, plus feature detection for endianess, integer types, system header files, etc.

But, to be fair, I did overstate that a little - sarcasm is a dangerous thing on the internet [smile]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

By more than a little if you just after you say it you then suggest to do things the exact same way.

This topic is closed to new replies.

Advertisement