为什么不简单地:
SetSize: constant Interfaces.C.Size_T;
pragma Import(
Convention => C,
Entity => SetSize,
External_Name => "set_size"
);
而在你的C
file:
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