digitalmars.D - Calling D from C
- J.P.Fletcher aston.ac.uk (10/10) Jul 26 2004 The page http://www.digitalmars.com/d/interface.html contains the statem...
- Walter (7/16) Jul 26 2004 statement
- John Fletcher (9/29) Jul 27 2004 I am wanting to do this as a stepping stone to calling D from Ruby, so I...
- John Fletcher (16/50) Jul 27 2004 In face the following D code resolves this problem.
- John Fletcher (11/36) Jul 27 2004 I have put the working example at hello world on wiki4D at
- Deja Augustine (4/62) Jul 27 2004 What exactly are you trying to accomplish? If you don't want your D
- John Fletcher (9/34) Jul 27 2004 I am trying to set up a system to access D from Ruby, via a layer of C i...
- David Tiktin (13/17) Jul 27 2004 Define a function called _init():
- teqDruid (4/47) Jul 27 2004 According to the compiler page, DMD can't handle shared libraries. Are
- John Reimer (8/13) Jul 27 2004 From what I understand, DMD can't be used to "create" shared libraries
- John Reimer (3/7) Jul 27 2004 GDC should be interesting. Is it capable of producting shared libraries...
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
<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 thestatement"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. Mymainprogram is in C and a function in D declared as extern(C). When I link this I get undefined references from phobos routine deh2 whichislooking 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
In article <ce4fvf$1n7b$1 digitaldaemon.com>, Walter says...<J.P.Fletcher aston.ac.uk> wrote in message news:ce4a9f$1l9i$1 digitaldaemon.com...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/PythonLanguageThe page http://www.digitalmars.com/d/interface.html contains thestatement"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. Mymainprogram is in C and a function in D declared as extern(C). When I link this I get undefined references from phobos routine deh2 whichislooking for _deh_beg and _deh_end Any thoughts please?Those symbols are defined by a D module that declares a "main()" function.
Jul 27 2004
In article <ce4v0p$1tjl$1 digitaldaemon.com>, John Fletcher says...In article <ce4fvf$1n7b$1 digitaldaemon.com>, Walter says...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<J.P.Fletcher aston.ac.uk> wrote in message news:ce4a9f$1l9i$1 digitaldaemon.com...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/PythonLanguageThe page http://www.digitalmars.com/d/interface.html contains thestatement"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. Mymainprogram is in C and a function in D declared as extern(C). When I link this I get undefined references from phobos routine deh2 whichislooking for _deh_beg and _deh_end Any thoughts please?Those symbols are defined by a D module that declares a "main()" function.
Jul 27 2004
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...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? JohnP.S. I know that some work has been done on calling D from Python - see http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguageIn 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
John Fletcher wrote:In article <ce4vpg$1u34$1 digitaldaemon.com>, John Fletcher says...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 codeIn article <ce4v0p$1tjl$1 digitaldaemon.com>, John Fletcher says...In article <ce4fvf$1n7b$1 digitaldaemon.com>, Walter says...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? JohnP.S. I know that some work has been done on calling D from Python - see http://www.prowiki.org/wiki4d/wiki.cgi?NotesForProgrammersUsedTo/PythonLanguageIn 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
Deja Augustine wrote:John Fletcher 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. 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. JohnI 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? JohnWhat 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
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
On Tue, 27 Jul 2004 16:46:26 +0100, John Fletcher wrote:Deja Augustine wrote:According to the compiler page, DMD can't handle shared libraries. Are you using GDC? JohnJohn Fletcher 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. 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. JohnI 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? JohnWhat 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
According to the compiler page, DMD can't handle shared libraries. Are you using GDC? JohnFrom 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
According to the compiler page, DMD can't handle shared libraries. Are you using GDC? JohnGDC 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