# Preprocessor conditionals for OS variation

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

## Recommended Posts

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.

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

##### Share on other sites
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.

##### Share on other sites
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_

##### Share on other sites
Thank you, all of you!

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Arek the AbsoluteI 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.

1. 1
2. 2
3. 3
JoeJ
12
4. 4
5. 5

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632176
• Total Posts
3004588

×