(C/C++) 如何生成可以在 Windows 和 Linux 上运行的可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7433663/
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
(C/C++) How to generate executable file that can run on both Windows and Linux?
提问by
I'm new in programming. From what i know, a program that is compiled in Linux should not be able to run in Windows. So, if we want to run the same program in both platform, we need to compile the source code under both platform, and create 2 different executable files.
我是编程新手。据我所知,在 Linux 中编译的程序应该无法在 Windows 中运行。所以,如果我们想在两个平台上运行同一个程序,我们需要在两个平台下编译源代码,并创建2个不同的可执行文件。
Recently I am studying the source code of an open source, cross-platform Java GUI application. I'm surprised to find that the backend engine of that GUI application is a small executable file generated from C++ codes. No matter users use that application in Windows or Linux, the Java GUI will call the same executable file in the bin folder.
最近我正在研究一个开源的、跨平台的 Java GUI 应用程序的源代码。我惊讶地发现该 GUI 应用程序的后端引擎是一个由 C++ 代码生成的小型可执行文件。无论用户在 Windows 还是 Linux 中使用该应用程序,Java GUI 都会调用 bin 文件夹中相同的可执行文件。
I just want to know, how can that executable file run on both Windows and Linux?
我只想知道,该可执行文件如何在 Windows 和 Linux 上运行?
I'm also interested to create a cross-platform Java GUI application using a C++ program as the engine. That C++ program is only available for Linux only. I've been googling for a while and i found that I need to use Cygwin to port it to Windows. However, if i use Cygwin, i will end up having 2 different executable files.
我也有兴趣创建一个使用 C++ 程序作为引擎的跨平台 Java GUI 应用程序。该 C++ 程序仅适用于 Linux。我一直在谷歌搜索一段时间,我发现我需要使用 Cygwin 将它移植到 Windows。但是,如果我使用 Cygwin,我最终将拥有 2 个不同的可执行文件。
How can i combine the Windows executable file with the Linux executable file? Is it possible to generate a single executable file that can run on both platform?
如何将 Windows 可执行文件与 Linux 可执行文件结合起来?是否可以生成可以在两个平台上运行的单个可执行文件?
Thanks.
谢谢。
采纳答案by David Heffernan
There is no way to have a single native executable compiled from C++ source that works on different platforms. I don't believe you that the same executable file is run on Windows and Linux, as you state in your second paragraph.
没有办法从 C++ 源代码编译一个可在不同平台上运行的本机可执行文件。正如您在第二段中所述,我不相信您会在 Windows 和 Linux 上运行相同的可执行文件。
回答by Adam Leyshon
The simple answer is, is that you can't.
简单的答案是,你不能。
The PE (Windows) and ELF (Linux) binary executable formats are totally different.
PE (Windows) 和 ELF (Linux) 二进制可执行格式完全不同。
Not to mention that a C/C++ Program requires linking to libraries that won't be available on either platform simultaneously.
更不用说 C/C++ 程序需要链接到两个平台上都无法同时使用的库。
However, you could use Wine on Linux to run the Windows executable providing it doesn't attempt exotic Windows specific calls.
但是,您可以在 Linux 上使用 Wine 来运行 Windows 可执行文件,前提是它不会尝试特殊的 Windows 特定调用。
Or you could choose to use a more "common" cross-platform language such as a CLI language (C#/IronPython/Java etc.) that .NET for Windows and Mono for linux/others supports.
或者您可以选择使用更“常见”的跨平台语言,例如 CLI 语言(C#/IronPython/Java 等),Windows 的 .NET 和 linux/其他人的 Mono 支持。
回答by Vineet Menon
You can't do that....i.e. you can't run the same executable on both Unix as well as Windows.
你不能那样做……也就是说,你不能在 Unix 和 Windows 上运行相同的可执行文件。
Windows uses executable files, while Unix used elf file format. They are basically different. Unlike the counterpart Java where you can run the same executable( bytecode) on both machines, which in turn uses an interpreter(the JVM) to execute.
Windows 使用可执行文件,而 Unix 使用 elf 文件格式。它们基本上是不同的。与可以在两台机器上运行相同的可执行文件(字节码)的对应 Java 不同,后者又使用解释器(JVM)来执行。
Now, said that you have two options...
现在,说你有两个选择......
- Either you can actually install C compilers on both windows and linux(unix) and then compile your code on both of them.
- Or you can install 'Wine', a porting software on linux which will be able to actually run windows executable file on linux by windows emulation.
- 您可以在 windows 和 linux(unix) 上实际安装 C 编译器,然后在它们上编译代码。
- 或者您可以安装'Wine',这是一个Linux 上的移植软件,它可以通过Windows 仿真在Linux 上实际运行Windows 可执行文件。
Probably, option no 2 would be better suited...Its your call...
可能,选项 2 会更适合......这是你的电话......
Regards,
问候,
回答by fasih.rana
Simply put, it is not possible to run the same executable on different platforms. It is true for Windows, Linux, or any other UNIX platform.
简而言之,不可能在不同平台上运行相同的可执行文件。对于 Windows、Linux 或任何其他 UNIX 平台都是如此。
The idea of having a single executable is nothing new and is the idea behind Java Runtime and Windows .Net (Mono for Linux). These themselves rely on different executables compiled on the specific machines.
拥有单个可执行文件的想法并不是什么新鲜事,它是 Java 运行时和 Windows .Net(Linux 的 Mono)背后的想法。这些本身依赖于在特定机器上编译的不同可执行文件。
No matter users use that application in Windows or Linux, the Java GUI will call the same executable file in the bin folder.
无论用户在 Windows 还是 Linux 中使用该应用程序,Java GUI 都会调用 bin 文件夹中相同的可执行文件。
They might simply be using relavant paths to these executables, and these executables are probably compiled separately on different platforms. Hard to tell without looking at the code.
他们可能只是使用这些可执行文件的相关路径,而这些可执行文件可能是在不同平台上单独编译的。不看代码很难说。
Cygwin is a command line interface on windows that provides UNIX look and feel.
Cygwin 是 Windows 上的命令行界面,提供 UNIX 外观。
One option to try would be Wine which is a program for Linux to run windows programs on Linux.
一个可以尝试的选择是 Wine,它是一个用于 Linux 的程序,用于在 Linux 上运行 Windows 程序。
回答by Thomas Matthews
It may be possible, if the OSes are on the same platform.
如果操作系统在同一平台上,这可能是可能的。
However, Linux can run on a variety of platforms with different processors: VAX, MAC, PC, Sun, etc.
但是,Linux 可以在具有不同处理器的各种平台上运行:VAX、MAC、PC、Sun 等。
One problem is that an executable contains assembly (machine code) instructions specific to a processor. The machine code for adding a register using ARM processor may be different than a Motorola, Intel or Sun processor. So with this context, one executable may not be guaranteed to run on different platforms.
一个问题是可执行文件包含特定于处理器的汇编(机器代码)指令。使用 ARM 处理器添加寄存器的机器代码可能与 Motorola、Intel 或 Sun 处理器不同。因此,在这种情况下,一个可执行文件可能无法保证在不同平台上运行。
Another issue is that the Run-Time library may also make calls to specific OS functions (like file open, displaying text, etc.) Some operating systems may not supply the same functionality; while others have a different method to execute the same functionality. An OS on an embedded platform may not support a file system. Windows Vista may block calls from unsecure MSDOS requests.
另一个问题是运行时库还可能调用特定的操作系统功能(如文件打开、显示文本等)。某些操作系统可能不提供相同的功能;而其他人则有不同的方法来执行相同的功能。嵌入式平台上的操作系统可能不支持文件系统。Windows Vista 可能会阻止来自不安全 MSDOS 请求的调用。
Other languages become platform independent by generating code which is executed by a "virtual machine". The code is not directly executed by a processor.
其他语言通过生成由“虚拟机”执行的代码而变得独立于平台。代码不是由处理器直接执行的。
The traditional method for porting C and C++ methods is to provide the source and let the customers build the software on their platform. Other delivery processes is to create an executable for every supported platform; this becomes a maintenance nightmare.
移植 C 和 C++ 方法的传统方法是提供源代码,让客户在他们的平台上构建软件。其他交付过程是为每个支持的平台创建一个可执行文件;这变成了维护噩梦。
回答by Pascal Neubert
Can't you make a C++ program load it's respective existing libraries using #ifdef? I think that's the C++ solution to cross platform binaries. However the code needs to be adjusted to work with both or all chosen libraries.
你不能让 C++ 程序使用 #ifdef 加载它各自的现有库吗?我认为这是跨平台二进制文件的 C++ 解决方案。但是,需要调整代码以与两个或所有选定的库一起使用。
However without a java front end or a 2 different binaries or a bat and a sh or a program like wine you won't be able to run the respective binary.
但是,如果没有 java 前端或 2 个不同的二进制文件或 bat 和 sh 或类似 wine 的程序,您将无法运行相应的二进制文件。
回答by wallyk
If you create a well behaved MS Windows application, it can run unmodified on Linux x86 platforms using the wine
utility.
如果您创建了一个性能良好的 MS Windows 应用程序,它可以使用该wine
实用程序在 Linux x86 平台上未经修改地运行。
Wine
is a compatibility layer which provides Win32 and associated runtime library calls so that the program thinks it is running on MS Windows, but the wine layer translates those into Linuxisms with really good performance.
Wine
是一个兼容层,它提供 Win32 和相关的运行时库调用,以便程序认为它是在 MS Windows 上运行的,但是 wine 层将这些转换为具有非常好的性能的 Linuxisms。
More detail at http://www.winehq.org/
更多详情请访问http://www.winehq.org/