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.
我几乎可以肯定我的问题是由于编译的程序被编译为linux可执行文件,但我只想仔细检查一下。
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return EXIT_SUCCESS;
}
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.
上面的“程序”应该在Windows和Linux上编译得很好,因为它与源代码兼容,因为没有特定于操作系统的库或类似的东西。
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?
然而,当我在我的Linux机器上键入“c99 hello.c -o hello.exe”,然后将该“可执行文件”传输到Windows机器时,它拒绝运行。根据我的理解,Linux生成一个只能在linux上运行的可执行文件,因此添加“.exe”无效。要在Linux for Windows上构建该程序,我需要在Windows机器上重新编译该程序吗?或者还有另一种更简单的方法吗?
2 个解决方案
#1
20
Windows and Linux executable files use two different incompatible formats:
Windows和Linux可执行文件使用两种不同的不兼容格式:
On Windows, it is the Portable Executable format.
在Windows上,它是可移植可执行文件格式。
On Linux it is the ELF (Executable and Linkable 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.
每种格式都使用了它们应该运行的操作系统的特性,因此它们通常不能在另一个平台上执行。
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的程序不能在FreeBSD上运行,尽管后者也使用ELF,因为它们没有链接到相同的库。
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.
要在Linux上编译Windows可执行文件,可以使用称为交叉编译的技术。编译器毕竟只是一个写入二进制文件的程序,因此任何编译器理论上都可以为任何平台编写代码,只要目标系统的库可以链接即可。
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.
MinGW-w64项目提供了一个允许这样做的工具链。例如,您可以使用sudo apt-get install mingw-w64命令在基于debian的系统上安装它。
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
#2
0
Its a compilation situation, diferently from java .exe don't have any virtual machine to interpret the code in diferent OS. Then he needs to have all specific .dll's and lib's from OS to execute the job who is made for.
它是一个编译情况,与java .exe不同,没有任何虚拟机来解释不同操作系统中的代码。然后他需要从操作系统中获取所有特定的.dll和lib来执行所需的工作。
In .exe all things are made to run on a specific OS, other way in Java a virtual machine is the magic who interpret and run your code on diferents OS.
在.exe中,所有东西都在特定的操作系统上运行,而在Java中,虚拟机是在不同的操作系统上解释和运行代码的神奇之处。
#1
20
Windows and Linux executable files use two different incompatible formats:
Windows和Linux可执行文件使用两种不同的不兼容格式:
On Windows, it is the Portable Executable format.
在Windows上,它是可移植可执行文件格式。
On Linux it is the ELF (Executable and Linkable 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.
每种格式都使用了它们应该运行的操作系统的特性,因此它们通常不能在另一个平台上执行。
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的程序不能在FreeBSD上运行,尽管后者也使用ELF,因为它们没有链接到相同的库。
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.
要在Linux上编译Windows可执行文件,可以使用称为交叉编译的技术。编译器毕竟只是一个写入二进制文件的程序,因此任何编译器理论上都可以为任何平台编写代码,只要目标系统的库可以链接即可。
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.
MinGW-w64项目提供了一个允许这样做的工具链。例如,您可以使用sudo apt-get install mingw-w64命令在基于debian的系统上安装它。
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
#2
0
Its a compilation situation, diferently from java .exe don't have any virtual machine to interpret the code in diferent OS. Then he needs to have all specific .dll's and lib's from OS to execute the job who is made for.
它是一个编译情况,与java .exe不同,没有任何虚拟机来解释不同操作系统中的代码。然后他需要从操作系统中获取所有特定的.dll和lib来执行所需的工作。
In .exe all things are made to run on a specific OS, other way in Java a virtual machine is the magic who interpret and run your code on diferents OS.
在.exe中,所有东西都在特定的操作系统上运行,而在Java中,虚拟机是在不同的操作系统上解释和运行代码的神奇之处。