LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-20-2005, 03:43 PM   #1
dmail
Member
 
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.
Code:
#if (defined(unix) || defined(_unix) || defined(__unix))
Code:
#if (defined(_WIN32) || defined(WIN32))
Code:
#if (defined(__OS2__)
Is there any which i have left out?
cheers
 
Old 12-20-2005, 04:25 PM   #2
tom_
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
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Quote:
Why not leave them all out...
These macros are in a header called Platform_check.h where i do something like
Code:
#if (defined(unix) || defined(_unix) || defined(__unix))
#define UNIX_BUILD
#endif
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

Code:
 mutex->lock();
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
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
See http://predef.sourceforge.net/index.php
 
Old 12-20-2005, 10:16 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by primo
thanks primo
http://predef.sourceforge.net/preos.html
this is exactly what i was looking for.
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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