Additional Source Files

Using rdlang as described earlier works well when you have a single .d source file. It is common when writing a D program to put functions into their own file or to include shared library (.so) files.

You can add extra options to the dmd call by creating a file called dmdadd.txt in the shared directory. Continuing on with the example where you are using Windows, and you have created the shared directory C:\Users\user1\dockershare, let's create another file called "test2.d". Add the following lines and save it:

import std.stdio;
import rinsided;
import foobar;

void main() {
    evalRQ(`print("Hello from R!")`);
    writeln(timestwo(3.5));
}

The timestwo function is defined in foobar.d. Create that file in C:\Users\user1\dockershare, add the following lines, and save it:

double timestwo(double x) {
    return 2*x;
}

rdlang doesn't know anything about foobar.d. If you were to call rdlang at this point, you would get an error. Let's try it:

rdlang test2

The output is

test2.d(3): Error: module foobar is in file 'foobar.d' which cannot be read
import path[0] = /usr/include/dmd/phobos
import path[1] = /usr/include/dmd/druntime/import


/bin/sh: 1: ./test2: not found

In order for test2.d to compile and run, you need to tell dmd about foobar.d. We do that by creating a file dmdadd.txt with the following contents:

foobar.d

That will add foobar.d to the call to dmd. Now let's try it again.

rdlang test2

At the bottom of the output, you see

[1] "Hello from R!"
7

You can put any additional compilation options in dmdadd.txt.

A Note On The Design Of rdlang

The commands in dmdadd.txt will automatically be added to the compilation if that file exists. It would be easy to modify rdlang to take an optional argument indicating whether or not there are additional options to add to the command line, and indeed, that is the way the rdlang script originally worked. It quickly became apparent that it is inconvenient to have to do that with every compilation, particularly when you consider that it's not necessary if you're following the good practice of keeping each project in its own directory. That led to the current approach.

What About Dub?

Many D programmers love Dub. You may even be wondering why I wrote the rdlang script at all when I could have just used Dub. If you are a happy Dub user, all you need to know is that it is installed on the image, so you can issue commands like dub build if you want. If you want to know the reasons for my choice, you can read them here.