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

This topic is 3368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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 :)

Share on other sites
Quote:
 Original post by mattdThis 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.

Share on other sites
Actually, you could get some guidance for that 'but which macros' open question by looking at how something like boost does it.

Share on other sites
Quote:
 Original post by mattdActually, 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.

Share on other sites
Quote:
 Original post by swiftcoderI 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.

Share on other sites
Quote:
 Original post by SiCraneDid 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.

Share on other sites
That's seems like a very different statement than "the most convoluted platform and feature detection on the planet."

Share on other sites
Quote:
 Original post by SiCraneThat'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]

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

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634133
• Total Posts
3015747
×