c - 从 Ada 访问 c 常量

标签 c ada

我有一个这样定义类型的头文件

#ifndef SETSIZE
   #define SETSIZE 32
#endif

typedef struct _set {
    unsigned  array[SETSIZE];
} set_t;

要使用相应的 C 函数,我需要在 Ada 中使用 set_t 类型。问题在于 SETSIZE 是一个可配置参数(默认值为 32)。如果我理解正确,我无法从 Ada 访问预处理器定义。 是否可以向 c 文件添加一个常量并像这样在 Ada 中使用它:

#ifndef SETSIZE
   #define SETSIZE 32
#endif

const size_t test = SETSIZE;
// Alternative
enum { test2 = SETSIZE };

--Ada--
-- import test somehow
type set_array is array (0 .. test) of aliased Interfaces.C.unsigned;
type set_t is record
  array_field : aliased set_array;
end record;

或者在 Ada 中正确使用这种类型而无需对原始 C 代码进行太多更改的任何其他方式

最佳答案

为什么不简单:

SetSize: constant Interfaces.C.Size_T;
pragma Import(
    Convention    => C,
    Entity        => SetSize,
    External_Name => "set_size" 
);

在你的 C 文件中:

const size_t set_size = SETSIZE;

使用 gnatmake 4.8.1 测试:

// File: set_def.c
#include <stdlib.h>

#ifndef SETSIZE
   #define SETSIZE 32
#endif

const size_t set_size = SETSIZE ;

typedef struct _set {
    unsigned  array[SETSIZE];
} set_t;

编译set_def.c:

Z:\> gcc -c set_def.c
-- File: main.adb
with Interfaces.C, Ada.Text_IO, Ada.Integer_Text_IO ;

procedure Main is

    use type Interfaces.C.Size_T ; -- To have access to the - operator

    SetSize: constant Interfaces.C.Size_T;

    pragma Import (
        Convention    => C,
        Entity        => SetSize,
        External_Name => "set_size" 
    );

    -- Note that you should go to SetSize - 1
    type Set_Array is array(0 .. SetSize - 1) of aliased Interfaces.C.Unsigned;
    type Set_T is
      record
        Array_Field: aliased Set_Array;
      end record;

    MySet: Set_T := (Array_Field => (1, 2, 3, others => 0)); 

begin
   Ada.Integer_Text_IO.Put (Integer(SetSize), 0); Ada.Text_IO.New_Line;
   Ada.Integer_Text_IO.Put (MySet.Array_Field'Length, 0); Ada.Text_IO.New_Line;
   for I in MySet.Array_Field'range loop
       Ada.Integer_Text_IO.Put (Integer(MySet.Array_Field(I)), 0);
       Ada.Text_IO.Put(' ');
   end loop;
end Main;

编译main.adb并与set_def.o链接:

Z:\> gnatmake main.adb -largs set_def.o
Z:\> main.exe
32
32
1 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

关于c - 从 Ada 访问 c 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32079775/

相关文章:

c - 从 C 中的套接字文件描述符读取时如何检测分隔符?

logging - 除了 Alog (http ://www. nongnu.org/alog/) 之外,是否还有 Ada 的日志记录框架或众所周知的设计模式?

Ada:访问 Real_Matrix 中的第一个元素(一行和一列)

ada - Ada 中的 "Missing full declaration for private type.."

c - 套接字收到的字节数少于发送的字节数

C、时间、月日、年等

c - 如何确定文件描述符是否可搜索?

c - goto语句提前结束程序的问题

scheduled-tasks - EDF 中的 Ada 调度

ada - 我的简单 Ada 程序无法执行下一个命令,但在构建和运行过程中这无关紧要