1

I have written a code in C++ which uses boost library and also uses multiple files. I am trying to execute the function in R.

Here is my C++ function prototype:

extern "C" double function1(double rho, double mu, double limit);

Here is my R function:

dyn.load('path_to_dll\\x64\\Debug\\project.dll') 
.Call("function1", as.double(1.0), mu = as.double(1.0/20.0), limit = as.double(3413.85))

When I am calling the function, it gives me following error:

Error in .Call("function1", as.double(1), mu = as.double(1/20),  : 
  C symbol name "function1" not in load table

I have followed this But no luck. Where am I going wrong?

user1584253
  • 975
  • 2
  • 18
  • 55

2 Answers2

2

Since this a multi-file C++ project, you should package the code as an R package. It is actually quite easy to do using Rcpp, see for example this answer. The Rcpp-package vignette contains further information. In addition, an easy way to make use of boost is via the BH package. There are many example packages for using Rcpp + BH on CRAN which you can study, e.g. my own dqrng. Some more details can also be found in the R Packages book and of course in the official documentation Writing R Extensions.

The crucial thing for using the BH package is LinkingTo: BH in the DESCRIPTION file. This ensures that -I<path_to_BH_package>/include is part of the compiler flags. In order to use header files that you place in inst/include, you should add PKG_CPPFLAGS = -I../inst/include to src/Makevars.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • 1
    And several [Rcpp Gallery](http://gallery.rcpp.org) posts illustrate it. – Dirk Eddelbuettel Jul 03 '18 at 15:13
  • I did followed your suggestion and went through your package dqrng. You are using boost library but there is no header file there. Is it that you are using BH package when you are building or installing the package? I am using this command R CMD INSTALL --library="C:\Users\user1\Documents\R\win-library\3.4\BH" project2_1.0.tar.gz – user1584253 Jul 04 '18 at 12:06
  • Also, I added inst/include folder in the package and copied boost library files there. But getting this error: Euler.h:6:53: fatal error: boost/math/special_functions/binomial.hpp: No such file or directory – user1584253 Jul 04 '18 at 12:08
  • @user1584253 Please see the update. Does that answer your question? – Ralf Stubner Jul 04 '18 at 13:14
  • I am using Windows. Should I create file Makevars in src folder and add PKG_CPPFLAGS = -I../inst/include in it? – user1584253 Jul 04 '18 at 13:31
  • @user1584253 The compiler flags generated from `LinkingTo` work also on Windows. I have removed the explicit path which might be confusing. – Ralf Stubner Jul 04 '18 at 13:36
  • @RalfStubner I linked BH file using 'LinkingTo' attribute and package is successfully installed. Thanks – user1584253 Jul 05 '18 at 05:05
0

Use Rcpp instead of .Call.

Add this headers and comment to your .cpp function file:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]

And now in R:

 install.packages("Rcpp")
 library(Rcpp)
 sourceCpp('path to your .cpp program')

The function EulerInversions should become visible in the environment and you can call it now as any other R function