From: matthewbrentmccarty@gmail.com
Subject: differences between Ada and C in gnat forcing me to use C instead of Ada
Date: Sun, 24 Mar 2019 22:46:05 -0700 (PDT)
Date: 2019-03-24T22:46:05-07:00 [thread overview]
Message-ID: <c1aea932-9ae1-4915-a260-b6bc0b24c12c@googlegroups.com> (raw)
Hi all:
I really want to use Ada. But gnat is forcing me to use C. Basically, I wanted to interface with my scanner using SANE (Scanner Access Now Easy). Using my "real" Debian machine since gnat doesn't work under Windows 10 (Debian subsystem for Linux), I created the ada specs from the C headers and the sizes in C and Ada are different. For example:
In C, this code:
printf("sizeof(char *)=%d\n", sizeof(char *));
prints "sizeof(char *)=8"
and this code:
printf("sizeof(SANE_Device)=%d\n", sizeof(SANE_Device));
prints "sizeof(SANE_Device)=32". In C, the header defines it this way:
typedef char SANE_Char;
typedef const SANE_Char *SANE_String_Const;
typedef struct
{
SANE_String_Const name; /* unique device name */
SANE_String_Const vendor; /* device vendor string */
SANE_String_Const model; /* device model name */
SANE_String_Const type; /* device type (e.g., "flatbed scanner") */
}
SANE_Device;
which makes sense to me since 8*4 = 32 (sizeof(char *) * 4 elements in the record. But on the Ada side, I get the following code:
Text_Io.Put_Line ("sane_sane_h.SANE_Device'Size=>" &
Integer'Image (sane_sane_h.SANE_Device'Size));
to print "sane_sane_h.SANE_Device'Size=> 256"
And sure enough "Integer'Image (InterfaceS.C.Strings.chars_ptr'Size))" prints out 64. Now, how can we interface to a C library passing around the 'Address of an array of pointers to records if the elements in the record are different sizes. No wonder, I get back garbage when calling sane_get_devices. How is it that "sizeof(char *) is 8 in C and InterfaceS.C.Strings.chars_ptr'Size is 64 in Ada. When I try to rep spec it, I get
--size for "SANE_String_Const" too small, minimum allowed is 64
--
--for SANE_String_Const'Size use 8;
and
--for SANE_Device'Size use 32;
-- causes compiler error "size for "SANE_Device" too small, minimum allowed is 256
-- for SANE_Device use record
-- name at 0 range 0 .. 7;
-- vendor at 1 range 0 .. 7;
-- model at 2 range 0 .. 7;
-- c_type at 3 range 0 .. 7;
-- end record;
And sure enough Standard'Address_Size is 64. Sorry to be so long winded. I guess I'll write C code. Arggggh!
Regards,
Matthew McCarty
PS: Here are the steps.
So, I installed the development libraries using:
%apt-get install sane sane-utils libsane-extras xsane
which gave me
/usr/include/sane/sane.h
I then ran
%g++ -c -fdump-ada-spec -C /usr/include/sane/sane.h
which includes the following types and record:
with Interfaces.C; use Interfaces.C;
...
...
subtype SANE_Char is char; -- /usr/include/sane/sane.h:46
...
type SANE_String is access all SANE_Char; -- /usr/include/sane/sane.h:47
...
-- unique device name
type SANE_Device is record
name : SANE_String_Const; -- /usr/include/sane/sane.h:104
vendor : SANE_String_Const; -- /usr/include/sane/sane.h:105
model : SANE_String_Const; -- /usr/include/sane/sane.h:106
c_type : SANE_String_Const; -- /usr/include/sane/sane.h:107
end record;
pragma Convention (C_Pass_By_Copy, SANE_Device); -- /usr/include/sane/sane.h:109
function sane_get_devices (device_list : System.Address; local_only : SANE_Bool) return SANE_Status; -- /usr/include/sane/sane.h:221
pragma Import (C, sane_get_devices, "sane_get_devices");
next reply other threads:[~2019-03-25 5:46 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-25 5:46 matthewbrentmccarty [this message]
2019-03-25 5:58 ` differences between Ada and C in gnat forcing me to use C instead of Ada Jere
2019-03-25 8:25 ` Dmitry A. Kazakov
2019-03-25 14:06 ` Florian Weimer
2019-03-25 10:56 ` Philip Munts
2019-03-25 11:54 ` Lucretia
2019-03-25 14:09 ` matthewbrentmccarty
2019-03-25 14:20 ` Dmitry A. Kazakov
2019-03-25 16:46 ` Jeffrey R. Carter
2019-03-25 18:01 ` Dmitry A. Kazakov
2019-03-25 17:38 ` Niklas Holsti
2019-03-25 16:42 ` matthewbrentmccarty
2019-03-25 18:18 ` Dmitry A. Kazakov
2019-04-04 0:51 ` matthewbrentmccarty
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox