Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-20-2005, 03:43 PM   #1
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
cross platform macros

I'm currently trying to make a cross platform app in c++ and I am wondering if I have left out any of the defines which different platform compilers use.
#if (defined(unix) || defined(_unix) || defined(__unix))
#if (defined(_WIN32) || defined(WIN32))
#if (defined(__OS2__)
Is there any which i have left out?
Old 12-20-2005, 04:25 PM   #2
LQ Newbie
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
Why not leave them all out -- it will save you time, and, if you don't have the environments in question installed, you'll probably get it wrong anyway.

For this stuff, I usually put a #define in the makefile. You change it depending on which platform you're on. This is generally safer, you don't need to care about what the compilers do, and you don't have to go through the code changing incorrect #ifdefs when you meet a new platform that behaves differently but still uses an existing #define.

And "#ifdef PLATFORM_UNIX" is quicker to type, too
Old 12-20-2005, 04:34 PM   #3
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Why not leave them all out...
These macros are in a header called Platform_check.h where i do something like
#if (defined(unix) || defined(_unix) || defined(__unix))
#define UNIX_BUILD
and similar for windows and mac, this is the only place where i check the platform, hereon in i check my own define. So if I come accross another platform define which i havnt included i just need to add if once to the Platform_check.h

This then allows me to include different headers dependant on the os or different pieces of code(ie small classes), for example a Mutex class. So in my code I can just

and it then makes the code independant of the platform. Maybe i should have said this in the first place.

But i do see what your saying, I just didnt want to have to alter the code (even if this only means adding #define UNIX_BUILD) and be able to compile on any machine.

Last edited by dmail; 12-20-2005 at 07:16 PM.
Old 12-20-2005, 10:02 PM   #4
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Old 12-20-2005, 10:16 PM   #5
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Originally Posted by primo
thanks primo
this is exactly what i was looking for.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best cross platform ide? bobwall Programming 3 11-10-2011 04:16 PM
cross platform c++ nyomon Programming 6 07-03-2005 04:24 PM
cross platform filesystem z9_87 Linux - Hardware 5 03-20-2005 07:49 PM
Cross-Platform LibAO Ross Clement Programming 0 03-05-2005 06:19 AM
Cross Platform biggiefatts Linux - Software 4 06-04-2002 04:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration