www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - calling D2 routines from C routines on linux

reply Charles Hixson <charleshixsn earthlink.net> writes:
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
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
next sibling parent Charles Hixson <charleshixsn earthlink.net> writes:
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
prev sibling parent Charles Hixson <charleshixsn earthlink.net> writes:
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
prev sibling next sibling parent Jimmy Cao <jcao219 gmail.com> writes:
On Tue, Jul 19, 2011 at 6:48 PM, Andrej Mitrovic <andrej.mitrovich gmail.com
 wrote:
 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.
Jul 19 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling parent Jimmy Cao <jcao219 gmail.com> writes:
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 ---------------------------
Jul 19 2011