Jump to content
  • Advertisement
Sign in to follow this  
Lode

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

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!