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

后端 未结 7 1953
我在风中等你
我在风中等你 2020-12-10 19:12

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 un

相关标签:
7条回答
  • 2020-12-10 19:30

    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.

    0 讨论(0)
  • 2020-12-10 19:32

    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.

    0 讨论(0)
  • 2020-12-10 19:36

    (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

    0 讨论(0)
  • 2020-12-10 19:41

    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.

    0 讨论(0)
  • 2020-12-10 19:42

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

    0 讨论(0)
  • 2020-12-10 19:46

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

    0 讨论(0)
提交回复
热议问题