comp.lang.ada
 help / color / mirror / Atom feed
* pragma import (C, , ); -- import a constant value from C library
@ 2012-10-26 22:45 Enzo Guerra
  2012-10-26 23:06 ` Simon Wright
  2012-10-27  7:40 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 8+ messages in thread
From: Enzo Guerra @ 2012-10-26 22:45 UTC (permalink / raw)


hello
trying to import a constant value from C library (libgsl)
but having no luck

 M_EULER : Long_Float;
 -- M_EULER, Euler's constant, \gamma
pragma Import (C, M_EULER, "M_EULER");

get error undefined reference to `M_EULER'

would appreciate any help
thanks
enzo



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-26 22:45 pragma import (C, , ); -- import a constant value from C library Enzo Guerra
@ 2012-10-26 23:06 ` Simon Wright
  2012-10-27  7:15   ` Maciej Sobczak
  2012-10-27  7:40 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 8+ messages in thread
From: Simon Wright @ 2012-10-26 23:06 UTC (permalink / raw)


Enzo Guerra <enzoguerra1@gmail.com> writes:

> trying to import a constant value from C library (libgsl)
> but having no luck
>
>  M_EULER : Long_Float;
>  -- M_EULER, Euler's constant, \gamma
> pragma Import (C, M_EULER, "M_EULER");
>
> get error undefined reference to `M_EULER'
>
> would appreciate any help

I wonder if the C M_EULER is in fact a macro? If so, it doesn't exist in
the library as a symbol in its own right.

You might try defining it as a variable in C and importing that:

   /* m_euler_definition.c */
   #include <libgsl.h>           /* wherever M_EULER's defined */ 
   const double m_euler_for_gnat = M_EULER;

   -- m_euler.ads
   M_EULER : constant Long_Float;  -- or Interfaces.C.double
   pragma Import (C, "M_EULER", "m_euler_for_gnat");

and then when you build you'll need to include m_euler_definition.o.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-26 23:06 ` Simon Wright
@ 2012-10-27  7:15   ` Maciej Sobczak
  2012-11-05  3:41     ` David Thompson
  0 siblings, 1 reply; 8+ messages in thread
From: Maciej Sobczak @ 2012-10-27  7:15 UTC (permalink / raw)


W dniu sobota, 27 października 2012 01:06:14 UTC+2 użytkownik Simon Wright napisał:

>    const double m_euler_for_gnat = M_EULER;

You might want to declare this variable with external linkage (the logic is: if you want to import something from Ada, then you have to export it from C), so:

    extern "C" const double m_euler_for_gnat = M_EULER;

-- 
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-26 22:45 pragma import (C, , ); -- import a constant value from C library Enzo Guerra
  2012-10-26 23:06 ` Simon Wright
@ 2012-10-27  7:40 ` Dmitry A. Kazakov
  2012-10-27  8:15   ` Simon Wright
  2012-10-27  8:19   ` Simon Wright
  1 sibling, 2 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2012-10-27  7:40 UTC (permalink / raw)


On Fri, 26 Oct 2012 15:45:29 -0700 (PDT), Enzo Guerra wrote:

> trying to import a constant value from C library (libgsl)
> but having no luck
> 
>  M_EULER : Long_Float;
>  -- M_EULER, Euler's constant, \gamma
> pragma Import (C, M_EULER, "M_EULER");
> 
> get error undefined reference to `M_EULER'
> 
> would appreciate any help

Don't do this. Ada.Numerics already defines Euler's constant.

And in general, you can define any constant this way in Ada. The precision
is limited only by the source where you get its tabulated value. And it
will work for all real types. E.g.

gamma : constant :=
   0.57721_56649_01532_86060_65120_90082_40243;

   X : Long_Float := gamma;
   Y : Float := gamma;
   ...

The compiler truncates the value when you use a narrower target like
Long_Float.

You can even perform simple calculations with such constants keeping their
huge precision. The compiler uses arbitrary-precision arithmetic for them.
E.g.

[ with Ada.Numerics; use Ada.Numerics; ]

half_pi : constant := pi / 2.0;
    -- far more than enough right digits for a Long_Float

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-27  7:40 ` Dmitry A. Kazakov
@ 2012-10-27  8:15   ` Simon Wright
  2012-10-27  8:19   ` Simon Wright
  1 sibling, 0 replies; 8+ messages in thread
From: Simon Wright @ 2012-10-27  8:15 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On Fri, 26 Oct 2012 15:45:29 -0700 (PDT), Enzo Guerra wrote:
>
>> trying to import a constant value from C library (libgsl)
>> but having no luck
>> 
>>  M_EULER : Long_Float;
>>  -- M_EULER, Euler's constant, \gamma
>> pragma Import (C, M_EULER, "M_EULER");
>> 
>> get error undefined reference to `M_EULER'
>> 
>> would appreciate any help
>
> Don't do this. Ada.Numerics already defines Euler's constant.
>
> And in general, you can define any constant this way in Ada. The precision
> is limited only by the source where you get its tabulated value. And it
> will work for all real types. E.g.
>
> gamma : constant :=
>    0.57721_56649_01532_86060_65120_90082_40243;
>
>    X : Long_Float := gamma;
>    Y : Float := gamma;
>    ...
>
> The compiler truncates the value when you use a narrower target like
> Long_Float.

Yes.

I worked on a project where the system engineers on the other side had
defined pi to be 3.14159 (not even 3.141592); so when we sent a Float
equal to Ada.Numerics.Pi they got a range check. We ended up agreeing
the bounds to be one mantissa bit outside the actual limit we intended.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-27  7:40 ` Dmitry A. Kazakov
  2012-10-27  8:15   ` Simon Wright
@ 2012-10-27  8:19   ` Simon Wright
  2012-10-27  8:47     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 8+ messages in thread
From: Simon Wright @ 2012-10-27  8:19 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> Ada.Numerics already defines Euler's constant.

Not in ARM12! (or GNAT GPL 2012).



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-27  8:19   ` Simon Wright
@ 2012-10-27  8:47     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2012-10-27  8:47 UTC (permalink / raw)


On Sat, 27 Oct 2012 09:19:30 +0100, Simon Wright wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> 
>> Ada.Numerics already defines Euler's constant.
> 
> Not in ARM12! (or GNAT GPL 2012).

You are right.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: pragma import (C, , ); -- import a constant value from C library
  2012-10-27  7:15   ` Maciej Sobczak
@ 2012-11-05  3:41     ` David Thompson
  0 siblings, 0 replies; 8+ messages in thread
From: David Thompson @ 2012-11-05  3:41 UTC (permalink / raw)


On Sat, 27 Oct 2012 00:15:06 -0700 (PDT), Maciej Sobczak
<see.my.homepage@gmail.com> wrote:

> W dniu sobota, 27 pa?dziernika 2012 01:06:14 UTC+2 u?ytkownik Simon Wright napisa?:
> 
> >    const double m_euler_for_gnat = M_EULER;
> 
> You might want to declare this variable with external linkage (the logic is: if you want to 
import something from Ada, then you have to export it from C), so:
> 
>     extern "C" const double m_euler_for_gnat = M_EULER;

No, that's a syntax error in C. (C has static, and extern, but not
extern "language"; that's a creation of C++.)

In C++ officially you need extern "C" to specify language linkage
(although in many implementations this is actually needed only on
routines not variables).

A global definition in C (at file scope, with "extern" or with no
storage-class specifier) has external linkage, which means the name is
visible to separately compiled modules (including Ada).




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-11-05  3:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-26 22:45 pragma import (C, , ); -- import a constant value from C library Enzo Guerra
2012-10-26 23:06 ` Simon Wright
2012-10-27  7:15   ` Maciej Sobczak
2012-11-05  3:41     ` David Thompson
2012-10-27  7:40 ` Dmitry A. Kazakov
2012-10-27  8:15   ` Simon Wright
2012-10-27  8:19   ` Simon Wright
2012-10-27  8:47     ` Dmitry A. Kazakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox