问题
How can I pass strings from a C function to function in ADA (C-ADA binding)?
Any examples would be appreciated.
回答1:
Basically, you would create a subprogram on the Ada side that maps you C function:
procedure Foo (C : Interfaces.C.Strings.chars_ptr);
pragma import (C, Foo, "foo");
so that from Ada you have access to foo()
.
The custom is to then provide a more Ada friendly version, with:
procedure Foo (C : String) is
S : Interfaces.C.Strings.chars_ptr := New_String (C);
begin
Foo (S);
Free (S);
end Foo;
If on the other hand the procedure is written in Ada and you want to call it from C, you would use:
procedure Foo (C : Interfaces.C.Strings.chars_ptr);
pragma Export (C, Foo, "foo");
and from C you can then call:
extern void foo(char* c);
foo("bar");
回答2:
C doesn't know about interfacing with Ada, so you have to do the interfacing work on the Ada side. The package Interfaces.C.Strings
contains operations for mapping C strings to Ada strings.
来源:https://stackoverflow.com/questions/35911763/passing-strings-in-c-ada-binding-application