digitalmars.D.learn - calling D2 routines from C routines on linux
- Charles Hixson <charleshixsn earthlink.net> Jul 19 2011
- Andrej Mitrovic <andrej.mitrovich gmail.com> Jul 19 2011
- Charles Hixson <charleshixsn earthlink.net> Jul 19 2011
- Charles Hixson <charleshixsn earthlink.net> Jul 19 2011
- Jimmy Cao <jcao219 gmail.com> Jul 19 2011
- Andrej Mitrovic <andrej.mitrovich gmail.com> Jul 19 2011
- Jimmy Cao <jcao219 gmail.com> Jul 19 2011
Actually, the C routines are there purely as a link to Python, but ... To me the problem appears to be that I don't know how to initialize the D system when it's not a main program, and I don't know what needs to be linked. To me it seems like this should be written up in the documentation, but there are problems because different systems would use different commands for the linking, (And for the compilation of the non-D classes.) There's a short writeup on how to call D from C++, but it's not enough to actually DO it. It doesn't tell how to initialize the D environment, e.g., or what libraries need to be linked. (phobos, yes, but it that all? And would you need it even if you didn't use phobos?) That said, I got this working a few years ago with D1, but is D2 the same? It seems quite different in many ways.
Jul 19 2011
See if this works for you:
import core.runtime;
void myMainFunc()
{
}
extern (C)
int initializeDee()
{
int result;
void exceptionHandler(Throwable e) { throw e; }
try
{
Runtime.initialize(&exceptionHandler);
result = myMainFunc();
Runtime.terminate(&exceptionHandler);
}
catch (Throwable o)
{
result = 0;
}
return result;
}
This is similar to how it's done for Windows GUI applications, where
we have to manually run the initialize function in druntime (unless
it's hidden by some GUI framework).
Jul 19 2011
On 07/19/2011 04:48 PM, Andrej Mitrovic wrote:See if this works for you: import core.runtime; void myMainFunc() { } extern (C) int initializeDee() { int result; void exceptionHandler(Throwable e) { throw e; } try { Runtime.initialize(&exceptionHandler); result = myMainFunc(); Runtime.terminate(&exceptionHandler); } catch (Throwable o) { result = 0; } return result; } This is similar to how it's done for Windows GUI applications, where we have to manually run the initialize function in druntime (unless it's hidden by some GUI framework).
Thank you. It looks good, and I *will* be trying it. But my main point was that it needs to be in the documentation. (And notice that you still didn't say what libraries need to be linked, or that none do.) I understand that most of the documentation is not specific to operating systems. And that's good. But where you *need* information about the operating system, the information needs to be there, just like the compiler options which differ between Linux and MSWind are documented differently. And yes, what you displayed (i.e., the code) should be identical between OSes. But that's only the majority of the information needed. I think I could figure it out by checking what worked the last time I did it, even though that was with D1, but it really should be in the documentation. So, for that matter, should the code. If I knew where to post the request, I'd post it there, but as it is I'm just hoping that an appropriate person will read this and act on it.
Jul 19 2011
I notice I've skipped the main reason. What I'd like to do is build libraries in D that I can call from multiple languages. C, Python, C++, Ada, Fortran, Lisp, whatever. Just about every language has a C interface. So if I can call the library from C, then I can call it from anywhere. (Possibly except D. Not sure about that, but I think the runtime functions might clash.) On 07/19/2011 04:48 PM, Andrej Mitrovic wrote:See if this works for you: import core.runtime; void myMainFunc() { } extern (C) int initializeDee() { int result; void exceptionHandler(Throwable e) { throw e; } try { Runtime.initialize(&exceptionHandler); result = myMainFunc(); Runtime.terminate(&exceptionHandler); } catch (Throwable o) { result = 0; } return result; } This is similar to how it's done for Windows GUI applications, where we have to manually run the initialize function in druntime (unless it's hidden by some GUI framework).
Jul 19 2011
--0016e64640e2497a1304a878f3de Content-Type: text/plain; charset=ISO-8859-1 On Tue, Jul 19, 2011 at 6:48 PM, Andrej Mitrovic <andrej.mitrovich gmail.comwrote:
This is similar to how it's done for Windows GUI applications, where we have to manually run the initialize function in druntime (unless it's hidden by some GUI framework).
What? I've never needed to do that when compiling programs with -L/exet:nt/su:windows:4.0 on Windows. --0016e64640e2497a1304a878f3de Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Jul 19, 2011 at 6:48 PM, Andrej Mitrovic <span dir=3D"ltr"><<a h= ref=3D"mailto:andrej.mitrovich gmail.com">andrej.mitrovich gmail.com</a>>= ;</span> wrote:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_qu= ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex= ;"> <br> This is similar to how it's done for Windows GUI applications, where<br=
it's hidden by some GUI framework).<br> </blockquote></div><br><div>What? =A0I've never needed to do that when = compiling programs with=A0<span class=3D"Apple-style-span" style=3D"font-fa= mily: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono'= ;, 'Lucida Console', monospace; font-size: 12px; white-space: pre; = background-color: rgb(255, 255, 255); ">-L/exet:nt/su:windows:4.0 </span><s= pan class=3D"Apple-style-span" style=3D"white-space: pre; background-color:= rgb(255, 255, 255); "><font class=3D"Apple-style-span" face=3D"arial, helv= etica, sans-serif">on Windows.</font></span></div> --0016e64640e2497a1304a878f3de--
Jul 19 2011
On 7/20/11, Jimmy Cao <jcao219 gmail.com> wrote:What? I've never needed to do that when compiling programs with -L/exet:nt/su:windows:4.0 on Windows.
If you use a main() function, DMD will add the runtime stuff for you. But if you use WinMain as your main function, you have to initialize manually. For example, if you try to compile and run this app: module win; import core.runtime; import std.c.windows.windows; extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { int[] a; a.length = 5000; auto b = a[]; return 1; }dmd win.d -L/exet:nt/su:windows:4.0 && win.exe
You'll see that it crashes: --------------------------- win.exe - Application Error --------------------------- The instruction at "0x00407cf6" referenced memory at "0x00000000". The memory could not be "read". Click on OK to terminate the program Click on CANCEL to debug the program --------------------------- OK Cancel ---------------------------
Jul 19 2011
--0016e64640e2309d2c04a87933a0 Content-Type: text/plain; charset=ISO-8859-1 Oh, I see. That's very convenient. On Wed, Jul 20, 2011 at 12:04 AM, Andrej Mitrovic < andrej.mitrovich gmail.com> wrote:On 7/20/11, Jimmy Cao <jcao219 gmail.com> wrote:What? I've never needed to do that when compiling programs with -L/exet:nt/su:windows:4.0 on Windows.
If you use a main() function, DMD will add the runtime stuff for you. But if you use WinMain as your main function, you have to initialize manually. For example, if you try to compile and run this app: module win; import core.runtime; import std.c.windows.windows; extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { int[] a; a.length = 5000; auto b = a[]; return 1; }dmd win.d -L/exet:nt/su:windows:4.0 && win.exe
You'll see that it crashes: --------------------------- win.exe - Application Error --------------------------- The instruction at "0x00407cf6" referenced memory at "0x00000000". The memory could not be "read". Click on OK to terminate the program Click on CANCEL to debug the program --------------------------- OK Cancel ---------------------------
--0016e64640e2309d2c04a87933a0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Oh, I see.<div>That's very convenient.<br><br><div class=3D"gmail_quote= ">On Wed, Jul 20, 2011 at 12:04 AM, Andrej Mitrovic <span dir=3D"ltr"><<= a href=3D"mailto:andrej.mitrovich gmail.com">andrej.mitrovich gmail.com</a>= ></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im">On 7/20/11, Jimmy Cao <= ;<a href=3D"mailto:jcao219 gmail.com">jcao219 gmail.com</a>> wrote:<br> > What? =A0I've never needed to do that when compiling programs with= <br> > -L/exet:nt/su:windows:4.0<br> > on Windows.<br> ><br> <br> </div>If you use a main() function, DMD will add the runtime stuff for you.= <br> But if you use WinMain as your main function, you have to initialize<br> manually.<br> <br> For example, if you try to compile and run this app:<br> <br> module win;<br> <br> import core.runtime;<br> import std.c.windows.windows;<br> <br> extern (Windows)<br> int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR<br> lpCmdLine, int iCmdShow)<br> {<br> =A0 =A0int[] a;<br> <br> =A0 =A0a.length =3D 5000;<br> =A0 =A0auto b =3D a[];<br> <br> =A0 =A0return 1;<br> }<br> <br> > dmd win.d -L/exet:nt/su:windows:4.0 && win.exe<br> <br> You'll see that it crashes:<br> ---------------------------<br> win.exe - Application Error<br> ---------------------------<br> The instruction at "0x00407cf6" referenced memory at "0x0000= 0000". The<br> memory could not be "read".<br> <br> <br> Click on OK to terminate the program<br> Click on CANCEL to debug the program<br> ---------------------------<br> OK =A0 Cancel<br> ---------------------------<br> </blockquote></div><br></div> --0016e64640e2309d2c04a87933a0--
Jul 19 2011









Charles Hixson <charleshixsn earthlink.net> 