00:00
00:00
Newgrounds Background Image Theme

MagDeWarrior just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

C++ main: Portable code

1,043 Views | 7 Replies
New Topic Respond to this Topic

C++ main: Portable code 2011-02-15 10:51:48


Portable code is compilable on multiple platforms without changing anything in the source.
In this tutorial I will explain the way I do this.

The precompiler carries some constants, from this you are able to find out which platform the compiler is running on.
Because it's such little code I will just completely post it here:

//detect Mac OS-X
#if (defined(__MACH__))
   //code for Mac platform, __Apple__ can also be used but that can result in problems
#elif defined (__linux__)
  //code for Linux platform
#elif defined (_WIN32)
  //code for Windows platform (also for 64 bit)
#else
  //unknown platform
#endif

Or, if the same code is being used for multiple Operating Systems:

#if (defined(__MACH__))|| defined (__linux__)
  //code for Mac AND Linux
#elif defined (_WIN32)
  //code for Windows
#else
  //unknown platform
#endif

Response to C++ main: Portable code 2011-02-15 10:53:23


Forgot the link to C++:main

Response to C++ main: Portable code 2011-02-15 18:18:46


I must be brain-dead, but how does this work?

For some reason, it seems like placing that in your code magically allows it to
run on mac, windows, and Linux.

if(above_statement_is_false)
tell_me( what you need to do);

Gordon Freeman is better than Master Chief. I'm sorry, but he just is.

BBS Signature

Response to C++ main: Portable code 2011-02-15 21:26:52


At 2/15/11 06:18 PM, Dragonslayerk wrote: For some reason, it seems like placing that in your code magically allows it to
run on mac, windows, and Linux.

That code lets you write code for different machines/OSs without having separate programs. That way, you can send the ONE file to any computer, and it will work.

These tags only tell you what machine/OS you're running on and don't magically grant you a free-pass on your own code when it comes to compatibility. It just makes machine/os detection easier.


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to C++ main: Portable code 2011-02-15 21:43:48


At 2/15/11 09:26 PM, Momo-the-Monkey wrote:
That code lets you write code for different machines/OSs without having separate programs. That way, you can send the ONE file to any computer, and it will work.

These tags only tell you what machine/OS you're running on and don't magically grant you a free-pass on your own code when it comes to compatibility. It just makes machine/os detection easier.

yeah, I thought so, I'm just dead today.
I'm having trouble comprehending the world today. Sorry.


Gordon Freeman is better than Master Chief. I'm sorry, but he just is.

BBS Signature

Response to C++ main: Portable code 2011-02-16 09:11:08


At 2/15/11 09:26 PM, Momo-the-Monkey wrote:
At 2/15/11 06:18 PM, Dragonslayerk wrote: For some reason, it seems like placing that in your code magically allows it to
run on mac, windows, and Linux.
That code lets you write code for different machines/OSs without having separate programs. That way, you can send the ONE file to any computer, and it will work.

These tags only tell you what machine/OS you're running on and don't magically grant you a free-pass on your own code when it comes to compatibility. It just makes machine/os detection easier.

Um, no? It's only for compiling. You'd need a runtime environment for real portability (eg, Java). But the C preprocessor only governs compilation time. You still have to compile separate binaries. It's useful because you don't actually have to change the source code when you want to compile for a certain OS.


#include <stdio.h>

char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";

main() {printf(p,10,34,p,34,10);}

BBS Signature

Response to C++ main: Portable code 2011-02-16 12:17:47


Ah yes, I did lose myself, didn't I? You are correct, I apologize. There is only one file for compilation, which still means you don't have to mess with different files for different OSs, just the binaries.

You are correct, I apologize.


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to C++ main: Portable code 2011-02-16 15:52:22


At 2/15/11 06:18 PM, Dragonslayerk wrote: I must be brain-dead, but how does this work?

For some reason, it seems like placing that in your code magically allows it to
run on mac, windows, and Linux.

No, I will explain again.

#if (defined(__MACH__))

Checks for Mac OS-X, any code between that and the next #elif, #elseif or #endif will only be compiled on a computer running Mac OS-X.