如何制作可移植的可执行文件?

发布于 2024-09-30 06:29:49 字数 239 浏览 3 评论 0原文

有没有办法编译 ac/c++ 源文件以输出可以在不同计算机上的其他处理器上运行的 .exe 文件? 我问这个是针对windows平台的。 我知道它可以用 java 或 c# 完成,但它使用虚拟机。

PS:对于那些说只需使用虚拟机即可完成或必须在每台机器上编译源代码的人,我想问的是,是否所有病毒都是用java或c#编写的,并且您需要一台虚拟机才能感染,或者您需要编译蠕虫源代码到你的机器上被感染? (我并不是想制造病毒,而是一个很好的例子:))

It's there a way to compile a c/c++ source file to output a .exe file that can be run on other processors on different computers ?
I am asking this for windows platform.
I know it can be done with java or c# , but it uses virtual machine.

PS: For those who said that it can be done just with virtual machines or the source cod must be compiled on every machine , i am asking if all viruses are written in java or c# and you need a vm machine to be infected or you need to compile source cod of worm on your machine to be infected ? (i am not trying to make a virus, but is a good example :) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

彼岸花ソ最美的依靠 2024-10-07 06:29:49

不同的计算机使用不同的指令集、操作系统系统调用等,这就是机器代码是特定于平台的原因。这就是为什么字节码、虚拟机等各种技术被开发出来以允许可移植的可执行文件。但是,通常 C/C++ 直接编译为特定于平台的机器代码。

因此,如果没有某种模拟层,Windows exe 根本无法在其他平台上运行。

但是,您可以使 C/C++ 源代码可移植。这意味着要使应用程序在另一个平台上运行,您所需要做的就是针对该平台进行编译。

Different computers use different instruction sets, OS system calls, etc., which is why machine code is platform specific. This is why various technologies like byte code, virtual machines, etc., have been developed to allow portable executables. However, generally C/C++ compiles directly to platform-specific machine code.

So a Windows exe simply won't run on another platform without some kind of emulation layer.

However, you can make your C/C++ source code portable. This means all you need to do to make your application run on another platform is to compile it for that platform.

七颜 2024-10-07 06:29:49

是的,可以,但这不一定是个好主意。

Apple 在从 Motorola 68000 迁移到 PowerPC 芯片时引入了 fat 二进制 的想法90 年代初(我并不是说他们发明了它,这只是我所知道的最早的化身)。该链接还描述了 FatELF Linux 通用二进制文件,但考虑到我们对它们知之甚少,它们似乎并没有流行起来。

这是一个单一文件,基本上包含捆绑在一个文件中的 68000 和 PowerPc 可执行文件,并且需要操作系统的一些智能,以便它可以加载并执行相关文件。

如果您愿意的话,您可以编写一个编译器,生成一个可以在许多平台上运行的胖二进制文件,但是:

  • 它会大得可怕;几乎
  • 可以肯定,每个目标系统都需要特殊的加载程序。

由于 gcc 对不同平台和交叉编译有如此巨大的支持,如果我疯狂地尝试的话,这将是我集中精力的地方:-)

Yes, you can, but it's not necessarily a good idea.

Apple introduced the idea of a fat binary when they were migrating from the Motorola 68000 to the PowerPC chips back in the early 90s (I'm not saying they invented it, that's just the earliest incarnation I know of). That link also describes the FatELF Linux universal binaries but, given how little we hear about them, they don't seem to have taken off.

This was a single file which basically contained both the 68000 and PowerPc executables bundled into one single file and required some smarts from the operating system so it could load and execute the relevant one.

You could, if you were so inclined, write a compiler which produced a fat binary that would run on a great many platforms but:

  • it would be hideously large; and
  • it would almost certainly require special loaders on each target system.

Since gcc has such a huge amount of support for different platforms and cross-compiling, it would be where I would concentrate the effort, were I mad enough to try :-)

一笑百媚生 2024-10-07 06:29:49

简短的回答是——你不能。更长的答案是用可移植(标准)C/C++ 编写并在您需要的平台上编译。

The short answer is - you can't. Longer answer is write in portable (standard) C/C++ and compile on the platforms you need.

时光磨忆 2024-10-07 06:29:49

但是,您可以使用不同的语言进行操作。如果您需要在多个平台上运行某些东西,我建议您研究 Java。与 c/c++ 类似的语言,您可以“编译”(某种程度上)程序以在几乎任何计算机上运行。

You can, however, do it in a different language. If you need something to run on multiple platforms, I suggest you investigate Java. Similar language to c/c++, and you can "compile" (sort of) programs to run on pretty much any computer.

妥活 2024-10-07 06:29:49

总结其他答案,如果您想创建一个可以在多个平台上加载和运行的单个可执行文件,您基本上有两个选择:

  1. 创建一个“胖二进制文件”,其中包含多个平台的机器代码。大多数开发工具通常不支持这一点,并且可能需要目标平台上的特殊加载程序;

  2. 编译为 JVM 或 .Net 的字节代码。我听说过一种可以生成 Java 字节代码的 C 编译器(一时记不起名字了),但从未使用过它,也不知道其实现的质量如何。

通常,C 语言支持多平台的过程是通过使用交叉编译器或在每个平台上运行编译器为每个目标生成不同的可执行文件。这需要您对如何编写和组织源代码进行一些思考,以便可以轻松地交换特定于平台的位,而不影响整体程序逻辑,以实现不同程度的“轻松”。

To summarize the other answers, if you want to create a single executable file that can be loaded and run on multiple platforms, you basically have two options:

  1. Create a "fat binary", which contains the machine code for multiple platforms. This is not normally supported by most development tools and may require special loaders on the target platform;

  2. Compile to a byte code for the JVM or for .Net. I've heard of one C compiler that generates Java byte code (can't remember the name offhand), but never used it, nor do I have any idea what the quality of the implementation would be.

Normally, the procedure for supporting multiple platforms in C is to generate different executables for each target, either by using a cross compiler or running a compiler on each platform. That requires you to put some thought into how you write and organize your source code so that the platform-specific bits can be easily swapped out without affecting the overall program logic, for varying degrees of "easily".

流云如水 2024-10-07 06:29:49

不要将处理器平台与操作系统平台混淆。
对于不同的操作系统平台,机器二进制文件完全不同。甚至不可能制作一对一的指令映射器。这是因为整个指令集架构可能不同,不同的指令组可能有完全不同的指令格式,甚至某些指令可能会丢失或目标平台不同。
只有模拟器或虚拟机才能做到这一点。

Do not confuse processor platforms with OS platforms.
for different OS platforms, machine binaries are altogether different. even not possible to make one-to-one instruction mapper. it is beacause whole instruction set architecture may be different, and different instruction groups may have totally different instruction format, and even some instructions may be missing or target platform.
Only an emulator or virtual machine can do this.

所有深爱都是秘密 2024-10-07 06:29:49

实际上,有些操作系统支持这一点;它通常被称为“fat 二进制文件”。

特别是,Mac OS 使用(使用)它在一个二进制文件中支持 PowerPC 和 x86。

然而,在 MS Windows 上,这是不可能的,因为操作系统不支持它。

Actually, some operating systems support this; it is usually called a "fat binary".

In particular, Mac OS uses (used) it to support PowerPC and x86 in one binary.

On MS Windows however, this is not possible as the OS does not support it.

情深缘浅 2024-10-07 06:29:49

Windows 可以在 64 位模式下运行 32 位可执行文件,没有任何问题。所以如果你在 32 位模式下编译你的 exe 将是可移植的。否则您必须发布 2 个版本的可执行文件。如果您使用 Java 或 C#(或任何字节码编译语言),JIT/解释器可以针对当前操作系统的模式优化您的代码,因此它是完全可移植的。但在 C++ 上,由于它生成本机代码,恐怕除了使用 2 个版本的二进制文件之外无法完成此操作。

Windows can run 32bit executables in 64bit mode with no problem. So your exe will be portable if you compile it in 32bit mode. Or else you must release 2 versions of your executable. If you use Java or C# (or any bytecode-compiled language), the JIT/interpreter can optimize your code for the current OS's mode, so it's fully portable. But on C++, since it produces native code, I'm afraid this can't be done except using 2 versions of your binary.

゛清羽墨安 2024-10-07 06:29:49

做到这一点的技巧是创建一个二进制文件,其中包含机器指令,这些机器指令将在您想要支持的操作系统和处理器上的虚拟机中进行模拟。

最广泛传播的此类虚拟机是 Java 虚拟机的变体。因此,我的建议是查看一个将 C 代码编译为 Java 字节代码的编译器。

另外,Windows 曾经处理过 x86 作为其他 (Alpha) 架构上的虚拟机

The trick to do this, is to create a binary which has machine instructions which will be emulated in a virtual machine on the operating systems and processors you want to support.

The most widely spread such virtual machine are variants of the Java virtual machine. So my suggestion would be to look at a compiler which compiles C code to Java byte code.

Also, Windows once upon a time treated x86 as a virtual machine on other (Alpha) architectures.

小苏打饼 2024-10-07 06:29:49

简短的回答是你不能,详细的回答是有多种选择。

  1. 胖二进制。缺点是这需要操作系统支持。据我所知,唯一支持它的用户级操作系统是 OS X,用于将其 Power PC 迁移到 Intel。
  2. 即时交叉翻译。 Transmeta 和 Apple 使用的。据我所知,同样没有通用解决方案提供商。
  3. C\C++ 解释器。我至少知道 Ch. 1。它可以在 Windows、Linux、OS X 上运行。注意 Ch 不完全兼容 C++。

The short answer is you can't, The long answer is there are several options.

  1. Fat Binary. Downside is that this requires OS support. The only user level OS I know of that supports it is OS X for their power pc to Intel migration.
  2. On the fly cross translation. As used by Transmeta and Apple. Again no general solution provider that I know of.
  3. a C\C++ interpreter. There is at least one I am aware of Ch. It runs on Windows, Linux, OS X. Note Ch is not fully C++ compatible.
倒带 2024-10-07 06:29:49

这个问题就像问“有没有一种方法可以从加拿大到世界上另一个城市?”

答案是:“是的。”

要在 Windows 平台上将 C/C++ 源代码编译为可执行文件(无需任何虚拟机),您可以使用 Windows Legacy API 或 MFC(特别是在静态库中使用 MFC,而不是在 Dll 中)。该可执行文件大约可以在所有具有 Windows 的 PC 上运行,因为 Windows 仅在 3 个平台上运行(x86、x64、IA64;支持 ARM 的 Windows 8 和 8.1 除外)。当然,您应该将源代码编译为 x86 代码才能运行32 位和 x86-64 平台以及 Itanium 可以在仿真中运行您的 exe。但对于所有运行 Windows 作为操作系统的处理器(例如手机中的 ARMS),您应该针对 Windows Phone 或 Windows CE 进行编译。

This question likes to that ask "Is there a way can travel from Canada to another city in the world?"

And answer is: "yes there is."

for compiling a C/C++ Source code to an Executable file on Windows Platform without any virtual machine you can use Windows Legacy API or MFC (specially with use MFC in a Static Library instead in Dll). This executable file approximately runs on all PCs that have windows, because windows runs on only 3 platforms (x86, x64, IA64; except windows 8 & 8.1 that supports ARMs).Of course you should compile your source to x86 codes to run on 32 bit and x86-64 platforms and Itaniums can run your exe in emulation. But about all Processors that runs windows as their OS (Like ARMS in mobiles) you should compile that for the Windows Phone or Windows CE.

热风软妹 2024-10-07 06:29:49

你可以写一个中间库~比如:

      [ Library ]

     [ mid-library]

[linux part] [windows part]

然后你就可以使用库了,你的app就可以移植了~~

You can write a mid-library~ such as:

      [ Library ]

     [ mid-library]

[linux part] [windows part]

then, you can use library, your app will be portable~~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文