在机器的什么范围二进制兼容性? [英] Binary compatibility over what range of machines?

查看:126
本文介绍了在机器的什么范围二进制兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C简单的程序,并在Ubuntu上使用GCC编译它。将另一台机器上这个文件的工作?

I wrote a simple program in C and compiled it using GCC on Ubuntu. Will this file work on another machine?


  • 什么是输出二进制文件的内容,它的外部依赖?

  • 可以压在其它Linux发行版上运行,并且在什么情况下?

  • 可不可以在其他操作系统上运行?

推荐答案

有几个层次/二进制不兼容的来源。

There are several levels/sources of binary incompatibility.

通常情况下,如果你正在运行另一台机器上的二进制,使用不同版本的相同的OS(这意味着什么......),那么它将会运行得很好。彻底。这个问题是不是与code不是工作,而是缺少code:的二进制取决于操作系统的位是不是有在目标机器上。这是找到code跑,不要跑code失败(这将是完全确定,直到它试图使用缺少的位点!)

Normally, if you're running a binary on another machine, with a different version of the "same OS" (whatever that means...), then it will run fine. Completely. The issue isn't with code not working, but missing code: the bits of the OS that the binary depends on that aren't there on the target machine. It's a failure to find code to run, not to run code (which would be perfectly OK, until the point where it tried to use the missing bit!)

所以,从你的Ubuntu的gcc二进制文件任何Linux系统,没有比它编上机年长的最有可能的运行。这取决于什么功能二进制从操作系统和系统库依赖。

So, binaries from your Ubuntu gcc will most likely run on any linux system that's no older than the machine it was compiled on. It depends on exactly what functionality the binary depends on from the OS and system libraries.

很少二进制文件没有外部依赖性。检查使用 LDD 你输出的依赖关系。一个最可能出问题的是libgcc的依赖。 libc和朋友变化非常频繁,所以很少会导致兼容性困难。 GCC经常变化,所以会限制你的二进制文件将运行在目标计算机上。

Very few binaries have no external dependencies. Examine the dependencies using ldd on your output. The one most likely to cause problems is the libgcc dependency. libc and friends change very infrequently, so hardly ever cause difficulties with compatibility. GCC changes often, so will restrict the target machines your binaries will run on.

的一般规则是:用作为构建的机器要运行中最早发行。所以,用一个RHEL 2构建机器,你就可以运行任何系统上的二进制年龄不(有一些例外)。这是一个共同的方针,保持简单的事情。如果你需要在许多不同的发行版的二进制兼容性,静态链接与libstdc ++(如果你使用C ++)是一个很好的选择。静态链接为libgcc是危险的,而不是尝试除非你真的知道你在做什么。

The general rule is: use as your build machine the oldest distro you want to run on. So, with a RHEL 2 build machine, you'll be able to run the binary on any system no older (with some exceptions). That's a common guideline that keeps things simple. If you need binary compatibility across lots of distros, statically linking to libstdc++ (if you use C++) is an excellent choice. Statically linking to libgcc is dangerous, and not to be attempted unless you really know what you're up to.

最后,请注意库的兼容性是其它UNIX平台更简单;它只是说是这样一个痛苦的Linux。凡是在AIX 6(说),或在SunOS 5.8或HP-UX 11.00或任何编译所有更高版本没有任何问题运行。环境是不够均匀的,他们可以只在他们的系统库遗留的克鲁夫特成堆的出货,以保证对老版本每一个符号present可在最新一期,具有相同的语义。

Finally, note that library compatibility is much simpler on other UNIX platforms; it's only linux that's such a pain. Anything compiled on AIX 6 (say) or SunOS 5.8 or HP-UX 11.00 or whatever runs with no issues on all later releases. The environment is homogenous enough that they can just ship with piles of legacy cruft in their system libraries, to guarantee that every symbol present on the old release is available on the latest one, with the same semantics.

这一切都只是机器code,所以你可能会认为二进制文件应该在其他操作系统的工作。他们不会。一个很大的原因是系统调用:当你需要调用从内核的东西(这是必要的最不平凡的功能),你必须知道如何说话内核。也就是说,你做一个系统调用,并告诉操作系统,做42件事,你知道。

It's all just machine code, so you might think binaries should work on other OSes. They won't. A big reason is system calls: when you need to invoke something from the kernel (which is necessary for most non-trivial functionality), you have to know how to "talk to the kernel". That is, you make a syscall and tell the OS, "do the 42 thing, you know".

对于系统调用的数字取决于内核。因此,在Solaris二进制是从Linux OK,除了那个挑剔的停止pretty太多一切工作正常。

The numbers for the syscalls depend on the kernel. So, the Solaris binary would be OK from linux, apart from that niggle that stops pretty much everything working.

在事实上,一些操作系统也支持其他内核的系统调用。 FreeBSD的知道linux的系统调用,当你告诉它做42件事,它在你的ELF的头部检查一个标志,并且确实为Linux系统调用号进行相应的操作,如果ELF被加盖作为Linux的。整齐。 (当然,你需要在Linux库以及链接......但是,静态链接的Linux二进制将在FreeBSD下运行良好,没有外部)。

In fact, some OSes do support the syscalls of other kernels. FreeBSD knows the linux syscalls, and when you tell it "do the 42 thing", it checks a flag in the header of your ELF, and does the appropriate action for the linux syscall numbers if the ELF is stamped as a linux one. Neat. (Of course, you need to link in linux libs as well... But, a statically linked linux binary will run fine in FreeBSD with no externals.)

一个二进制是不是只是一个二进制它包含元数据,获取内核PTED才可以运行的东西之间​​的$ P $的整个负载。大多数二进制为code和数据,以及连接信息的斑点。但是,即使是一些不具有任何外部的依赖必须由内核解析的。

A binary isn't "just a binary"; it contains a whole load of meta-data that gets interpreted by the kernel before it can run the thing. Most of the binary is a blob of code and data, plus linker information. But, even something that doesn't have any external dependencies has to be parseable by the kernel.

有些内核了解多个二进制格式,如PE,ELF,或a.out的(过时)。 Linux程序将永远不会在Windows上运行,即使他们没有进行任何系统调用(比如一个二进制不能完全退出,但例子的缘故...)。这是因为MS是不是要ELF支持添加到他们的核心:围绕code描述它是如何被加载并开始无法被Windows读取的包装,所以它里面的code赢了 T为运行:Windows不知道该文件的位甚至code

Some kernels understand multiple binary formats, such as PE, ELF, or a.out (obsolete). The linux binaries will never run on Windows, even if they don't make any syscalls (such a binary can't exit cleanly, but for the sake of example...). That's because MS is not about to add ELF support to their kernel: the wrapper around the code that describes how it is to be loaded and started can't be read by Windows, so the code inside it won't be run: Windows doesn't know which bits of the file are even code!

这篇关于在机器的什么范围二进制兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆