www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Calling D from C

reply J.P.Fletcher aston.ac.uk writes:
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
Jul 26 2004
parent reply "Walter" <newshound digitalmars.com> writes:
<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.
Jul 26 2004
parent reply John Fletcher <John_member pathlink.com> writes:
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
Jul 27 2004
parent reply John Fletcher <John_member pathlink.com> writes:
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
Jul 27 2004
parent reply John Fletcher <John_member pathlink.com> writes:
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
Jul 27 2004
parent reply Deja Augustine <deja scratch-ware.net> writes:
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
Jul 27 2004
parent reply John Fletcher <J.P.Fletcher aston.ac.uk> writes:
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
Jul 27 2004
next sibling parent David Tiktin <dtiktin nospam.totally-bogus.com> writes:
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
Jul 27 2004
prev sibling parent reply teqDruid <me teqdruid.com> writes:
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
Jul 27 2004
next sibling parent John Reimer <brk_6502 yahoo.com> writes:
 
 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.
Jul 27 2004
prev sibling parent John Reimer <brk_6502 yahoo.com> writes:
 According to the compiler page, DMD can't handle shared libraries.  Are
 you using GDC?
 
 John
GDC should be interesting. Is it capable of producting shared libraries? An interesting possibility would be to create a shared library with GDC and link it to dmd based program. Would that work?
Jul 27 2004