Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 27, 2004 Calling D from C | ||||
---|---|---|---|---|
| ||||
The page http://www.digitalmars.com/d/interface.html contains the statement "C code can correspondingly call D functions, if the D functions use an attribute that is compatible with the C compiler, most likely the extern (C):" with an example. I am trying to do something like this using the Linux version of dmd. My main program is in C and a function in D declared as extern(C). When I link this I get undefined references from phobos routine deh2 which is looking for _deh_beg and _deh_end Any thoughts please? John Fletcher |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to J.P.Fletcher | <J.P.Fletcher@aston.ac.uk> wrote in message news:ce4a9f$1l9i$1@digitaldaemon.com... > The page http://www.digitalmars.com/d/interface.html contains the statement > > "C code can correspondingly call D functions, if the D functions use an attribute that is compatible with the C compiler, most likely the extern (C):" > with an example. > > I am trying to do something like this using the Linux version of dmd. My main > program is in C and a function in D declared as extern(C). > > When I link this I get undefined references from phobos routine deh2 which is > looking for _deh_beg and _deh_end > > Any thoughts please? Those symbols are defined by a D module that declares a "main()" function. |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <ce4fvf$1n7b$1@digitaldaemon.com>, Walter says... > > ><J.P.Fletcher@aston.ac.uk> wrote in message news:ce4a9f$1l9i$1@digitaldaemon.com... >> The page http://www.digitalmars.com/d/interface.html contains the >statement >> >> "C code can correspondingly call D functions, if the D functions use an attribute that is compatible with the C compiler, most likely the extern >(C):" >> with an example. >> >> I am trying to do something like this using the Linux version of dmd. My >main >> program is in C and a function in D declared as extern(C). >> >> When I link this I get undefined references from phobos routine deh2 which >is >> looking for _deh_beg and _deh_end >> >> Any thoughts please? > >Those symbols are defined by a D module that declares a "main()" function. > > I am wanting to do this as a stepping stone to calling D from Ruby, so I don't need a main function. Is it possible to resolve this in some way in an initialisation routine which can get called, so that I do not need a D main program? Thanks John P.S. I know that some work has been done on calling D from Python - see http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguage |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | In article <ce4v0p$1tjl$1@digitaldaemon.com>, John Fletcher says... > >In article <ce4fvf$1n7b$1@digitaldaemon.com>, Walter says... >> >> >><J.P.Fletcher@aston.ac.uk> wrote in message news:ce4a9f$1l9i$1@digitaldaemon.com... >>> The page http://www.digitalmars.com/d/interface.html contains the >>statement >>> >>> "C code can correspondingly call D functions, if the D functions use an attribute that is compatible with the C compiler, most likely the extern >>(C):" >>> with an example. >>> >>> I am trying to do something like this using the Linux version of dmd. My >>main >>> program is in C and a function in D declared as extern(C). >>> >>> When I link this I get undefined references from phobos routine deh2 which >>is >>> looking for _deh_beg and _deh_end >>> >>> Any thoughts please? >> >>Those symbols are defined by a D module that declares a "main()" function. >> >> > >I am wanting to do this as a stepping stone to calling D from Ruby, so I don't need a main function. Is it possible to resolve this in some way in an initialisation routine which can get called, so that I do not need a D main program? > >Thanks > >John > >P.S. I know that some work has been done on calling D from Python - see http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguage > > In face the following D code resolves this problem. extern(C) int call_main() { return main(); } int main() { /* just checking */ printf("Hello from main \n"); return 0; } and then put int x = call_main(); into the C code. John |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | In article <ce4vpg$1u34$1@digitaldaemon.com>, John Fletcher says... > >In article <ce4v0p$1tjl$1@digitaldaemon.com>, John Fletcher says... >> >>In article <ce4fvf$1n7b$1@digitaldaemon.com>, Walter says... >>> >>> >> >>P.S. I know that some work has been done on calling D from Python - see http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguage >> >> > >In face the following D code resolves this problem. > >extern(C) int call_main() >{ >return main(); >} > >int main() >{ >/* just checking */ >printf("Hello from main \n"); >return 0; >} > >and then put > >int x = call_main(); > >into the C code. > >John > > I have put the working example at hello world on wiki4D at http://www.prowiki.org/wiki4d/wiki.cgi?DcalledFromC There is one remaining query. I find it compiles, links and runs with only the definition of the D main, and no actual call. Does the D main do some hidden housekeeping which would be needed for another example? Does more housekeeping get done when D main exits? Is it O.K. to go on calling D routines after main has exited? Are there any restrictions? John |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | John Fletcher wrote:
> In article <ce4vpg$1u34$1@digitaldaemon.com>, John Fletcher says...
>
>>In article <ce4v0p$1tjl$1@digitaldaemon.com>, John Fletcher says...
>>
>>>In article <ce4fvf$1n7b$1@digitaldaemon.com>, Walter says...
>>>
>>>>
>
>>>P.S. I know that some work has been done on calling D from Python - see
>>>http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguage
>>>
>>>
>>
>>In face the following D code resolves this problem.
>>
>>extern(C) int call_main()
>>{
>>return main();
>>}
>>
>>int main()
>>{
>>/* just checking */
>>printf("Hello from main \n"); return 0;
>>}
>>
>>and then put
>>
>>int x = call_main();
>>
>>into the C code.
>>
>>John
>>
>>
>
>
> I have put the working example at hello world on wiki4D at
>
> http://www.prowiki.org/wiki4d/wiki.cgi?DcalledFromC
>
> There is one remaining query. I find it compiles, links and runs with only the
> definition of the D main, and no actual call.
>
> Does the D main do some hidden housekeeping which would be needed for another
> example?
>
> Does more housekeeping get done when D main exits?
>
> Is it O.K. to go on calling D routines after main has exited? Are there any
> restrictions?
>
> John
>
>
>
>
What exactly are you trying to accomplish? If you don't want your D code to be the entry-point then make a D DLL and use DllMain instead of Main to initialize your code
|
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deja Augustine |
Deja Augustine wrote:
> John Fletcher wrote:
> >
> > I have put the working example at hello world on wiki4D at
> >
> > http://www.prowiki.org/wiki4d/wiki.cgi?DcalledFromC
> >
> > There is one remaining query. I find it compiles, links and runs with only the definition of the D main, and no actual call.
> >
> > Does the D main do some hidden housekeeping which would be needed for another example?
> >
> > Does more housekeeping get done when D main exits?
> >
> > Is it O.K. to go on calling D routines after main has exited? Are there any restrictions?
> >
> > John
> >
> >
> >
> >
>
> What exactly are you trying to accomplish? If you don't want your D code to be the entry-point then make a D DLL and use DllMain instead of Main to initialize your code
I am trying to set up a system to access D from Ruby, via a layer of C in between. I also working mainly under Linux, so the equivalent of a DLL would be *.so, which I know how to do, but I don't know the equivalent of the DllMain.
I posted here rather than on D.gnu because the issues (up to now) are generic to D viz. will it come unstuck if it goes on being called after main() has exited. If it does I need two routines, one for initialising and one for exit, plus the opportunity to call the latter to clean up on error exit from the scripting language, Ruby.
John
|
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | On 27 Jul 2004, John Fletcher <J.P.Fletcher@aston.ac.uk> wrote: > I am trying to set up a system to access D from Ruby, via a layer of C in between. I also working mainly under Linux, so the equivalent of a DLL would be *.so, which I know how to do, but I don't know the equivalent of the DllMain. Define a function called _init(): void _init(void) { /* your code */ } It will be called when the .so is loaded, just like DllMain. There's a _fini() function for unload time. "man dlopen" for details. It works even if you are not doing dynamic loading with dlopen()! Dave -- D.a.v.i.d T.i.k.t.i.n t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m |
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Fletcher | On Tue, 27 Jul 2004 16:46:26 +0100, John Fletcher wrote:
>
>
> Deja Augustine wrote:
>
>> John Fletcher wrote:
>> >
>> > I have put the working example at hello world on wiki4D at
>> >
>> > http://www.prowiki.org/wiki4d/wiki.cgi?DcalledFromC
>> >
>> > There is one remaining query. I find it compiles, links and runs with only the definition of the D main, and no actual call.
>> >
>> > Does the D main do some hidden housekeeping which would be needed for another example?
>> >
>> > Does more housekeeping get done when D main exits?
>> >
>> > Is it O.K. to go on calling D routines after main has exited? Are there any restrictions?
>> >
>> > John
>> >
>> >
>> >
>> >
>> >
>> What exactly are you trying to accomplish? If you don't want your D code to be the entry-point then make a D DLL and use DllMain instead of Main to initialize your code
>
> I am trying to set up a system to access D from Ruby, via a layer of C in between. I also working mainly under Linux, so the equivalent of a DLL would be *.so, which I know how to do, but I don't know the equivalent of the DllMain.
>
> I posted here rather than on D.gnu because the issues (up to now) are generic to D viz. will it come unstuck if it goes on being called after main() has exited. If it does I need two routines, one for initialising and one for exit, plus the opportunity to call the latter to clean up on error exit from the scripting language, Ruby.
>
> John
According to the compiler page, DMD can't handle shared libraries. Are you using GDC?
John
|
July 27, 2004 Re: Calling D from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to teqDruid | >
> According to the compiler page, DMD can't handle shared libraries. Are you using GDC?
>
> John
From what I understand, DMD can't be used to "create" shared libraries from D source. In actually fact, it sort of can with direct manipulation of gcc compiler options (link stage). But the created shared library blows apart when you try to link with it. Apparently, there's some details on Linux shared libraries to be worked out that Walter has not had time to look into. I hope that eventually he finds time to fix this.
However, D has no trouble linking with shared libraries or calling the dynamic loader functions for manual setup.
|
Copyright © 1999-2021 by the D Language Foundation