在 Ada 中访问 C 字符串时出现分段错误

Segmentation fault when accessing C string in Ada

我试图从 Ada 子程序访问在 C 程序中声明的字符串,但出现分段错误。谁能帮我解决这个问题?

这是一个失败的例子,段错误似乎来自 ada_decs.adb 中对 Interfaces.C.Strings.Value 的调用,但我不确定为什么,也不知道如何让它工作。

gdb 的回溯显示:

#0  0x00000000004167ea in system.secondary_stack.ss_mark ()
#1  0x00000000004037e6 in ada_print_it (s=0x427244 "Hello") at (...)/ada_decs.adb:2
#2  0x000000000040327c in main (argc=1, argv=0x7fffffffe568) at (...)/main.c:5

(其中 (...) 表示完整的文件路径)。

ada_decs.ads:

with Interfaces.C.Strings;
with Ada.Text_IO;
package Ada_Decs is
    procedure Print_It (s : Interfaces.C.Strings.chars_ptr) with
        Export => True,
        Convention => C,
        External_Name => "ada_print_it";
end Ada_Decs;

ada_decs.adb:

package body Ada_Decs is
    procedure Print_It (s : Interfaces.C.Strings.chars_ptr) is
        str : String := Interfaces.C.Strings.Value(s);
    begin
        Ada.Text_IO.Put_Line(str);
    end Print_It;
end Ada_Decs;

main.c:

#include <stdio.h>
extern void ada_print_it (const char *s);

int main(int argc, const char *argv[]) {
  const char *hello = "Hello";
  ada_print_it(hello);
}

对于用 C 编写的主程序,您需要让 Ada RTS 自行初始化,然后才能使用它的任何功能。并在退出时自行关闭。

adainit()adafinal() 调用将执行此操作。

extern void adainit (void);
extern void adafinal (void);    

int main(int argc, const char *argv[]) {

   adainit();

   const char *hello = "Hello";
   ada_print_it(hello);

   adafinal();
}

对于用 Ada 编写的主程序。这是由活页夹 Gnatbind 自动处理的。

您可能还需要添加链接器参数,让链接器找到这些 RTS 函数:请参阅 Gnat documents (chapter 3.11, Mixed Language interfacing) for more details. There's a worked example at the bottom of this page

gcc9.3 的两个链接;其他 gcc 版本可能略有不同,因此请查看正确的文档。