4

I have a pretty simple C program that does some cryptographic calculations using only standard library functions.

I will be running the program on Ubuntu (10.04, 32 bit) and compiled it on OS X 10.6 using cc with the -m32 flag. When I tried to run it on Ubuntu I got the error message "cannot execute binary file."

When I compile it on Ubuntu it runs fine.

Is there any easy way to compile code on OS X into a binary that will run on Ubuntu? If not, what are the differences that cause the binary to be incompatible?

jww
  • 97,681
  • 90
  • 411
  • 885
TWA
  • 12,756
  • 13
  • 56
  • 92
  • Partial duplicate: http://stackoverflow.com/questions/5245444/cross-compiler-for-linux-on-mac-os-x This also links to [`crosstool-NG`](http://crosstool-ng.org). –  Aug 09 '11 at 01:12

2 Answers2

4

You need to set up a cross compiler. Using the GNU toolchain, the process looks something like this:

  1. Download binutils, gcc, and glibc.
  2. Untar bintuls (to something like binutils-x.y.z).
  3. mkdir binutils-linux
  4. cd binutils-linux
  5. ../binutils-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
  6. make
  7. sudo make install
  8. cd ..
  9. untar gcc (to something like gcc-x.y.z).
  10. mkdir gcc-linux
  11. cd gcc-linux
  12. ../gcc-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
  13. make
  14. sudo make install
  15. cd ..
  16. untar glibc (to something like glibc-x.y.z)
  17. mkdir glibc-linux
  18. cd glibc-linx
  19. ../glibc-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
  20. make
  21. sudo make install
  22. cd ..

I've never tried OSX as a host OS, so I don't know if there are any other complications, but this is the general approach. I'm working from memory, so add a comment if you need more help.

Klox
  • 931
  • 12
  • 21
  • Thanks for the info. I think I am going to install a copy of Ubuntu in VirtualBox to use for compiling. That seems like the easiest route. – TWA Aug 09 '11 at 01:53
  • You'd need the Linux kernel headers close to the distro you're targeting too. GLIBC can't interface to the kernel unless it's got those. – Adam Hawes Aug 09 '11 at 01:54
  • Note that it will take more than a few minutes to compile gcc... and it may take some investigation to get the right configure options for binutils and glibc too. – RBerteig Aug 09 '11 at 01:57
  • A virtual box sure looks like the *easier* way out. But think of all the geek cred you can score by doing it the hard way! – RBerteig Aug 09 '11 at 01:58
  • 2
    @RBerteig - lol, that is true. Fifteen years ago I would probably have gone for the geek cred. These days I just want to get stuff done. :) – TWA Aug 09 '11 at 02:03
1

I'm afraid you can't given the minimum portability of gcc.

Of course you can build a cross compiler like this but I'll suggest you to use and compile with an ubuntu virtual machine.

Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • You can! I've done just this before, successfully. GCC is highly portable. It's the supporting structures (libraries and kernel interfaces) that are not portable. It's not particularly easy to build your cross compiler and there's a lot of mines in the field for the unwary. Given the ubiquity of virtual machines these days it's easier and safer to just use one. – Adam Hawes Aug 09 '11 at 01:54