Preprocessor conditionals for OS variation

Started by
6 comments, last by GameDev.net 17 years, 9 months ago
I've recently purchased a Mac in order to get some experience developing for a third platform, in addition to Windows and Linux. To start with I'm simply trying to port a couple of my existing programs, and a question has come up that I can't seem to find a solution to. In the past, I've been able to have certain parts of my code only compile for certain OSes by using #ifdef commands. For example:

#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
#else
int main(){
#endif
However, I've been unable to identify an equivalent condition that can be used for identifying Mac OS X. This presents difficulties as a result of a few differences, like the fact the OpenGL headers are in a different path, OpenGL, instead of GL. I'd like to be able to do something like:

#ifdef _MAC_OS
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
It's occurred to me because of its UNIX heritage it may not actually be possible, in which case I'd most likely have to turn to a tool like autoconf or something instead. Still, I'd appreciate any advice that would let me keep my source consistent and compiling regardless of the platform.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Advertisement
You could always add your own _MAC_OS define for the mac version (in the commandline of the compiler for example), if all else fails. :)
Orin Tresnjak | Graphics ProgrammerBethesda Game StudiosStandard Disclaimer: My posts represent my opinions and not those of Bethesda/Zenimax, etc.
There's definitely a define for it built in, I can't remember what it is though (I, too, recently bought a mac). I'll see if I can't look it up and let you know.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
This is taken from my platform check, although it has never been tested on a mac ;so I would like to know if it works (it should do), tested on win32 and linux
#ifndef PLATFORM_CHECK_H_#	define PLATFORM_CHECK_H_//windows#	if (defined(__WIN32__) || defined(_WIN32) || defined(WIN32))#		define WINDOWS_BUILD	1//os2#	elif (defined(__OS2__) || defined(_OS2)  || defined(OS2) || defined(Macintosh) || 					defined(macintosh) || defined(__MACOSX__) || defined(__APPLE__))#		define MAC_BUILD		1//nix#	elif (defined(unix) || defined(_unix) || defined(__unix) || defined(__unix__) || 											defined(linux) || defined(__linux))#		define UNIX_BUILD		1#	endif#endif //PLATFORM_CHECK_H_
Pre-defined compiler macros.
Thank you, all of you!
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
I forgot to reply to the AP, so I'll do that now: It works fine. Taking it on a more single-step basis, apparently __APPLE__ is the define that's working best for me, for some reason __MACOSX__ doesn't seem to be present, even though SiCrane's link claims it should be. Oh well, as long as one of them works.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Quote:Original post by Arek the Absolute
I forgot to reply to the AP, so I'll do that now: It works fine. Taking it on a more single-step basis, apparently __APPLE__ is the define that's working best for me, for some reason __MACOSX__ doesn't seem to be present, even though SiCrane's link claims it should be. Oh well, as long as one of them works.


Thanks for that.

This topic is closed to new replies.

Advertisement