问题
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?
回答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.
回答2:
You need to set up a cross compiler. Using the GNU toolchain, the process looks something like this:
- Download binutils, gcc, and glibc.
- Untar bintuls (to something like binutils-x.y.z).
- mkdir binutils-linux
- cd binutils-linux
- ../binutils-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
- make
- sudo make install
- cd ..
- untar gcc (to something like gcc-x.y.z).
- mkdir gcc-linux
- cd gcc-linux
- ../gcc-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
- make
- sudo make install
- cd ..
- untar glibc (to something like glibc-x.y.z)
- mkdir glibc-linux
- cd glibc-linx
- ../glibc-x.y.z/configure --target=i386-ubuntu-linux (not sure on the exact target)
- make
- sudo make install
- 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.
来源:https://stackoverflow.com/questions/6990333/compiling-c-program-on-os-x-to-run-on-linux