以下显示了用于返回 char ** 作为返回类型和 out Ada 参数的 Ada、C 和 GPR 文件。
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
procedure Str is
function Get_Str return chars_ptr with
Import => True,
Convention => C,
External_Name => "get_str";
procedure Get_Str (Str : in out chars_ptr) with
Import => True,
Convention => C,
External_Name => "get_str2";
Str : chars_ptr := Get_Str;
Str2 : chars_ptr := Null_Ptr;
begin
Get_Str (Str2);
Put ("==> " & Value (Str));
Put ("==> " & Value (Str2));
Free (Str);
Free (Str2);
end Str;
#include <malloc.h>
#include <string.h>
const char *str = "Hello, Ada\n";
const char *str2 = "This is another C string!\n";
// As a return type.
const char *get_str (void) {
char *ptr = malloc (strlen (str) + 1);
strcpy (ptr, str);
return ptr;
}
// As a return / out parameter.
void get_str2 (char **ptr) {
*ptr = malloc (strlen (str2) + 1);
strcpy (*ptr, str2);
}
project Str is
for Languages use ("C", "Ada");
for Source_Dirs use (".");
for Exec_Dir use ".";
for Main use ("str.adb");
end Str;
使用gprbuild -P str编译。
所以要在 Ada 中实现你的功能:
package C renames Interfaces.C;
type A is
record
F1, F2 : C.int;
end record with
Convention => C;
function Dump_Record (Rec : aliased A; Out_Str : out chars_ptr) return C.int with
Import => True,
Convention => C,
External_Name => "Dump_Record";
显然,需要为 Rec 参数设置别名,以便编译器知道它是 C 中的引用,并且不会在其他地方重新分配它,即辅助堆栈。