Interfacing D to C R standalone math library

TJB broughtj at gmail.com
Thu May 31 06:33:07 PDT 2012


On Thursday, 31 May 2012 at 12:52:15 UTC, Mike Parker wrote:
> On 5/31/2012 9:51 PM, Mike Parker wrote:
>> On 5/31/2012 9:40 PM, TJB wrote:
>>> One more question, if I may. I noticed that I forgot to 
>>> include the
>>> set_seed function. The call to rnorm works the way I have 
>>> called it, but
>>> it must be using some default seed (its a random number 
>>> generator).
>>>
>>> I have tried including the set_seed function as follows:
>>>
>>> import std.stdio;
>>>
>>> extern (C) void set_seed(unsigned int, unsigned int);
>>> extern (C) double rnorm(double, double);
>>>
>>> void main() {
>>> set_seed(0,77991);
>>> foreach(i; 0 .. 100) {
>>> writeln(myfunc(0.0, 1.0));
>>> }
>>> }
>>>
>>> double myfunc(double a, double b) {
>>> return rnorm(a, b);
>>> }
>>>
>>> I compile this the same way as you suggest, but now the 
>>> compiler is
>>> squealing at me with the following message:
>>>
>>> $ dmd rnorm.d -L-L/usr/lob -L-lRmath -L-lm
>>> rnorm.d(6): found 'int' when expecting ')'
>>> rnorm.d(6): semicolon expected following function declaration
>>> rnorm.d(6): Declaration expected, not ','
>>>
>>> When I grep set_seed in Rmath.h I get:
>>>
>>> $ grep set_seed Rmath.h
>>> void set_seed(unsigned int, unsigned int);
>>>
>>> So I think I have called it correctly. Any thoughts or 
>>> suggestions?
>>>
>>> Thanks, you have been so helpful!
>>>
>>> TJB
>>>
>>>
>>
>> D doesn't know anything about C's 'unsigned int'. You need to 
>> convert
>> parameters to the equivalent in D, which would be uint.
>>
>> extern (C) void set_seed(unsigned int, unsigned int);
>>
>> See the following page for more info on interfacing with C:
>>
>> http://dlang.org/interfaceToC.html
>
> Sorry, I meant:
>
>  extern (C) void set_seed(uint, uint);

Ah, yes.  That works!  Thank you.

I'm loving D programming and its community.  I can see myself 
never using C++ again.

TJB




More information about the Digitalmars-d-learn mailing list