简体   繁体   English

在OS X上编译C程序以在Linux上运行

[英]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. 我有一个非常简单的C程序,只使用标准库函数进行一些加密计算。

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. 我将在Ubuntu(10.04,32位)上运行该程序,并使用带有-m32标志的cc在OS X 10.6上编译它。 When I tried to run it on Ubuntu I got the error message "cannot execute binary file." 当我试图在Ubuntu上运行它时,我收到错误消息“无法执行二进制文件”。

When I compile it on Ubuntu it runs fine. 当我在Ubuntu上编译它时运行正常。

Is there any easy way to compile code on OS X into a binary that will run on Ubuntu? 有没有简单的方法将OS X上的代码编译成可在Ubuntu上运行的二进制文件? If not, what are the differences that cause the binary to be incompatible? 如果没有,导致二进制文件不兼容的区别是什么?

You need to set up a cross compiler. 您需要设置交叉编译器。 Using the GNU toolchain, the process looks something like this: 使用GNU工具链,该过程看起来像这样:

  1. Download binutils , gcc , and glibc . 下载binutilsgccglibc
  2. Untar bintuls (to something like binutils-xyz). Untar bintuls(像binutils-xyz这样的东西)。
  3. mkdir binutils-linux mkdir binutils-linux
  4. cd binutils-linux cd binutils-linux
  5. ../binutils-xyz/configure --target=i386-ubuntu-linux (not sure on the exact target) ../binutils-xyz/configure --target = i386-ubuntu-linux(不确定确切的目标)
  6. make 使
  7. sudo make install sudo make install
  8. cd .. cd ..
  9. untar gcc (to something like gcc-xyz). 解开gcc(像gcc-xyz这样的东西)。
  10. mkdir gcc-linux mkdir gcc-linux
  11. cd gcc-linux cd gcc-linux
  12. ../gcc-xyz/configure --target=i386-ubuntu-linux (not sure on the exact target) ../gcc-xyz/configure --target = i386-ubuntu-linux(确切的目标不确定)
  13. make 使
  14. sudo make install sudo make install
  15. cd .. cd ..
  16. untar glibc (to something like glibc-xyz) 解开glibc(像glibc-xyz这样的东西)
  17. mkdir glibc-linux mkdir glibc-linux
  18. cd glibc-linx cd glibc-linx
  19. ../glibc-xyz/configure --target=i386-ubuntu-linux (not sure on the exact target) ../glibc-xyz/configure --target = i386-ubuntu-linux(确切的目标不确定)
  20. make 使
  21. sudo make install sudo make install
  22. cd .. 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. 我从来没有尝试过OSX作为主机操作系统,所以我不知道是否还有其他任何复杂情况,但这是一般方法。 I'm working from memory, so add a comment if you need more help. 我正在从记忆中工作,所以如果你需要更多的帮助,请添加评论。

I'm afraid you can't given the minimum portability of gcc. 我担心你不能给出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. 当然,你可以建立一个交叉编译器是这样 ,但我会建议你使用和编译一个Ubuntu虚拟机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM