4 Replies - 2283 Views - Last Post: 15 April 2010 - 06:57 AM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Creating a cross-platform executable

Posted 15 April 2010 - 06:26 AM

Hi everybody,
I have a program for work that we port to four different operating systems, so, by extension, we have to build and distribute four different executables. This works fine, but I was just curious, is there any trick in C++ that I could use to combine these executables, so we only had to distribute one executable, but it would run properly on each of the four systems?
Thanks,
Zel2008
Is This A Good Question/Topic? 0
  • +

Replies To: Creating a cross-platform executable

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Creating a cross-platform executable

Posted 15 April 2010 - 06:37 AM

You need preprocessor blocks. Take a look at these:

For Windows 64-bit
// This stands for Windows 64-bit
#if defined(_WIN64)
// specific macros/headers/functions, goes here



For Windows 32-bit
// __WIN32__, WIN32 or _WIN32 (they are all the same)
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32)



APPLE (mac)
#if defined(__APPLE_CC__)



LINUX
#if defined(_LINUX)



And an example:
#include <iostream>

#if defined(_WIN64)
void foo()
{
    std::cout << "Windows 64-bit!\n";
}
#elif defined(__WIN32__) || defined(WIN32) || defined(_WIN32)
void foo()
{
    std::cout << "Windows 32-bit!\n";
}
#elif defined(__APPLE_CC__)
void foo()
{
    std::cout << "MAC OSX!\n";
}
#else
void foo()
{   
    std::cout << "Linux (UNIX based)\n";
}
#endif

int main()
{
    // So, foo() is defined ONLY once according to 
    // preprocessor blocks. It will print your OS.
    foo();

    return 0;
}



And, I can do something like this too:
#include <iostream>

#if defined(_WIN64)
// We are using windows 64 bit, let's define something
// here:
int a = 300; // a will be 300 in case of 64-bit windows
#else
// not 64-bit Windows -> being an #else, we are handling code
// for every other OS besides 64-bit Windows
int a = 30; // a will be 30 in case of non 64-bit Windows
#endif

// print a ...



Or another example:
// If the next #if executes, then we have a Windows platform, so let's
// include windows.h
#if defined (_WIN64) || defined(_WIN32) || defined(WIN32) || defined(__WIN32__)
#include <windows.h>
#else
// UNIX based system here, <windows.h> not available, so #include other
// header file
#include <unistd.h> // Specific to UNIX
#endif // Close the block



So, according to these macros, you can implement your functions to do different things on different OS'es.

This post has been edited by sarmanu: 15 April 2010 - 06:43 AM

Was This Post Helpful? 2
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Creating a cross-platform executable

Posted 15 April 2010 - 06:42 AM

Thanks sarmanu,
Wow, that looks complicated! Does that mean I have to have those preprocessor blocks in all the code that makes up the executable? If so, I don't think that's realistic, because I have over 200 files that make up the executables. Is there anything else I can try, like a wrapper of some kind?
Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Creating a cross-platform executable

Posted 15 April 2010 - 06:53 AM

Well, I don't think that you have to change too many things, because most of the header files are cross-platform (<iostream>, <string>, <cstdio>, <cstdlib>, etc ... the important header files). If you use many functions from windows specific headers (like <windows.h>), then you'd have to change...many things :)
Was This Post Helpful? 0
  • +
  • -

#5 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Creating a cross-platform executable

Posted 15 April 2010 - 06:57 AM

Ugh, that's what I was afraid of. The OS's we support are Windows, Mac, Linux 32 bit, and Linux 64 bit. So, there isn't a wrapper or something similar that could pick which one of the executables would run on a particular system?
Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1