- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在处理一些我必须从 C 调用的 Ada 代码,我遇到了一个我无法解决的问题,也不知道为什么会这样。
下面是一个测试项目来说明问题:
lookup.ads
with Interfaces.C; use Interfaces.C;
package lookup is
procedure Printf(str : in Interfaces.C.char_array; i : in Positive);
pragma Import(C, printf, "printf");
procedure PrintLookup;
pragma Export(C, PrintLookup, "print_lookup");
end lookup;
查找.adb
with Interfaces.C; use Interfaces.C;
package body lookup is
-- Month_Length : constant array (1..12) of Positive := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
Month_Length : constant array (1..12) of Positive := (4 | 6 | 9 | 11 => 30, 2 => 28, others => 31);
procedure PrintLookup is
begin
printf("Month_Length(5): %d"&To_C(ascii.LF)&To_C(ascii.NUL), Month_Length(5));
end PrintLookup;
end lookup;
主.adb
with lookup;
procedure main is
begin
lookup.PrintLookup;
end main;
主.c
extern void print_lookup();
int main()
{
print_lookup();
return 0;
}
我有一个简单的 makefile 来构建它:
BUILD=ada
GM=gnatmake
CC=gcc
LIB=-L/usr/lib/gcc/i686-linux-gnu/4.9/adalib
ifeq ($(BUILD),ada)
main:
$(GM) lookup.adb main.adb
else
main: lookup.o main.o
$(CC) $(LIB) lookup.o main.o -o $@ -lgnat
lookup.o:
$(GM) lookup.adb
main.o:
$(CC) -c main.c
endif
.PHONY: clean
clean:
rm -f lookup.ali lookup.o
rm -f main.ali main.o
rm -f main
makefile 将生成一个名为main 的可执行文件。如果 makefile 第一行的 BUILD 变量设置为 ada,它将使用 Ada main.adb,否则 C < em>main.c
现在,问题来了:如果在 lookup.adb 中我使用 Month_Length 数组的第一个变体(现在已被注释掉),我得到两个主电源的以下输出是正确的:
Month_Length(5): 31
但是对于另一个数组(称为查找表),C 变体返回 0:
Month_Length(5): 0
有谁知道为什么从 C 调用查找表数组时返回 0?有人遇到过这个问题吗?我错过了什么?感谢您的帮助。
最佳答案
作为Vroomfondel评论中提到,必须调用 adainit 来初始化 ADA。以下是我为使其正常工作所做的修改:
这是生成文件:
BUILD=c
GM=gnatmake
GB=gnatbind
CC=gcc
LIB=-L/usr/lib/gcc/i686-linux-gnu/4.9/adalib
ifeq ($(BUILD),ada)
main:
$(GM) lookup.adb main.adb
else
main: lookup.o main.o
$(CC) $(LIB) lookup.o b~lookup.o main.o -o $@ -lgnat
lookup.o:
$(GM) lookup.adb
$(GB) -n lookup.ali
$(GM) b~lookup.adb
main.o:
$(CC) -c main.c
endif
.PHONY: clean
clean:
rm -f lookup.ali lookup.o
rm -f b~lookup.*
rm -f main.ali main.o
rm -f main
gnatbind 生成 b~lookup.ads 和 b~lookup.adb 文件,其中将包含 adainit() 和 adafinal() 函数,然后我用 gnatmake 构建了它们(我不得不构建因为我没有使用 gnatlink ) 并且我在链接部分包含了生成的 b~lookup.o 文件。
main.c 必须修改如下(只需在 ADA 调用前后调用 init 和 final 函数):
extern void print_lookup();
extern void adainit();
extern void adafinal();
int main()
{
adainit();
print_lookup();
adafinal();
return 0;
}
其余保持不变。
关于c - 从 C 调用 Ada 时 Ada 查找表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445732/
如何在过程中编写迭代器?对不起我的转储问题,我是新手。感谢您的回答。 最佳答案 这完全取决于您需要迭代的内容。 数组?使用loop : plain, for, or while. predefined
我现在知道很多编程语言。回到我 18 岁的时候,我几乎加入了美国空军,并且对 Ada 进行了测试。那是十多年前的事了。 Ada 编程语言在军队中是否仍然像以前一样重要? 我想知道新的军事软件项目是否仍
在 Java 或 C# 中,您经常会拥有 final 的类成员。或 readonly - 它们设置一次,然后再也不碰。它们可以为类的不同实例保存不同的值。 艾达有没有类似的东西?我试图在 Ada 中创
即使使用这个简单的示例,我也无法让动态调度正常工作。我相信问题在于我如何设置类型和方法,但看不到在哪里! with Ada.Text_Io; procedure Simple is type A
我目前正在自学 Ada,尽管我可以从解决一些更传统的问题开始。 更具体地说,我尝试计算阶乘 n!,而 n>100。到目前为止,我的实现是: with Ada.Text_IO; with Ada.Int
目前正在学习 Ada 并真正享受它,有一件事情困扰着我:什么是 tagged类型?根据 John Barnes 的 Programming in Ada 2012,它表示实例化的对象在运行时带有标签。
你好 我正在尝试我在 Ada 中创建单人骰子游戏的第一个程序。 但面临着保持球员得分的问题。 目标:每个玩家有 10 个回合,如果 2 次掷骰总数为 7,则获得 10 分 问题:每次总分被重置并且 1
您可以通过让函数返回一个值来分配给变量: My_Int : Integer := My_Math_Func [(optional params)]; 或者你可以用一个过程来做到这一点(假设 My_In
我试图在 Ada 中将字符转换为整数,似乎没有任何效果,到目前为止我已经能够从 ASCII 返回 DEC,但我想返回 0(整数)。 Character'Pos('0'); 返回 48 --我希望它返回
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我正在写一篇关于 Ada 83 的论文。我们有一个作业,列出了论文的各个部分(历史、设计目标、语法等)。讲师提到我们中的一些人将有一些部分简单地说“此语言不支持此功能。” 其中两个部分是数据类型和
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我想声明一个元素类型为变体记录的数组。像这样: type myStruct (theType : vehicleType) is record ... when car => numOfWheels
我正在实例化一个带有枚举的通用包,以访问多个值之一并在子程序重载中使用。我想要一组定义明确、编译时检查过的值,我可以使用和查找。 generic -- Different types beca
我有以下包: ------------------- -- File: father.ads ------------------- package Father with SPARK_Mode =>
对于最后的程序,我从 gnat 收到以下错误消息: test2.adb:23:61: error: invalid operand types for operator "-" test2.adb:2
我编写了一个加密文件的 Ada 程序。它逐 block 读取它们以节省目标机器上的内存。不幸的是,Ada 的目录库读取 Long_Integer 中的文件大小,将读取限制为近 2GB 文件。尝试读取超
我想打印访问变量(指针)的地址以进行调试。 type Node is private; type Node_Ptr is access Node; procedure foo(n: in out No
我是一名优秀的程序员,十分优秀!