View mode: basic / threaded / horizontal-split · Log in · Help
March 12, 2012
Calling D from C
I have a problem when calling D functions from C. While I can
perform simple arithmetic operations (i.e. the calculation is
performed in D and returned to C), I experience problems when
trying to perform string/char operations or call functions from
the D standard library (e.g. writefln()). The usual error message
I get is either "Bus error" or "Segmentation fault". I haven't
been able to find the reason for this. The programs compile and
link, however, when run, they terminate with "Bus error" whenever
a D function is performed within the D code, e.g. something like
char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
would be appreciated.

I am using Mac OS X, 10.6.7
March 12, 2012
Re: Calling D from C
On 12-03-2012 15:53, Chris W. wrote:
> I have a problem when calling D functions from C. While I can
> perform simple arithmetic operations (i.e. the calculation is
> performed in D and returned to C), I experience problems when
> trying to perform string/char operations or call functions from
> the D standard library (e.g. writefln()). The usual error message
> I get is either "Bus error" or "Segmentation fault". I haven't
> been able to find the reason for this. The programs compile and
> link, however, when run, they terminate with "Bus error" whenever
> a D function is performed within the D code, e.g. something like
> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
> would be appreciated.
>
> I am using Mac OS X, 10.6.7

Are you remembering to initialize the runtime, attach your thread, 
etc... Also, you can't call D functions directly from C code. You have 
to go through an extern (C) wrapper that then calls the D function.

-- 
- Alex
March 12, 2012
Re: Calling D from C
On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> wrote:

> I have a problem when calling D functions from C. While I can
> perform simple arithmetic operations (i.e. the calculation is
> performed in D and returned to C), I experience problems when
> trying to perform string/char operations or call functions from
> the D standard library (e.g. writefln()). The usual error message
> I get is either "Bus error" or "Segmentation fault". I haven't
> been able to find the reason for this. The programs compile and
> link, however, when run, they terminate with "Bus error" whenever
> a D function is performed within the D code, e.g. something like
> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
> would be appreciated.
>
> I am using Mac OS X, 10.6.7

If C is running your application startup, you must initialize D's runtime  
from your C main routine.

-Steve
March 12, 2012
Re: Calling D from C
On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer 
wrote:
> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> 
> wrote:
>
>> I have a problem when calling D functions from C. While I can
>> perform simple arithmetic operations (i.e. the calculation is
>> performed in D and returned to C), I experience problems when
>> trying to perform string/char operations or call functions from
>> the D standard library (e.g. writefln()). The usual error 
>> message
>> I get is either "Bus error" or "Segmentation fault". I haven't
>> been able to find the reason for this. The programs compile and
>> link, however, when run, they terminate with "Bus error" 
>> whenever
>> a D function is performed within the D code, e.g. something 
>> like
>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or 
>> help
>> would be appreciated.
>>
>> I am using Mac OS X, 10.6.7
>
> If C is running your application startup, you must initialize 
> D's runtime from your C main routine.
>
> -Steve

Yes, I am using extern (C) and in my C main function I call

gc_init();
thread_attachThis();

This works fine for primitive types such as int + int 
calculations. But anything more sophisticated renders a Bus 
error. I am sure it is just some little detail I have forgotten.
March 12, 2012
Re: Calling D from C
On 12-03-2012 16:09, Chris W. wrote:
> On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:
>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> wrote:
>>
>>> I have a problem when calling D functions from C. While I can
>>> perform simple arithmetic operations (i.e. the calculation is
>>> performed in D and returned to C), I experience problems when
>>> trying to perform string/char operations or call functions from
>>> the D standard library (e.g. writefln()). The usual error message
>>> I get is either "Bus error" or "Segmentation fault". I haven't
>>> been able to find the reason for this. The programs compile and
>>> link, however, when run, they terminate with "Bus error" whenever
>>> a D function is performed within the D code, e.g. something like
>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
>>> would be appreciated.
>>>
>>> I am using Mac OS X, 10.6.7
>>
>> If C is running your application startup, you must initialize D's
>> runtime from your C main routine.
>>
>> -Steve
>
> Yes, I am using extern (C) and in my C main function I call
>
> gc_init();
> thread_attachThis();
>
> This works fine for primitive types such as int + int calculations. But
> anything more sophisticated renders a Bus error. I am sure it is just
> some little detail I have forgotten.

Don't forget to call this: 
https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33

Documented here: 
https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101

-- 
- Alex
March 12, 2012
Re: Calling D from C
On Monday, 12 March 2012 at 15:17:32 UTC, Alex Rønne Petersen 
wrote:
> On 12-03-2012 16:09, Chris W. wrote:
>> On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer 
>> wrote:
>>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> 
>>> wrote:
>>>
>>>> I have a problem when calling D functions from C. While I can
>>>> perform simple arithmetic operations (i.e. the calculation is
>>>> performed in D and returned to C), I experience problems when
>>>> trying to perform string/char operations or call functions 
>>>> from
>>>> the D standard library (e.g. writefln()). The usual error 
>>>> message
>>>> I get is either "Bus error" or "Segmentation fault". I 
>>>> haven't
>>>> been able to find the reason for this. The programs compile 
>>>> and
>>>> link, however, when run, they terminate with "Bus error" 
>>>> whenever
>>>> a D function is performed within the D code, e.g. something 
>>>> like
>>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or 
>>>> help
>>>> would be appreciated.
>>>>
>>>> I am using Mac OS X, 10.6.7
>>>
>>> If C is running your application startup, you must initialize 
>>> D's
>>> runtime from your C main routine.
>>>
>>> -Steve
>>
>> Yes, I am using extern (C) and in my C main function I call
>>
>> gc_init();
>> thread_attachThis();
>>
>> This works fine for primitive types such as int + int 
>> calculations. But
>> anything more sophisticated renders a Bus error. I am sure it 
>> is just
>> some little detail I have forgotten.
>
> Don't forget to call this: 
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33
>
> Documented here: 
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101

Thanks a million, calling rt_init(); in my C code did the trick. 
Now I can perform string operations etc. I knew it was just a 
tiny little detail.
March 12, 2012
Re: Calling D from C
On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen  
<xtzgzorex@gmail.com> wrote:

> On 12-03-2012 16:09, Chris W. wrote:
>> On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:
>>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> wrote:
>>>
>>>> I have a problem when calling D functions from C. While I can
>>>> perform simple arithmetic operations (i.e. the calculation is
>>>> performed in D and returned to C), I experience problems when
>>>> trying to perform string/char operations or call functions from
>>>> the D standard library (e.g. writefln()). The usual error message
>>>> I get is either "Bus error" or "Segmentation fault". I haven't
>>>> been able to find the reason for this. The programs compile and
>>>> link, however, when run, they terminate with "Bus error" whenever
>>>> a D function is performed within the D code, e.g. something like
>>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
>>>> would be appreciated.
>>>>
>>>> I am using Mac OS X, 10.6.7
>>>
>>> If C is running your application startup, you must initialize D's
>>> runtime from your C main routine.
>>>
>>> -Steve
>>
>> Yes, I am using extern (C) and in my C main function I call
>>
>> gc_init();
>> thread_attachThis();
>>
>> This works fine for primitive types such as int + int calculations. But
>> anything more sophisticated renders a Bus error. I am sure it is just
>> some little detail I have forgotten.
>
> Don't forget to call this:  
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33
>
> Documented here:  
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101

More appropriate:

http://dlang.org/phobos/core_runtime.html#initialize

And actually, I think this should do everything necessary.  No need to  
call gc_init and thread_attachThis().

-Steve
March 12, 2012
Re: Calling D from C
On Mon, 12 Mar 2012 11:36:45 -0400, Steven Schveighoffer  
<schveiguy@yahoo.com> wrote:

> On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen  
> <xtzgzorex@gmail.com> wrote:
>
>> On 12-03-2012 16:09, Chris W. wrote:
>>> On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:
>>>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> wrote:
>>>>
>>>>> I have a problem when calling D functions from C. While I can
>>>>> perform simple arithmetic operations (i.e. the calculation is
>>>>> performed in D and returned to C), I experience problems when
>>>>> trying to perform string/char operations or call functions from
>>>>> the D standard library (e.g. writefln()). The usual error message
>>>>> I get is either "Bus error" or "Segmentation fault". I haven't
>>>>> been able to find the reason for this. The programs compile and
>>>>> link, however, when run, they terminate with "Bus error" whenever
>>>>> a D function is performed within the D code, e.g. something like
>>>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
>>>>> would be appreciated.
>>>>>
>>>>> I am using Mac OS X, 10.6.7
>>>>
>>>> If C is running your application startup, you must initialize D's
>>>> runtime from your C main routine.
>>>>
>>>> -Steve
>>>
>>> Yes, I am using extern (C) and in my C main function I call
>>>
>>> gc_init();
>>> thread_attachThis();
>>>
>>> This works fine for primitive types such as int + int calculations. But
>>> anything more sophisticated renders a Bus error. I am sure it is just
>>> some little detail I have forgotten.
>>
>> Don't forget to call this:  
>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33
>>
>> Documented here:  
>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101
>
> More appropriate:
>
> http://dlang.org/phobos/core_runtime.html#initialize
>
> And actually, I think this should do everything necessary.  No need to  
> call gc_init and thread_attachThis().

Hm... just realized you can't do this, since it's a D function :D

But yeah, all it does is call rt_init, so you should be good.

-Steve
March 12, 2012
Re: Calling D from C
On 12-03-2012 16:36, Steven Schveighoffer wrote:
> On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen
> <xtzgzorex@gmail.com> wrote:
>
>> On 12-03-2012 16:09, Chris W. wrote:
>>> On Monday, 12 March 2012 at 15:00:31 UTC, Steven Schveighoffer wrote:
>>>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. <wendlec@cd.ie> wrote:
>>>>
>>>>> I have a problem when calling D functions from C. While I can
>>>>> perform simple arithmetic operations (i.e. the calculation is
>>>>> performed in D and returned to C), I experience problems when
>>>>> trying to perform string/char operations or call functions from
>>>>> the D standard library (e.g. writefln()). The usual error message
>>>>> I get is either "Bus error" or "Segmentation fault". I haven't
>>>>> been able to find the reason for this. The programs compile and
>>>>> link, however, when run, they terminate with "Bus error" whenever
>>>>> a D function is performed within the D code, e.g. something like
>>>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint or help
>>>>> would be appreciated.
>>>>>
>>>>> I am using Mac OS X, 10.6.7
>>>>
>>>> If C is running your application startup, you must initialize D's
>>>> runtime from your C main routine.
>>>>
>>>> -Steve
>>>
>>> Yes, I am using extern (C) and in my C main function I call
>>>
>>> gc_init();
>>> thread_attachThis();
>>>
>>> This works fine for primitive types such as int + int calculations. But
>>> anything more sophisticated renders a Bus error. I am sure it is just
>>> some little detail I have forgotten.
>>
>> Don't forget to call this:
>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33
>>
>>
>> Documented here:
>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101
>>
>
> More appropriate:
>
> http://dlang.org/phobos/core_runtime.html#initialize

But that's effectively an extern (D) function. That's why I linked to 
rt_init.

>
> And actually, I think this should do everything necessary. No need to
> call gc_init and thread_attachThis().
>
> -Steve


-- 
- Alex
March 12, 2012
Re: Calling D from C
On Monday, 12 March 2012 at 15:39:15 UTC, Alex Rønne Petersen 
wrote:
> On 12-03-2012 16:36, Steven Schveighoffer wrote:
>> On Mon, 12 Mar 2012 11:17:31 -0400, Alex Rønne Petersen
>> <xtzgzorex@gmail.com> wrote:
>>
>>> On 12-03-2012 16:09, Chris W. wrote:
>>>> On Monday, 12 March 2012 at 15:00:31 UTC, Steven 
>>>> Schveighoffer wrote:
>>>>> On Mon, 12 Mar 2012 10:53:09 -0400, Chris W. 
>>>>> <wendlec@cd.ie> wrote:
>>>>>
>>>>>> I have a problem when calling D functions from C. While I 
>>>>>> can
>>>>>> perform simple arithmetic operations (i.e. the calculation 
>>>>>> is
>>>>>> performed in D and returned to C), I experience problems 
>>>>>> when
>>>>>> trying to perform string/char operations or call functions 
>>>>>> from
>>>>>> the D standard library (e.g. writefln()). The usual error 
>>>>>> message
>>>>>> I get is either "Bus error" or "Segmentation fault". I 
>>>>>> haven't
>>>>>> been able to find the reason for this. The programs 
>>>>>> compile and
>>>>>> link, however, when run, they terminate with "Bus error" 
>>>>>> whenever
>>>>>> a D function is performed within the D code, e.g. 
>>>>>> something like
>>>>>> char[] s2 = s.dup; (s is a char* passed from C). Any hint 
>>>>>> or help
>>>>>> would be appreciated.
>>>>>>
>>>>>> I am using Mac OS X, 10.6.7
>>>>>
>>>>> If C is running your application startup, you must 
>>>>> initialize D's
>>>>> runtime from your C main routine.
>>>>>
>>>>> -Steve
>>>>
>>>> Yes, I am using extern (C) and in my C main function I call
>>>>
>>>> gc_init();
>>>> thread_attachThis();
>>>>
>>>> This works fine for primitive types such as int + int 
>>>> calculations. But
>>>> anything more sophisticated renders a Bus error. I am sure 
>>>> it is just
>>>> some little detail I have forgotten.
>>>
>>> Don't forget to call this:
>>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L33
>>>
>>>
>>> Documented here:
>>> https://github.com/D-Programming-Language/druntime/blob/master/src/core/runtime.d#L101
>>>
>>
>> More appropriate:
>>
>> http://dlang.org/phobos/core_runtime.html#initialize
>
> But that's effectively an extern (D) function. That's why I 
> linked to rt_init.
>
>>
>> And actually, I think this should do everything necessary. No 
>> need to
>> call gc_init and thread_attachThis().
>>
>> -Steve

It's fine, no need to call gc_init() or thread_attachThis() once 
the runtime is initialized. Thanks guys.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home