1

I'm trying to move a constant from C to Ada that is located in a header file.

file.h

#define TEST 0x1234

How would I export this constant to ada? I know to export functions and import them into ada but I can't seem to figure out how to do the same thing for constants.

Sterisk
  • 63
  • 9

1 Answers1

7

If you’re asking how to write an Ada representation of this constant by hand, then the answer is

TEST : constant := 16#1234#;

If you want to maintain the relationship between the C and the Ada automatically, you can get gcc (or, better, g++; see at end) to do the grunt work. Given sterisk.hh containing your example,

#define TEST 0x1234

compile it with

g++ -c -fdump-ada-spec sterisk.hh

which generates in sterisk_hh.ads

pragma Ada_2005;
pragma Style_Checks (Off);

with Interfaces.C; use Interfaces.C;

package sterisk_hh is

   TEST : constant := 16#1234#;  --  sterisk.hh:1

end sterisk_hh;

A third way would be to create a small C source which includes the header (here C is better; you don’t want the name to be mangled in the object file) in say sterisk.c:

#include "sterisk.hh"

const int _name_thats_unlikely_to_clash = TEST;

and compile to sterisk.o:

gcc -c sterisk.c

Then, in your Ada source:

Test : constant Integer
with
  Import,
  External_Name => "_name_thats_unlikely_to_clash";

Of course, this way means you have to decide on a specific type for the constant, and it has to match the C type.

Also, you need to include the C object file in your link:

gnatmake foo.adb -largs sterisk.o

Why is g++ better than gcc? Because g++ preserves more of the names in the C source, for example parameter names in functions.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62