如何在 Windows 上交叉编译 C 代码以使二进制文件也能在 Unix (Solaris/HPUX/Linux) 上运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/579695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:02:16  来源:igfitidea点击:

How do I cross-compile C code on Windows for a binary to also be run on Unix (Solaris/HPUX/Linux)?

cwindowsunixcross-platformcross-compiling

提问by Ville M

I been looking into Cygwin/Mingw/lccand I liked to be able to compile perl native C extensions on my windows(preferably under cygwin) and then run them on Solaris and HP unix without any further fuss, is this possible?

我一直在研究Cygwin/ Mingw/ lcc,我喜欢能够在我的 Windows(最好在 cygwin 下)编译 perl 本机 C 扩展,然后在 Solaris 和 HP unix 上运行它们而无需大惊小怪,这可能吗?

This all stems from my original perl cross-platform question here.

这一切都源于我在这里的原始 perl 跨平台问题。

采纳答案by Douglas Leeder

Cross-compiler are very hard to setup and get working correctly.

交叉编译器很难设置和正常工作。

Consider that (the people at) NetBSD have to put in a huge amount of work to get cross-compiling to work, and they're running the same OS, just different architectures.

考虑到(在)NetBSD 的人必须投入大量工作才能使交叉编译工作,而且他们运行相同的操作系统,只是架构不同。

You'd have to, at least, copy all the headers from the other OSs to Windows, and get a cross-compiler, linker etc for the target OS/architecture.

您至少必须将所有头文件从其他操作系统复制到 Windows,并为目标操作系统/体系结构获取交叉编译器、链接器等。

Also that may well not be possible - perl and shared libraries may be compiled with a native/non-gcc compiler which won't be available on Windows at all.

这也很可能是不可能的——perl 和共享库可能是用原生/非 gcc 编译器编译的,而这些编译器在 Windows 上根本不可用。

回答by Nathan Kidd

(This is a very old question, but missing some useful info -- I've personally done this for Solaris (SPARC & x86), AIX, HP-UX and Linux (x86, x64).)

(这是一个非常古老的问题,但缺少一些有用的信息——我个人为 Solaris(SPARC 和 x86)、AIX、HP-UX 和 Linux(x86、x64)做过这个。)

  • Getting C++ cross-compiled is much harder than straight C.

  • HP-UX 32-bit PA-RISC is not supported because it uses SOM format instead of ELF and binutils doesn't (and likely won't ever) support SOM. In other words, you can only cross-compile 64-bit PA-RISC. (Requires PA-RISC 2.0 chip.)

  • I would go with mingw instead of cygwin, if you can. Cygwin introduces a lot of file permission headaches and cygwin1.dll dependencies that can be troublesome. If possible, however, build on linux. Everything will be much faster because all the tools and scripts you're running are designed for an environment where execand statare fast operations. Windows + NTFS is not that environment.

  • Start with the crosstools script, but be prepared to spend a lotof time on this.

  • Try with the very latest gcc/binutuils first, but if you can't overcome problems try dropping back to older packages. E.g. for Power3 (AIX) gcc 4.x series cross compiler generates bad code, 3.x is fine.

  • When copying native libs and headers make sure you are copying from the oldest machine you're likely to run on. Copying a new libc means your code won't run on any machine with an older libc.

  • When copying native libs and headers you probably want 'tar -h' to turn symlinks into actual files, also watch that on Solaris some requisite crt object files are buried in a cc directory, not under /usr/lib

  • 让 C++ 交叉编译比直接 C 更难。

  • 不支持 HP-UX 32 位 PA-RISC,因为它使用 SOM 格式而不是 ELF,并且 binutils 不(并且可能永远不会)支持 SOM。换句话说,您只能交叉编译 64 位 PA-RISC。(需要 PA-RISC 2.0 芯片。)

  • 如果可以的话,我会使用 mingw 而不是 cygwin。Cygwin 引入了许多令人头疼的文件权限问题和可能很麻烦的 cygwin1.dll 依赖项。但是,如果可能,请在 linux 上构建。一切都将是更快,因为所有的工具和脚本,你正在运行的设计用于环境execstat速度快的操作。Windows + NTFS 不是那个环境。

  • 从 crosstools 脚本开始,但要准备好花大量时间在这上面。

  • 首先尝试使用最新的 gcc/binutuls,但如果您无法克服问题,请尝试回退到较旧的软件包。例如,对于 Power3 (AIX) gcc 4.x 系列交叉编译器生成错误代码,3.x 很好。

  • 复制本机库和头文件时,请确保从可能运行的最旧机器上复制。复制新的 libc 意味着您的代码不会在任何具有旧 libc 的机器上运行。

  • 复制本机库和头文件时,您可能希望“tar -h”将符号链接转换为实际文件,还要注意在 Solaris 上,一些必需的 crt 对象文件被埋在 cc 目录中,而不是在 /usr/lib 下

回答by brianegge

I agree with Douglas, that getting a cross compiler up and working is very hard to do. This is generally, your choice of last resort. If you are boot strapping, or making a binary for an embedded device, then often cross-compiling is your only option. You should be comfortable compiling your own gcc under Cygwin before considering cross compiling. To cross compile, you need to build a gcc to run under windows, but which will create binaries for your execution platform. Sample instructions for doing this can be found here.

我同意Douglas 的观点,即启动交叉编译器并使其工作非常困难。这通常是您最后的选择。如果您正在引导程序或为嵌入式设备制作二进制文件,那么交叉编译通常是您唯一的选择。在考虑交叉编译之前,您应该能够在 Cygwin 下编译自己的 gcc。要交叉编译,您需要构建一个 gcc 以在 Windows 下运行,但这将为您的执行平台创建二进制文件。可以在此处找到执行此操作的示例说明。

Perhaps you are wanting to cross compile because you don't have root and/or can't compile on your target platform. For example, I had a hosting provider which ran Redhat Linux. I could run Perl CGI scripts, and associated modules, but I could not compile on the target machine, and an libraries I built had to exist in my own directory.

也许您想要交叉编译,因为您没有 root 和/或无法在目标平台上编译。例如,我有一个运行 Redhat Linux 的托管服务提供商。我可以运行 Perl CGI 脚本和相关模块,但我无法在目标机器上编译,而且我构建的库必须存在于我自己的目录中。

To solve this, I could have attempted to cross compile for my target platform, but instead, I decided to setup a similar host inside a VM on Windows. From within Cygwin, you can create a script which ssh's into your VM, copies your source, and does a full configure/build. The last step was to deploy the binary artifact onto my hosted system.

为了解决这个问题,我本可以尝试为我的目标平台交叉编译,但相反,我决定在 Windows 上的 VM 中设置一个类似的主机。在 Cygwin 中,您可以创建一个脚本,该脚本通过 ssh 连接到您的 VM、复制您的源代码并执行完整的配置/构建。最后一步是将二进制工件部署到我的托管系统上。

I've successfully had both Solaris 10 and Open Solaris running within a VM on Windows. Unfortunately, you might have a harder time running HPUX under a VM.

我已经成功地在 Windows 上的 VM 中运行了 Solaris 10 和 Open Solaris。不幸的是,在 VM 下运行 HPUX 可能会更困难。

回答by masher

Why don't you have a read up on "Grand Unified Builder" (http://lilypond.org/gub/and http://valentin.villenave.info/The-LilyPond-Report-11(section #4))

为什么不阅读“Grand Unified Builder”(http://lilypond.org/gub/http://valentin.villenave.info/The-LilyPond-Report-11(第 4 部分))

I don't know how it works, but GUB allows the Lilypond developers to compile for about 11 platforms on a linux box.

我不知道它是如何工作的,但是 GUB 允许 Lilypond 开发人员在 linux 机器上为大约 11 个平台进行编译。

回答by SumoRunner

Compile on Windows then use Wine to run them on any *nix. It works well most of the time.

在 Windows 上编译,然后使用 Wine 在任何 *nix 上运行它们。大多数时候它运行良好。

回答by Tommy Hui

No, this isn't possible at the binary level. There are so many differences at binary level between the various OSes and CPUs.

不,这在二进制级别是不可能的。各种操作系统和 CPU 之间在二进制级别上有很多差异。

But what you can do is make the your C extensions source compatible so that it can compile to different platforms. C was designed as a "portable assembly language". As long as you stick with routines that are cross-platform, then they will usually work the same. You'll still need to test because there could be bugs that exists on particular platform.

但是您可以做的是使您的 C 扩展源兼容,以便它可以编译到不同的平台。C 被设计为“便携式汇编语言”。只要您坚持使用跨平台的例程,它们通常会以相同的方式工作。您仍然需要进行测试,因为特定平台上可能存在错误。

回答by Beep beep

This can't be done ... but is it that much of a hassle to recompile the code under Solaris or HP?

这是做不到的……但是在 Solaris 或 HP 下重新编译代码有那么麻烦吗?