Shared library seemingly not containing any routines

I am trying to make an R interface for the C ++ Faddeeva library (various types of complex error functions); unfortunately I have very little experience with calling external code in R and it is quite difficult.

My naive attempt

R CMD SHLIB Faddeeva.cc

      

there was a shared library Faddeeva.so

that I load in R,

dyn.load("Faddeeva.so")
dlls <- getLoadedDLLs()
getDLLRegisteredRoutines(dlls[['Faddeeva']])

      

It's empty, I haven't registered any of the functions. I believe I will have to write interface code to work with SEXP so that I can use the .Call interface (can Rcpp make this step easier?), But I'm still confused as to why there are no registered subroutines in this shared library.

Any advice or guidance for running such a project would be appreciated!


Edit: Thanks to Dirk's answer and help with Rcpp, the interface is now implemented in the Faddeeva package .

+3


source to share


1 answer


I would step back and look at other packages that use external libraries. Old, but goldie RcppGSL , but for example RcppRedis using (C-library) hiredis .

There is really no deep magic:

  • Use a header file from another library to declare identifiers
  • Write a short C ++ file to deploy one of these ids
  • Link to another library (which should be visible on your system) in Linux do ldconfig -p | grep libraryname

    ) by adding it src/Makevars

    to PKG_LIBS

    .
  • Alternatively, if you pick up Johnson Fadeeva sources in your package, just include the files in src/

    and skip step 3.

Note that I didn't say anything about Rcpp . This "only" helps with point 2. The rest is the same as linking the C library with the R extensions. Which is probably thousands of different packages on CRAN.

If you are completely lost, consider the new r-package-devel list , but read a little at Insert R Extensions First .

The Jelmer nloptr package wraps another Johnson library: nlopt . Maybe it can help as inspiration. I helped a little to make the installation more efficient (using an existing system libnlopt

).

Edit: I looked more at the page from Johnson . There is no library. Simple .cc

and .hh

. Put them in src/

your package, perhaps renaming it to .cpp

and .h

- and you're done!



Edit 2: Ok, I created a small sample package from the recipe I just described. Using Rcpp the caller becomes as simple as

#include <Rcpp.h>
using namespace Rcpp;

#include "Faddeeva.h"

// [[Rcpp::export]]
double Dawson(double x) { // special case for real x
  return Faddeeva::Dawson(x);
}

      

and we can use the package as usual:

edd@max:/tmp$ Rscript -e 'library(RcppFaddeeva); Dawson(4.2)'
[1] 0.122761
edd@max:/tmp$ 

      

I'll put this up on GitHub in a moment.

Edit 3: It's now in this GitHub repository .

+3


source







All Articles