How to create standalone Rmath.h library to access R functions in C?

Viewed 163

I am facing difficulties in creating Rmath.dll. I have already referred to several sources:

  1. R-exts 6.17: Using these functions in your own C code
  2. R-admin 9: The standalone Rmath library
  3. Using the R Standalone Math Library

The problems which I am facing are as follows:

  1. My R (4.1.0) installation directory does not contain the src/nmath/standalone directory to build the library as mentioned in "6.17 Using these functions in your own C code".

  2. I tried building the library by downloading and extracting the R-4.1.0.tar file and then following the instructions given here in R-admin. But still getting the error "undefined reference to `dnorm4'" after executing the command as shown below:

    gcc -g -c -o Interoperate.o Interoperate.c -ID:/R-4.1.0/src/include -LD:/R-4.1.0/src/nmath/standalone/  -lRmath -lm
    

Interoperate.c contains the following code:

#define MATHLIB_STANDALONE
#include <R.h>
#include <Rmath.h>

int main()
{
/*  Inputs  */
    double V1 = 1;
    double V2 = 2;

/*  Normal Distribution  */ 
    double result = dnorm(V1, V2, M_PI, 1);
    printf("%lf\n",result);
    return 0;
}

I am out of options now. I need help with this issue.

1 Answers

You generally build libRmath.a -- the Rmath library -- when building R. It is done each and every time the Debian (and Ubuntu) packages are built because I have set it up that way. Now it may been decades since I last tried this on Windows so I cannot help you with that part -- but at the end of the day it is a call to ar that glues the files together so if you know how to do this you may be able to do it by hand.

Your example works fine here (on Ubuntu):

$ gcc -o answer answer.c -lRmath -I /usr/share/R/include
$ ./answer 
-2.114329
$ 
$ ldd answer
        linux-vdso.so.1 (0x00007ffe3a391000)
        libRmath.so.1 => /usr/lib/libRmath.so.1 (0x00007f12e10b8000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f12e0ece000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f12e0d7f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f12e0d5d000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f12e111f000)
$ 
Related