How to compile Phobos with other D code to create a shared library?
data pulverizer
data.pulverizer at gmail.com
Sat Jun 5 07:39:02 UTC 2021
On Friday, 4 June 2021 at 15:19:17 UTC, bachmeier wrote:
>
> It requires an R package if you want to call D functions from
> R. You need to link to R itself if you want to do something
> like pass a vector from R to D and then access that data from
> D. Since R is aware of the location of all the underlying
> libraries, it's easiest to let it generate the dub.sdl for you.
>
> I guess you're using the older .C interface [as done
> here](http://users.stat.umn.edu/~geyer/rc/). Good enough if
> passing double* and int* pointers to D functions will suffice.
> Only thing you need to do to initialize the D runtime is add
> this to your D code
>
> ```
> struct DllInfo;
>
> extern(C) void R_init_libfoo(DllInfo * info) {
> Runtime.initialize();
> }
> ```
>
> where foo is the name of the library you're loading (foo.so).
I've just read the documentation and had a go, **compiling with
GDC worked for both `.C` and `.Call`**.
I tried with all three compilers DMD, LDC, and GDC, and looked at
the outputs with the nm tool. I don't think this matters much but
DMD and LDC are more likely to list functions (DMD) or symbols in
referenced modules (LDC) as weak symbols. I think the more likely
the reason GDC worked is because it is a GNU compiler this has
full GLIBC compatibility with reference to the `librt.so.1`
`GLIBC_PRIVATE` error.
The R ext documentation you kindly referenced says:
*"By default, R uses the operating-system-specific dynamic loader
to lookup the symbol in all loaded DLLs and the R executable or
libraries it is linked to."*
and that:
*"Registering routines has two main advantages: it provides a
faster way to find the address of the entry point via tables
stored in the DLL at compilation time, and it provides a run-time
check that the entry point is called with the right number of
arguments and, optionally, the right argument types."*
So registering symbols is not the issue. The error was specific
to GLIBC which is probably why the GNU compiler worked.
Thank you.
More information about the Digitalmars-d-learn
mailing list