14

I am running Linux Ubuntu 10.04 and I have a Windows 7 machine and a MacBook running Mac OS X 10.6.4. How can I write a simple C program (as in NOT QT!) like:

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("Hello Linux and Mac and Windows!")
    return 0;
}

to run on all my machines without having to compile this program on Ubuntu, then Windows 7, then Mac OS X? Could I just create this in Ubuntu and cross compile it to run on several different operating systems?
UPDATE
I do not mean to produce ONE binary to run on all. I mean to produce THREE binaries from the same C code in the same OS.

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
  • 1
    possible duplicate of [How to Compile for OS X in Linux or Windows?](http://stackoverflow.com/questions/693952/how-to-compile-for-os-x-in-linux-or-windows) – richq Sep 19 '10 at 01:32
  • I don't think so. I already own a Mac running OS X. – Mohit Deshpande Sep 19 '10 at 01:38
  • You will need to include a newline in the `printf()` statement to get reliable output. – Jonathan Leffler Sep 19 '10 at 01:50
  • A major part of the answer lies in choosing how you write your programs. Know the standards and the APIs for each of your platforms. Work out how you adapt the source to use the common APIs. Work out when there isn't common functionality native, and decide how you are going to resolve that. Most of my programs compile OK on Unix, Linux, MacOS X and Cygwin without much trouble - unless they are doing something that is not available on one of the platforms. – Jonathan Leffler Sep 19 '10 at 01:54
  • 1
    Specifically, "Could I just create this in Ubuntu and cross compile it to run on several different operating systems?" is where you duplicate "How to Compile for OS X in Linux or Windows?" Duplicate questions aren't a bad thing. –  Sep 19 '10 at 02:12
  • note that you would have to do the same thing(cross compiling) for qt(which can only be used w/c++ not c). The way qt helps is in providing cross platform apis instead of platform specific ones and using crossplatform makefile. – Roman A. Taycher Apr 03 '12 at 08:57

3 Answers3

14

An executable has a specific format (e.g. ELF) and architecture (e.g. x86). Thus, you do have to compile multiple times. However, it is possible to cross-compile to e.g. Windows 7 x86 and Mac OS X x86 from Ubuntu. The procedures for each are different, as you would expect.

For Windows, you will want mingw32. See Compile Windows C console applications in Linux .

For OS X, see How to compile Intel Mac binaries on Linux? , which links to a tutorial.

You can search to find more information on each.

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • For the OS X tutorial, it seems kind of old. Will it also work for the iPhone SDK? – Mohit Deshpande Sep 19 '10 at 01:43
  • @Mohit, I don't know, you asked about a MacBook. For Linux->iPhone, see [ Starting iPhone app development in Linux? ](http://stackoverflow.com/questions/276907/starting-iphone-app-development-in-linux); it's certainly not simple, though. – Matthew Flaschen Sep 19 '10 at 01:53
3

Unfortunately, the executable file formats used by Linux, Windows, and OSX are profoundly different in detail. There is no way to produce a single binary that works on all three.

It is possible to generate Windows and OSX executables using cross compilers from Linux (or vice versa in any other combination you like) but setting up the build environment is probably more trouble than it's worth. See http://www.kegel.com/crosstool/ if you really want to try that.

zwol
  • 135,547
  • 38
  • 252
  • 361
0

Cross-platform C Hello World! Desktop Windowed application. Windows / macOs / Linux. (Not C++, not QT)

https://nappgui.com/en/start/hello.html

frang
  • 69
  • 5