为什么一个Linux编译的程序无法在Windows工作 [英] Why does a linux compiled program not work on Windows

查看:164
本文介绍了为什么一个Linux编译的程序无法在Windows工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎可以肯定我的问题是由于一个事实,即编译后的程序被编译为一个Linux可执行文件,但我只是想仔细检查这一点。

I am almost sure my problem is due to the fact that the compiled program is compiled as a linux executable, but I just want to double check this.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello world!\n");
    return EXIT_SUCCESS;
}

上面的程序应该编译Windows和Linux上就好了,因为它是源$ C ​​$ C兼容,因为没有操作系统特定的库或类似的东西。

The above "program" should compile on Windows and Linux just fine, since it is source code compatible, as there are no operating system specific libraries or anything like that.

然而,当我在C99的hello.c -o文件hello.exe在我的Linux机器类型,然后转移的可执行文件到Windows机器,它拒绝运行。据我所知,Linux的产生,只有在Linux上运行的可执行文件,因此添加名为.exe没有效果。要在Linux上构建的Windows那个节目,我需要重新编译Windows机器上的程序?
还是有另一种,将工作更简单的方法?

Yet, when I type in "c99 hello.c -o hello.exe" on my Linux box, and then transfer that "executable" to a windows machine, it refuses to run. From what I understand, Linux generates an executable file that only runs on linux, so adding ".exe" has no effect. To build that program on Linux for Windows, I would need to recompile that program on a Windows machine? Or is there another simpler method that will work?

推荐答案

Windows和Linux的可执行文件使用两种不同的不兼容的格式:

Windows and Linux executable files use two different incompatible formats:

在Windows上,它是移植可执行格式

On Windows, it is the Portable Executable format.

在Linux上它是 ELF(可执行和链接格式)

每个格式,使他们都应该运行在操作系统的特殊性用途,所以它们通常不能在其他平台上执行。

Each format makes uses of the specificities of the OS they are supposed to run on, so they can't normally be executed on another platform.

此外,可执行只包含其加载到内存之后执行code的一小部分。可执行文件被链接到提供大部分的该允许程序与系统进行交互的功能系统库。结果
随着系统彼此,库而变化,并且对于比方说,Linux的一个方案非常不同,则不能在FreeBSD尽管后者也使用的ELF运行,因为它们不连接到相同的库

Also, an executable only contains a small portion of the code that is executed after it is loaded into memory. An executable file is linked to system libraries that provide most of the functionality that allows the program to interact with the system.
As systems are very different from each other, libraries vary, and a program for say, Linux, cannot be run on FreeBSD despite the latter using also ELF, because they are not linked to the same libraries.

要编译Linux上的Windows可执行文件,可以使用公知作为交叉编译的技术。
编译器毕竟只是写入到一个二进制文件的程序,因此,任何编译器理论上可以写code任何平台,只要在目标系统的库可对被链接。

To compile a Windows executable on Linux, a technique known as cross-compilation can be used. A compiler is after all just a program that writes into a binary file, so any compiler can in theory write code for any platform, as long as the target system's libraries are available to be linked against.

借助的MinGW-W64 项目提供了一个工具链,让这一点。
例如,您可以使用在基于Debian的系统上安装它命令和apt-get安装的MinGW-W64 命令。

The MinGW-w64 project provides a toolchain that allows this. For example, you can install it on debian-based systems using the sudo apt-get install mingw-w64 command.

安装的可执行文件,可以使用这样的:

The installed executables can be used like this:

i686-w64-mingw32-gcc hello.c -o hello32.exe      # 32-bit
x86_64-w64-mingw32-gcc hello.c -o hello64.exe    # 64-bit

这篇关于为什么一个Linux编译的程序无法在Windows工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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