Calling / running / executing .d script from another .d script

Ali Çehreli acehreli at yahoo.com
Sun Jul 28 21:43:24 UTC 2019


On 07/28/2019 05:56 AM, BoQsc wrote:
> Right now, I'm thinking what is correct way to run another .d script 
> from a .d script. Do you have any suggestions?

I can be easier than a shared library solution that Max Haughton 
described. (None of the following code are compiled.)

If you have the following code in foo.d:

module foo;

int foo(int i) {
   return i + 42;
}

You can call it from bar.d like this:

module bar;

import foo;

int bar(int i) {
   return foo(i);
}

Then you can combine the two modules with the following main:

module main;

import bar;

void main() {
   bar(7);
}

Here is how to compile the whole program:

dmd main.d bar.d foo.d -of=my_program

However, it can be even simpler with dmd's -i command line switch, which 
automatically includes all imported modules in the program:

dmd main.d -of=my_program

Ali


More information about the Digitalmars-d-learn mailing list