Interfacing to C

From: Garry (ggkraemer_at_cox.net)
Date: 02/08/05


Date: 8 Feb 2005 10:07:58 -0800

I'm just starting in ADA. Can anyone explain why I have to define a
dummy procedure in my hsdbg package to get my code to work?

Error without dummy procedure:
hsdbg.ads:4:09: package "HSDBG" does not allow a body
hsdbg.ads:4:09: remove incorrect body in file "hsdbg.adb"
gnatmake: "hsdbg.ads" compilation error
error: "hsdbg.adb" must be recompiled ("hsdbg.ads" has been modified)
gnatlink: Failed to open binder output

Code follows:
/* Start of marktime.c */
#include <stdio.h>

void marktime(char str[255])
{
   printf("marktime: str='%s'\n",str);
}
/* End of marktime.c */

-- Start of hsdbg.ads

--
with Interfaces.C;
with Interfaces.C.Strings;
package HSDBG is
--  Dummy procedure?
      type DUMB is digits 5 range 0.0 .. 1.0E10;
      procedure DUMMY (B : DUMB);
--  End Dummy procedure
      procedure marktime(Variable : Interfaces.C.Strings.chars_ptr);
      pragma Import(C, marktime, "marktime");
end HSDBG;
-- End of hsdbg.ads
-- Start of hsdbg.adb
--
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
--
--  Define package body
--
package body HSDBG is
--
--
--  dummy procedure?
  procedure DUMMY(B : DUMB) is
  begin
     put("In Dummy");
  end DUMMY;
--  end dummy procedure
   procedure marktime(Variable : String) is
      procedure marktime(Variable : chars_ptr);
      pragma Import(C, marktime, "marktime");
      Variable_In_C_Format : chars_ptr := New_String(Variable);
   begin
      marktime(Variable_In_C_Format);
      Free(Variable_In_C_Format);
   end marktime;
 end HSDBG;
-- End of hsdbg.adb
-- Start of tst.adb
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with hsdbg; use hsdbg;
procedure tst is
  C_Format : chars_ptr := New_String("This is a string in ADA that is
written by the marktime C procedure");
begin
   Put_Line ("test started");
   marktime(C_Format);
end tst;
-- End of tst.adb