将多种语言编译在一起,使它们可以在不同的平台上运行

发布于 2024-08-14 22:57:37 字数 174 浏览 4 评论 0原文

首先,我要感谢所有回答我之前关于一起编译多种语言。现在我想知道是否可以将多种语言编译在一起,以便它们可以在不同的平台上运行。

I would like to start with a THANK YOU to all those that answered my previous question on Compiling multiple languages together. Now I want to know whether or not it's possible to Compile multiple languages together so that they would run on different platforms.

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

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

发布评论

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

评论(4

2024-08-21 22:57:37

如果您编译为与机器无关的字节码(或类似的东西),然后在每个目标机器上解释字节码,则相对容易,因为这与编译多种语言在单台机器上运行是相同的问题。很多人已经解决了这个问题,解决方案的好坏程度各不相同。

如果要编译为绝对机器代码,则必须包含它将运行的每台机器的机器代码,然后每个目标机器的加载器必须知道如何选择适当的机器代码。

在 PowerPC Mac 和 68K Mac 必须和平共处的时代,Apple 就用“胖二进制文件”进行了尝试。它确实有效,但并没有取得巨大的成功。

If you compile to machine-independent bytecode (or something similar), and then interpret the bytecodes on each target machine, it is relatively easy, as it is the same problem as compiling multiple languages to run on a single machine. Lots of people have solved this problem, to varying degrees of goodness of solution.

If you want to compile to absolute machine code, you have to include the machine code for every machine it will run on, and then the loader for each target machine has to know how to select the appropriate machine code.

Apple tried this with "fat binaries", in the days when PowerPC Macs and 68K Macs had to coexist peacefully. It worked, but it was not exactly a howling success.

酷遇一生 2024-08-21 22:57:37

我认为,无论你是编译一种语言还是多种语言,你编译的代码能否在不同平台上运行的答案取决于是否存在虚拟机,例如 JVM(Java 虚拟机)或 CLR(公共语言运行时),它们可以解释指令并在不同平台上执行它们。由于不同的机器体系结构支持不同的指令集(这里的指令集指的是汇编),因此不可能将代码编译为机器语言以使其在不同的平台上运行。正如对上一个问题的答复中所指出的,多种语言将编译为 Java 字节码(例如),它可以链接在一起并在任何具有 JVM 的机器上运行。由于 JVM 有针对许多不同机器体系结构的实现,因此 Java 字节码可以在任何这些体系结构上运行。

I think that whether you are compiling one or multiple languages, the answer to whether your compiled code will run on different platforms depends on the existence of a virtual machine, like the JVM (Java Virtual Machine) or CLR (Common Language Runtime) that can interpret instructions and execute them on different platforms. Since different machine architectures support different instruction sets (here an instruction set refers to assembly), it isn't possible to compile your code to machine language in such a way that it will run on different platforms. As indicated in a response to your last question, multiple languages will compile to Java bytecode (for example), which can be linked together and run on any machine that has a JVM. Because there are implementations of the JVM for many different machine architectures, your Java bytecode can be run on any of those architectures.

还给你自由 2024-08-21 22:57:37

是的,可以将多种语言一起编译,以便它们在多个平台上运行。有几种方法:

虚拟机

ScalaJava 都编译到 JVM,它可以在许多平台上运行。另一个例子是 .NET,它包含多种语言,可以通过 Mono 项目在 Windows 或 Linux 上运行。

胖二进制文件

某些文件格式是在多个平台上运行的胖二进制文件。 Apple 曾两次使用它们在硬件实现之间进行传输。

Yes, it is possible to compile multiple languages together so that they work on multiple platforms. There's a few ways:

Virtual machine

Scala and Java both compile to the JVM, which runs on many platforms. Another example is .NET, which includes many languages and can be run on Windows or Linux through the Mono Project.

Fat binaries

Some file formats are fat binaries that run on several platforms. Apple has used them to transfer between hardware implementations, on two occasions.

蓝眼泪 2024-08-21 22:57:37

我最近听说了另一种方法。

您不是将应用程序作为机器二进制(或字节码)分发,而是以编译器的中间语言的“大部分编译”形式分发。只要所有语言的所有编译器都使用通用的中间形式,并且所有后端代码生成器都使用该通用的中间形式,您就可以在目标上安装所需的特定后端,然后分发中间体。

MacOberon 使用这种方法同时针对 680x0 和 PowerPC MAC 硬件。他们称之为“细长的二进制文件”。完全相同的“二进制文件”被分发到两种类型的机器,后端基本上通过生成适当的代码来完成编译。

有趣的是,这种方法并不比传统方法慢很多,因为将“二进制”从磁盘物理读取到内存中的成本绝对主导了应用程序的启动时间。

There's another approach that I heard about just recently.

Instead of distributing your application as machine binary (or bytecodes), you distribute it in "mostly-compiled" form, in the compiler's intermediate language. As long as all of your compilers for all of your languages use a common intermediate form, and all of your back-end code generators use that common intermediate form, you can install the particular back end you need on the target, and then distribute the intermediates.

MacOberon used this approach to target the 680x0 and PowerPC MAC hardware simultaneously. They called in "slim binaries". The exact same "binary" was distributed to either flavor of machine, and the back end essentially finished the compilation by generating the appropriate code.

Interestingly enough, this method is not significantly slower than the traditional method, because the cost of physically reading the "binary" from the disk into memory absolutely dominates the startup time of the application.

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