Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call Ada functions from C++?

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++?

like image 791
paxos1977 Avatar asked Nov 01 '08 16:11

paxos1977


2 Answers

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.

like image 159
VonC Avatar answered Oct 05 '22 19:10

VonC


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:

  • https://gcc.gnu.org/onlinedocs/gnat_ugn/Building-Mixed-Ada-and-C_002b_002b-Programs.html#Building-Mixed-Ada-and-C_002b_002b-Programs
  • http://www.ghs.com/download/whitepapers/ada_c++.pdf
like image 22
Glen Avatar answered Oct 05 '22 21:10

Glen