Linux 执行二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9222407/
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
Executing Binary Files
提问by darksky
I downloaded a binary file that was compiled (a C program) using GCC 4.4.4 for x86-64 Red Hat Linux.
我下载了一个使用 GCC 4.4.4 for x86-64 Red Hat Linux 编译的二进制文件(一个 C 程序)。
Is it normal that when I try to run it on a Mac OS X (running Lion so also x86-64) running GCC 4.2.1 that it would say: cannot execute binary file
? It can't detect it as a binary file.
这是正常的,当我尝试在Mac OS X上运行(运行狮子也是如此的x86-64)运行GCC 4.2.1,它会说:cannot execute binary file
?它无法将其检测为二进制文件。
Why would it do that? I believe the gcc version has nothing to do with that since the file has already been compiled. It has been compiled for x86-64 of which both machines run. Can someone please explain?
为什么会那样做?我相信 gcc 版本与此无关,因为该文件已经编译。它是为 x86-64 编译的,其中两台机器都可以运行。有人可以解释一下吗?
采纳答案by Adrian Cornish
It's highly unlikely that a binary compiled for a particular OS will run on another. Either get a binary for Mac, or get the source and compile it yourself.
为特定操作系统编译的二进制文件极不可能在另一个操作系统上运行。要么获取 Mac 的二进制文件,要么获取源代码并自己编译。
There are many things that will cause issues - version of libc and libstdc++, there can be difference in versions of .so libraries - different API interface to the OS itself. Or even a different binary format (ie VMS binaries do not run on AIX).
有很多事情会导致问题 - libc 和 libstdc++ 的版本,.so 库的版本可能存在差异 - 操作系统本身的不同 API 接口。或者甚至是不同的二进制格式(即 VMS 二进制文件不在 AIX 上运行)。
回答by old_timer
binaries in this sense are compiled to make operating system calls, when your program has a printf() that boils down to operating system calls. If the operating system it was compiled for is say a 64 bit redhat linux then that likely means the binary is going to look for redhat linux names for shared libraries in redhat linux paths. Which has absolutely nothing in any way shape or form to do with a completely different operating system, Mac OS X, and its system calls and shared or static libraries, etc. Its like taking a wheel off of a mini cooper and trying to bolt it onto a bicycle. Yes at one point in time it was raw metal and rubber, and could have been formed into a bike tire. But once you make that binary, the car tire or the bike tire, that is what it is. sometimes you find an emulator like wine that emulates windows on top of a posix system. or a virtual machine like vmware that lets you run a whole different operating system on top of another by virtualizing a whole computer.
当您的程序具有可归结为操作系统调用的 printf() 时,这种意义上的二进制文件被编译为进行操作系统调用。如果它编译的操作系统是 64 位 redhat linux,那么这可能意味着二进制文件将在 redhat linux 路径中查找共享库的 redhat linux 名称。这与完全不同的操作系统 Mac OS X 及其系统调用和共享或静态库等完全没有任何形式或形式有关。 这就像从 mini cooper 上取下一个轮子并试图用螺栓固定它骑自行车。是的,在某个时间点,它是原始金属和橡胶,可以制成自行车轮胎。但是一旦你制作了那个二进制文件,汽车轮胎或自行车轮胎,就是这样。有时您会发现像 wine 这样的模拟器可以在 posix 系统之上模拟 windows。或者像 vmware 这样的虚拟机,通过虚拟化整台计算机,您可以在另一个操作系统之上运行完全不同的操作系统。
it is also true that you cannot generally expect to take any old C program and have it compile and run on any operating system that say has a gcc compiler. yes you can learn to write c programs that are portable, but you have to carefully stick to libraries that are supported on all the target platforms. so even taking the source code for your program to the mac and compiling it is not necessarily going to just work.
同样,您通常不能期望采用任何旧的 C 程序并让它在任何具有 gcc 编译器的操作系统上编译和运行。是的,您可以学习编写可移植的 c 程序,但是您必须小心地坚持使用所有目标平台都支持的库。因此,即使将您的程序的源代码带到 mac 并对其进行编译也不一定能正常工作。
回答by Daryl
Although Rad Hat Linux and Mac OS X are both 'Unix based', they cannot run each other's binaries. Just like you can't run Windows binaries on Macs and vice versa.
尽管 Rad Hat Linux 和 Mac OS X 都是“基于 Unix”的,但它们不能运行彼此的二进制文件。就像您不能在 Mac 上运行 Windows 二进制文件一样,反之亦然。