TWA
TWA

Reputation: 12816

Compiling C Program on OS X to Run on Linux

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?

Upvotes: 4

Views: 2133

Answers (2)

Klox
Klox

Reputation: 966

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.

Upvotes: 4

Erre Efe
Erre Efe

Reputation: 15557

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.

Upvotes: 1

Related Questions