用一种语言编写的代码如何从另一种语言调用

用一种语言编写的代码如何从另一种语言调用

问题描述:

这是一个我一直想知道答案,但从未真正问过的问题。

This is a question that I've always wanted to know the answer, but never really asked.

如何用一种语言编写代码,特别是解释语言,通过编译语言编写的代码调用。

How does code written by one language, particularly an interpreted language, get called by code written by a compiled language.

例如,我在C ++中编写一个游戏,并将一些AI行为外包方案。在Scheme中编写的代码是如何被编译的C ++代码使用的?它如何被C ++源代码使用,它如何被C ++编译的代码使用?

For example, say I'm writing a game in C++ and I outsource some of the AI behavior to be written in Scheme. How does the code written in Scheme get to a point that is usable by the compiled C++ code? How is it used by the C++ source code, and how is it used by the C++ compiled code? Is there a difference in the way it's used?


多语言在一个项目中如何交互? p>

How do multiple-languages interact in one project?


对于无处不在的问题,没有一个答案。一般来说,答案是两种语言必须同意某事 - 一组或规则或调用协议。

There is no single answer to the question that works everywhere. In general, the answer is that the two languages must agree on "something" -- a set or rules or a "calling protocol".

在高级别,任何协议需要指定三件事:

In a high level, any protocol needs to specify three things:


  • 发现:如何找到彼此。


  • 调用:如何实际向对方发出请求。

这些细节很大程度上取决于协议本身。

The details depend heavily on the protocol itself.

有时这两种语言会一起工作。有时两种语言同意支持一些外部定义的协议。这些天,OS或运行时环境(.NET和Java)也经常涉及。有时候,能力只有一种方式(A可以调用B,但B不能调用A)。

Sometimes the two languages conspire to work together. Sometimes the two languages agree to support some outside-defined protocol. These days, the OS or the "runtime environment" (.NET and Java) is often involved as well. Sometimes the ability only goes one way ("A" can call "B", but "B" cannot call "A").

注意这是同样的问题任何语言面向与操作系统通信。

Notice that this is the same problem that any language faces when communicating with the OS. The Linux kernel is not written in Scheme, you know!

让我们从Windows世界中看到一些典型的答案:

Let's see some typical answers from the world of Windows:


  • C与C ++ :C ++使用C协议的扭曲(变形)变体。 C ++可以调用C,C可以调用C ++(虽然名称可能有点混乱,它可能需要外部帮助翻译名称)。这不只是Windows;它在支持两者的所有平台中通常是真的。最常用的操作系统也使用C协议。

  • C with C++: C++ uses a contorted ("mangled") variation of the "C protocol". C++ can call into C, and C can call into C++ (although the names can be quite messy sometimes and it might need external help translating the names). This is not just Windows; it's generally true in all platforms that support both. Most popular OS's use a "C protocol" as well.

VB6与大多数语言:VB6的首选方法是COM协议。其他语言必须能够写入COM对象,以便从VB6可用。 VB6也可以生成COM对象(虽然不是每个可能的COM对象的变体)。

VB6 vs. most languages: VB6's preferred method is the "COM protocol". Other languages must be able to write COM objects to be usable from VB6. VB6 can produce COM objects too (although not every possible variation of COM objects).

VB6也可以谈一个非常有限的C协议的变体,然后只有

VB6 can also talk a very limited variation of the "C protocol", and then only to make calls outside: it cannot create objects that can be talked to directly via the "C protocol".

.NET语言 :所有.NET语言都编译为相同的低级语言(IL)。

.NET languages: All .NET languages communicate compile to the same low-level language (IL). The runtime manages the communication and from that point of view, they all look like the same language.

VBScript与其他语言 :VBScript只能说COM协议的一个子集。

VBScript vs. other languages: VBScript can only talk a subset of the COM protocol.

调用协议,像许多其他基于Web的协议正变得流行。毕竟,它是所有关于用不同的语言编写的代码(和运行在不同的盒子里!)

One more note: SOAP "Web Services" is really a "calling protocol" as well, like many other web-based protocol that are becoming popular. After all, it's all about talking to code written in a different language (and running in a different box at that!)