Thread overview
Calling D function from C
Oct 12, 2010
Daniel Worthington
Oct 12, 2010
Denis Koroskin
Oct 12, 2010
bearophile
Oct 12, 2010
Daniel Worthington
Oct 12, 2010
Denis Koroskin
Oct 12, 2010
Emil Madsen
Oct 12, 2010
Denis Koroskin
Oct 18, 2010
Emil Madsen
October 12, 2010
I'm trying to set up a project where I can call D functions from C code. Based on the documentation ( http://www.digitalmars.com/d/2.0/interfaceToC.html) it looks like I should be able to do the following:

dee.d:
- - - - - - - -
import std.stdio;

extern(C)
{
    void sayHelloFromD()
    {
        writeln("Hello from D.");
    }
}
- - - - - - - -


dee.h
- - - - - - - -
void sayHelloFromD();
- - - - - - - -


cee.c
- - - - - - - -
#include <stdio.h>
#include "dee.h";

int main()
{
    printf("Hello from C.\n");
    sayHelloFromD();
    return 0;
}
- - - - - - - -


To compile:
dmd -c dee.d
gcc dee.o cee.c -arch i386 -o hello

It fails with the following:
Undefined symbols:
  "_D3std5stdio6stdoutS3std5stdio4File", referenced from:
      _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr in dee.o
     (maybe you meant: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr)
  "_D15TypeInfo_Struct6__vtblZ", referenced from:
      _D47TypeInfo_S3std6traits15__T8DemangleTkZ8Demangle6__initZ in dee.o
  "_D3std9contracts7bailOutFAyaixAaZv", referenced from:

 _D3std9contracts112__T7enforceTbVAyaa42_2f6f70742f646d64322f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1332Z7enforceFbLAxaZb
in dee.o
  "_D3std5stdio12__ModuleInfoZ", referenced from:
      _D3dee12__ModuleInfoZ in dee.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


I would appreciate any help you can give me with this.

Daniel


October 12, 2010
On Tue, 12 Oct 2010 04:32:28 +0400, Daniel Worthington <daniel.worthington@gmail.com> wrote:

> I'm trying to set up a project where I can call D functions from C code.
> Based on the documentation (
> http://www.digitalmars.com/d/2.0/interfaceToC.html) it looks like I should
> be able to do the following:
>
> dee.d:
> - - - - - - - -
> import std.stdio;
>
> extern(C)
> {
>     void sayHelloFromD()
>     {
>         writeln("Hello from D.");
>     }
> }
> - - - - - - - -
>
>
> dee.h
> - - - - - - - -
> void sayHelloFromD();
> - - - - - - - -
>
>
> cee.c
> - - - - - - - -
> #include <stdio.h>
> #include "dee.h";
>
> int main()
> {
>     printf("Hello from C.\n");
>     sayHelloFromD();
>     return 0;
> }
> - - - - - - - -
>
>
> To compile:
> dmd -c dee.d
> gcc dee.o cee.c -arch i386 -o hello
>
> It fails with the following:
> Undefined symbols:
>   "_D3std5stdio6stdoutS3std5stdio4File", referenced from:
>       _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr in dee.o
>      (maybe you meant: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr)
>   "_D15TypeInfo_Struct6__vtblZ", referenced from:
>       _D47TypeInfo_S3std6traits15__T8DemangleTkZ8Demangle6__initZ in dee.o
>   "_D3std9contracts7bailOutFAyaixAaZv", referenced from:
>
>  _D3std9contracts112__T7enforceTbVAyaa42_2f6f70742f646d64322f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1332Z7enforceFbLAxaZb
> in dee.o
>   "_D3std5stdio12__ModuleInfoZ", referenced from:
>       _D3dee12__ModuleInfoZ in dee.o
> ld: symbol(s) not found
> collect2: ld returned 1 exit status
>
>
> I would appreciate any help you can give me with this.
>
> Daniel

You need to link with phobos.lib (because this is where writeln is defined and implemented).
October 12, 2010
Denis Koroskin:

> You need to link with phobos.lib (because this is where writeln is defined and implemented).

And maybe use dmc instead of gcc if the compilation is done on Windows.

Bye,
bearophile
October 12, 2010
Linking with the phobos lib got it to compile, but I get a Bus Error when running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory.

Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X.

Daniel

On Mon, Oct 11, 2010 at 6:17 PM, bearophile <bearophileHUGS@lycos.com>wrote:

> Denis Koroskin:
>
> > You need to link with phobos.lib (because this is where writeln is
> defined
> > and implemented).
>
> And maybe use dmc instead of gcc if the compilation is done on Windows.
>
> Bye,
> bearophile
>


October 12, 2010
On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <daniel.worthington@gmail.com> wrote:

> Linking with the phobos lib got it to compile, but I get a Bus Error when
> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at
> the crash reports, it looks like this happens whenever the D code tries to
> allocate memory.
>
> Do you think this is an issue of using dmd and gcc together? I'm on Mac OS
> X.
>
> Daniel

I think that's because you skipped some initialization for D.
At the very least, a GC needs to be initialized first.

Try calling gc_init() (defined in core.memory) and thread_attachThis() (core.thread) because invoking other D code.

That should help.
October 12, 2010
wouldn't compiling, without garbage collector help too?

2010/10/12 Denis Koroskin <2korden@gmail.com>

> On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington < daniel.worthington@gmail.com> wrote:
>
>  Linking with the phobos lib got it to compile, but I get a Bus Error when
>> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking
>> at
>> the crash reports, it looks like this happens whenever the D code tries to
>> allocate memory.
>>
>> Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X.
>>
>> Daniel
>>
>
> I think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first.
>
> Try calling gc_init() (defined in core.memory) and thread_attachThis()
> (core.thread) because invoking other D code.
>
> That should help.
>



-- 
// Yours sincerely
// Emil 'Skeen' Madsen


October 12, 2010
On Tue, 12 Oct 2010 11:46:39 +0400, Emil Madsen <sovende@gmail.com> wrote:

> wouldn't compiling, without garbage collector help too?
>
> 2010/10/12 Denis Koroskin <2korden@gmail.com>
>
>> On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <
>> daniel.worthington@gmail.com> wrote:
>>
>>  Linking with the phobos lib got it to compile, but I get a Bus Error when
>>> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking
>>> at
>>> the crash reports, it looks like this happens whenever the D code tries to
>>> allocate memory.
>>>
>>> Do you think this is an issue of using dmd and gcc together? I'm on Mac OS
>>> X.
>>>
>>> Daniel
>>>
>>
>> I think that's because you skipped some initialization for D.
>> At the very least, a GC needs to be initialized first.
>>
>> Try calling gc_init() (defined in core.memory) and thread_attachThis()
>> (core.thread) because invoking other D code.
>>
>> That should help.
>>
>
>
>

It would, but then are you okay with memory leaks (i.e. writeln might allocate something)?
October 18, 2010
Hopefully one could use printf, and tbh I dont like the hole garbage collector thingy :)

2010/10/12 Denis Koroskin <2korden@gmail.com>

> On Tue, 12 Oct 2010 11:46:39 +0400, Emil Madsen <sovende@gmail.com> wrote:
>
>  wouldn't compiling, without garbage collector help too?
>>
>> 2010/10/12 Denis Koroskin <2korden@gmail.com>
>>
>>  On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <
>>> daniel.worthington@gmail.com> wrote:
>>>
>>>  Linking with the phobos lib got it to compile, but I get a Bus Error
>>> when
>>>
>>>> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and
>>>> looking
>>>> at
>>>> the crash reports, it looks like this happens whenever the D code tries
>>>> to
>>>> allocate memory.
>>>>
>>>> Do you think this is an issue of using dmd and gcc together? I'm on Mac
>>>> OS
>>>> X.
>>>>
>>>> Daniel
>>>>
>>>>
>>> I think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first.
>>>
>>> Try calling gc_init() (defined in core.memory) and thread_attachThis()
>>> (core.thread) because invoking other D code.
>>>
>>> That should help.
>>>
>>>
>>
>>
>>
> It would, but then are you okay with memory leaks (i.e. writeln might
> allocate something)?
>



-- 
// Yours sincerely
// Emil 'Skeen' Madsen