你能打电话给C++吗? 来自 Ada 的函数?
可以从 Ada 调用 C++ 函数吗?
我想知道是否有一种方法可以直接执行此操作,而无需用 C 实现并编写 C++ 包装器和 C++ 包装器。 和 Ada 包装器,例如我想使用 c++ -> Ada 而不是 C++ -> c-> 艾达.
Can you call C++ functions from Ada?
I'm wondering if there is a way to do this directly, without doing the implementation in C and writing a C++ wrapper & and Ada wrapper, e.g. I would like to go c++ -> Ada rather than c++ -> c -> Ada.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我能给你的唯一真正与编译器无关的答案是,它就像在你的系统上从 C 调用 C++ 一样可能。
与 C 非常相似,您必须找出 C++ 例程的名称损坏符号,并在 C(本例中为 Ada)端编写一个链接到该损坏名称的绑定。 您可能还需要在 C++ 方面做一些事情,例如声明 C++ 函数 extern。
如果您可以将 C++ 函数声明为 extern "C",那就很容易了。 只需在 C++ 端执行此操作,并在 Ada 端使用 Ada 的标准 C 导入功能即可。
示例:
在您的 cpp 中:
在您的 .adb 中:
The only really compiler-agnostic answer I can give you is that it is just as possible as calling C++ from C on your system.
Much like with C, you have to figure out your C++ routine's name-mangled symbol and write a binding on the C (in this case the Ada) side that links to that mangled name. You will also probably have to do some things on the C++ side, like declaring the C++ function extern.
If you can declare your C++ function extern "C", it's easy. Just do that on the C++ side, and use Ada's standard C import features on the Ada side.
Example:
in your cpp:
in your .adb:
Ada 到 C++ 的问题是 C++ 没有定义的 ABI。
每个编译器都可以定义最有效的 ABI。
因此,与其他语言 (Ada) 进行交互是一件痛苦的事情,因为您需要 Ada 编译器知道 C++ 是用哪个编译器编译的,然后才能生成正确的代码来调用任何 C++ 方法/函数。
另一方面,C ABI 是跨所有编译器定义良好的标准,因此为任何语言提供了一个很好的方便的连接接口。
The problem with Ada to C++ is that C++ does NOT have a defined ABI.
Each compiler is allowed to define the most effecient ABI it can.
Thus interfacing from other languages (Ada) is a pain as you would need your Ada compiler to know which compiler the C++ was compiled with before it could generate the correct code to call any C++ method/function.
On the other hand the C ABI is well defined a standard across all compilers and as such provides a nice convenient interface for any language to connect with.
您可能对这篇论文感兴趣,其中讨论了 Ada 到 C++ 的对象级绑定:
http://www.adacore.com/uploads/technical-papers/Class_level_interface.pdf
此外,最新版本的 GNAT 还具有强大的自动绑定生成器。
You might be interested in this paper, which discusses an object-level binding of Ada to C++:
http://www.adacore.com/uploads/technical-papers/Class_level_interfacing.pdf
Also, recent version of GNAT feature a powerful automatic binding generator.
这似乎不可能......
来自 的信息这里
“从 Ada 到 C++ 的直接接口超出了 Ada 的范围(至少 95)”
It does not seem possible...
Information from here
"Direct interface to C++ from Ada is outside the scope of the Ada (at least 95)"
自 99/00 以来我就没有接触过 Ada,所以我的记忆可能有点粗略,但是当我们在 Ada 中开发图形窗口应用程序时,我们需要用 C++ 做一些事情(非关键任务)以及我们将两者结合起来的方式就是将 C++ 内容放在 DLL 中,然后为该 DLL 创建一个 C 包装器,然后使用指定 C 包装器的编译指示接口,然后我们可以从该接口调用 DLL 中的方法。
我认为这相当于三重维护,因为如果方法签名在 c++ dll 中发生更改,则必须在编译指示接口和 C 包装器中更新方法签名。
基本上这是一种痛苦。 我认为我们必须在 C 包装器中使用 Win32 Lean and Mean 预编译器指令。
无论如何,这是我们三个参与该项目的人都记得的。
I haven't touched Ada since 99/00 so my memory may be a bit sketchy but when we were working on a graphical windows app in Ada we needed to do some stuff in C++ (non mission critical) and the way we incorporated the two was to have the c++ stuff in a dll(s) and then create a C wrapper to that DLL and then use a pragma interface that specified the c wrapper and then we could call the methods within the dll from that interface.
I think it amounted to some triple maintenance because the method signatures had to be updated in the pragma interface and the C wrapper if they changed in the c++ dll.
Basically it was a pain. I think we had to use a Win32 Lean and Mean precomplier directive in the C wrapper.
Anyway, that is all the three of us who worked on the project can remember.
Green Hills Software 也发布了有关此内容的精彩 PDF。
http://www.ghs.com/download/whitepapers/ada_c++.pdf
Green Hills Software published a great PDF about this as well.
http://www.ghs.com/download/whitepapers/ada_c++.pdf