Can you name the Ada functions from C ++?

I am a complete Ada newbie, although I have been using Pascal for 2-3 years during HS.

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

+3
source share
5 answers

According to this old textbook, this should be possible.

However, as this thread has shown , you should be careful with the C ++ extern "C" definitions of your Ada functions.

+5
source

Here is an example using g ++ / gnatmake 5.3.0:

NOTE. Be careful when transferring 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;
    }

Literature:

+3

. , "C" . ++ extern "C" , Ada pragma Export ( "C" ,...

. !

+1

. , ++ Ada.

0

Yes. A few 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 the floating point values. Two applications, one in C ++ and one in Ada, each used both DLLs. Thus, there is every possible C ++ combination called / called from Ada. Everything worked fine. It was on Windows, whatever version was current at that time; I don’t remember, but maybe this worked on Linux or BeOS.

Now, if I could find the source code from this ...

0
source

Source: https://habr.com/ru/post/1699004/


All Articles