繁体   English   中英

在 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 自动处理。

您可能还需要添加 linker arguments 以让 linker 找到这些 RTS 函数:请参阅Gnat 文档(第 3.11 章,混合语言)了解更多详细信息。 本页底部有一个工作示例。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM