I have a C++ structure in "MyData.h"
struct MyData
{
int a;
int b;
};
That I want to pass a pointer of it into an Ada call
#include "MyData.h"
extern "C" void ada_call(MyData* data);
void func()
{
MyData* data = new MyData();
ada_call(data);
}
How can I build an Ada function that will accept this?
答案 0 :(得分:2)
The solution has a couple steps to it.
You need to generate an Ada record for that struct. g++ has a built in tool for doing this. Run:
g++ -c -fdump-ada-spec "MyData.h" -C
And you will get a mydata_h.ads which has a definition of a record that matches your struct. It should look like this:
with Interfaces.C; use Interfaces.C;
package MyData_h is
type MyData is record
a : aliased int; -- MyData.h:3
b : aliased int; -- MyData.h:4
end record;
pragma Convention (C_Pass_By_Copy, MyData); -- MyData.h:1
end MyData_h;
Once you have that, you need to write a procedure that takes it as a parameter. This procedure must have it passed as 'access', it must then be exported, so it is accessible from C.
data_function.ads:
with MyData_h;
use MyData_h;
package DATA_FUNCTION is
procedure ADA_CALL ( DATA : access MyData_h.MyData );
pragma Export(Convention=>C,Entity=>ADA_CALL,External_Name=>"ada_call");
end DataFunction;
data_function.adb
with MyData_h;
use MyData_h;
with Interfaces.C; use Interfaces.C;
package body DATA_FUNCTION is
procedure ADA_CALL ( DATA : access MyData_h.MyData )
begin
DATA.a = 1;
DATA.b = 2;
end ADA_CALL;
end DATA_FUNCTION;