Compiling C Program on OS X to Run on Linux

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 09:55:18

问题


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:

  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.



来源:https://stackoverflow.com/questions/6990333/compiling-c-program-on-os-x-to-run-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!