Passing strings in C-ADA binding application

旧城冷巷雨未停 提交于 2020-01-03 05:36:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!