简体   繁体   English

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

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

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? 我一直在研究Cygwin / Mingw / lcc ,我喜欢能够在我的Windows上编译perl本机C扩展(最好是在cygwin下),然后在Solaris和HP unix上运行它们而不用进一步大惊小怪,这可能吗?

This all stems from my original perl cross-platform question here . 这一切都从我原来的perl的跨平台问题源于这里

(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. 获得C ++交叉编译要比直接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. 不支持HP-UX 32位PA-RISC,因为它使用SOM格式而不是ELF,而binutils不支持(也可能不支持)SOM。 In other words, you can only cross-compile 64-bit PA-RISC. 换句话说,您只能交叉编译64位PA-RISC。 (Requires PA-RISC 2.0 chip.) (需要PA-RISC 2.0芯片。)

  • I would go with mingw instead of cygwin, if you can. 如果可以的话,我会选择mingw而不是cygwin。 Cygwin introduces a lot of file permission headaches and cygwin1.dll dependencies that can be troublesome. Cygwin引入了许多文件权限头痛和cygwin1.dll依赖,这可能很麻烦。 If possible, however, build on linux. 但是,如果可能的话,建立在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. 一切都会快得多,因为您运行的所有工具和脚本都是针对execstat快速操作的环境而设计的。 Windows + NTFS is not that environment. Windows + NTFS不是那种环境。

  • Start with the crosstools script, but be prepared to spend a lot of time on this. 从crosstools脚本开始,但要准备好花费大量时间。

  • Try with the very latest gcc/binutuils first, but if you can't overcome problems try dropping back to older packages. 首先尝试使用最新的gcc / binutuils,但如果你无法解决问题,请尝试使用旧版软件包。 Eg for Power3 (AIX) gcc 4.x series cross compiler generates bad code, 3.x is fine. 例如,对于Power3(AIX),gcc 4.x系列交叉编译器生成错误的代码,3.x很好。

  • 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. 复制新的libc意味着您的代码将无法在具有较旧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 复制本机库和头文件时,您可能希望'tar -h'将符号链接转换为实际文件,同时在Solaris上观察一些必需的crt目标文件隐藏在cc目录中,而不是在/ usr / lib下

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上根本不可用。

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. 在考虑交叉编译之前,您应该在Cygwin下编译自己的gcc。 To cross compile, you need to build a gcc to run under windows, but which will create binaries for your execution platform. 要交叉编译,您需要构建一个在Windows下运行的gcc,但它将为您的执行平台创建二进制文件。 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. 也许您想要交叉编译,因为您没有root和/或无法在目标平台上编译。 For example, I had a hosting provider which ran Redhat Linux. 例如,我有一个运行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. 我可以运行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. 为了解决这个问题,我本可以尝试为我的目标平台进行交叉编译,但是我决定在Windows上的VM中设置类似的主机。 From within Cygwin, you can create a script which ssh's into your VM, copies your source, and does a full configure/build. 在Cygwin中,您可以创建一个ssh进入VM的脚本,复制源代码,并进行完整的配置/构建。 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. 我已成功在Windows上的VM中运行Solaris 10和Open Solaris。 Unfortunately, you might have a harder time running HPUX under a VM. 不幸的是,您可能更难在VM下运行HPUX。

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个平台。

Compile on Windows then use Wine to run them on any *nix. 在Windows上编译然后使用Wine在任何* nix上运行它们。 It works well most of the time. 它大部分时间都很好用。

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扩展源兼容,以便它可以编译到不同的平台。 C was designed as a "portable assembly language". C被设计为“便携式汇编语言”。 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. 您仍然需要进行测试,因为特定平台上可能存在错误。

这是无法做到的......但是在Solaris或HP下重新编译代码是一件很麻烦的事吗?

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

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