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

发布于 2024-07-13 21:40:17 字数 486 浏览 4 评论 0原文

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

这一切都源于我原来的 perl 跨平台问题 此处

I been looking into Cygwin/Mingw/lcc and 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

懒的傷心 2024-07-20 21:40:18

(这是一个非常古老的问题,但缺少一些有用的信息 -
我个人曾在 Solaris(SPARC 和 x86)、AIX、HP-UX 和 Linux(x86、x64)上执行此操作。)

  • 获得 C++ 交叉编译比直接 C 困难得多。

    p>

  • 不支持 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/binutuils,但如果您无法解决问题,请尝试退回到较旧的软件包。 例如,对于 Power3 (AIX) gcc 4.x 系列交叉编译器会生成错误代码,3.x 就可以。

  • 复制本机库和标头时,请确保从可能运行的最旧的计算机进行复制。 复制新的 libc 意味着您的代码将无法在任何具有较旧 libc 的计算机上运行。

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

(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).)

  • 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 exec and stat are fast operations. Windows + NTFS is not that environment.

  • Start with the crosstools script, but be prepared to spend a lot of 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

策马西风 2024-07-20 21:40:18

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

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

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

而且这很可能是不可能的 - perl 和共享库可以使用本机/非 gcc 编译器进行编译,而这在 Windows 上根本不可用。

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.

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.

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.

沫雨熙 2024-07-20 21:40:18

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

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

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

我已成功在 Windows 上的 VM 中运行 Solaris 10 和 Open Solaris。 不幸的是,您在虚拟机下运行 HPUX 可能会遇到困难。

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.

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.

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.

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.

药祭#氼 2024-07-20 21:40:18

您为什么不阅读“Grand Unified Builder”(http://lilypond.org/gub/< /a> 和 http://valentin.villenave.info/The-LilyPond-Report- 11(第 #4 节))

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

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))

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

风吹雪碎 2024-07-20 21:40:18

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

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

一向肩并 2024-07-20 21:40:18

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

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

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

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.

那请放手 2024-07-20 21:40:18

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文