可以从 C++ 调用 Ada 函数吗?

发布于 2024-07-07 12:21:17 字数 127 浏览 7 评论 0 原文

我是一个完全的 Ada 新手,尽管我在高中期间已经使用 Pascal 2-3 年了。

IIRC,可以从 C/C++ 调用 Pascal 编译函数。 是否可以调用过程& 从 C++ 用 Ada 编写的函数?

I'm a complete Ada newbie, though I've used Pascal for 2-3 years during HS.

IIRC, it is possible to call Pascal compiled functions from C/C++. Is it possible to call procedures & functions written in Ada from C++?

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

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

发布评论

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

评论(4

回眸一遍 2024-07-14 12:21:17

根据这个旧教程,应该是可能的。

但是,如 此线程,您必须小心 Ada 函数的 c++ extern "C" 定义。

According to this old tutorial, it should be possible.

However, as illustrated by this thread, you must be careful with the c++ extern "C" definitions of your Ada functions.

情徒 2024-07-14 12:21:17

以下是使用 g++/gnatmake 5.3.0 的示例:

注意:在 C++ 和 Ada 之间传递数据时要小心

ada_pkg.ads

    package Ada_Pkg is
        procedure DoSomething (Number : in Integer);
        pragma Export (C, DoSomething, "doSomething");
    end Ada_Pkg;

ada_pkg.adb

    with Ada.Text_Io;
    package body Ada_Pkg is
        procedure DoSomething (Number : in Integer) is
        begin
            Ada.Text_Io.Put_Line ("Ada: RECEIVED " & Integer'Image(Number));
        end DoSomething;
    begin
        null;
    end Ada_Pkg;

< strong>ma​​in.cpp

    /*
    TO BUILD:
    gnatmake -c ada_pkg
    g++ -c main.cpp
    gnatbind -n ada_pkg
    gnatlink ada_pkg -o main --LINK=g++ -lstdc++ main.o
    */

    #include <iostream>

    extern "C" {
        void doSomething (int data);
        void adainit ();
        void adafinal ();
    }

    int main () {
        adainit(); // Required for Ada
        doSomething(44);
        adafinal(); // Required for Ada 
        std::cout << "in C++" << std::endl;
        return 0;
    }

参考文献:

Here's an example using g++/gnatmake 5.3.0:

NOTE: Be careful when passing data between C++ and Ada

ada_pkg.ads

    package Ada_Pkg is
        procedure DoSomething (Number : in Integer);
        pragma Export (C, DoSomething, "doSomething");
    end Ada_Pkg;

ada_pkg.adb

    with Ada.Text_Io;
    package body Ada_Pkg is
        procedure DoSomething (Number : in Integer) is
        begin
            Ada.Text_Io.Put_Line ("Ada: RECEIVED " & Integer'Image(Number));
        end DoSomething;
    begin
        null;
    end Ada_Pkg;

main.cpp

    /*
    TO BUILD:
    gnatmake -c ada_pkg
    g++ -c main.cpp
    gnatbind -n ada_pkg
    gnatlink ada_pkg -o main --LINK=g++ -lstdc++ main.o
    */

    #include <iostream>

    extern "C" {
        void doSomething (int data);
        void adainit ();
        void adafinal ();
    }

    int main () {
        adainit(); // Required for Ada
        doSomething(44);
        adafinal(); // Required for Ada 
        std::cout << "in C++" << std::endl;
        return 0;
    }

References:

茶底世界 2024-07-14 12:21:17

这种事情一直在做。 诀窍是告诉双方在例程中使用“C”风格的调用协议。 在 C++ 中,这是通过 extern "C" 声明完成的,在 Ada 方面是通过 pragma Export ("C", ...

在您最喜欢的相应参考源中查找这些内容以获取详细信息。请注意指针和参考参数!

That kind of thing is done all the time. The trick is to tell both sides to use a "C"-style calling protocol for the routine. In C++ this is done with extern "C" declarations, and in the Ada side with pragma Export ("C", ...

Look those up in your favorite respective reference sources for details. Watch out for pointer and reference paramters!

三生殊途 2024-07-14 12:21:17

是的。 几年前,我写了一个简短的简单演示来证明这一点。 有两个 DLL,一个用 C++ 编写,另一个用 Ada 编写。 他们只是将常量添加到浮点值中。 两个应用程序(一个使用 C++,一个使用 Ada)都使用了这两个 DLL。 因此,C++ 调用/Ada 调用的每种可能的组合都存在。 一切都很好。 这是在 Windows 上运行的,无论当时的版本如何; 我不记得了,但可能已经在 Linux 或 BeOS 上运行了。

现在如果我能从中找到源代码就好了......

Yes. Several years ago I wrote a short simple demo to prove it. There were two DLLs, one written in C++ and the other in Ada. They just added constants to floating point values. Two apps, one in C++ and one in Ada, each used both the DLL. So every possible combination of C++ calling/called from Ada existed. It all worked fine. This was on Windows whatever version was current at the time; I don't recall but may have gotten this working on Linux or BeOS.

Now if only I could find the source code from that...

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