[Rcpp-devel] Including a compiled function with inline
Douglas Bates
bates at stat.wisc.edu
Mon Jun 4 18:36:52 CEST 2012
On Mon, Jun 4, 2012 at 4:19 AM, Marie Auger-Methe
<marie.augermethe at gmail.com> wrote:
> Hi list,
> I am writing a Rcpp function (referred as f2). To be able to debug it, I am
> currently using inline to write f2. f2 uses a function that I've wrote and
> is now part of a package that I have made (referred as f1). f1 is an Rcpp
> function that is not called within R but only called from Rcpp functions. I
> was wondering how I could use this f1 function within f2 when using inline.
> From what I understand I should be able to use the argument includes from
> cxxfunction. I think it is related to this thread:
> http://thread.gmane.org/gmane.comp.lang.r.rcpp/2593/focus=2600
> But I can't quite figure out.
Unfortunately, it's complicated if you want to access the compiled
code from another package. There is a mechanism for another
package's compiled C functions but it is somewhat unwieldy. If you
want to access another package's compiled C++ classes and methods and
stand-alone functions you need to arrange for the compiled code to be
in a known location and have this location on the path for the loader.
The Rcpp package does this but getting the details right on multiple
platforms is tricky.
Perhaps the easiest approach is to include not only the headers but
all the C++ code in the file listed in the includes argument to
cxxfunction. It's a bit wasteful but at least it works. :-)
>
> Here is a small example:
>
> library(inline)
> library(fxwithinfx) # My package that contain f1
>
> src <- '
> using namespace Rcpp;
> double y = 9;
> NumericVector z(1);
>
> f1(y, z);
> z[0] = z[0] + 1;
> return z;
> '
> f2 <- cxxfunction(signature(), body = src, plugin = "Rcpp", includes =
> "#include <fxwithinfx/f1.h>")
>
> # I get the following error:
>
> Error in compileCode(f, code, language = language, verbose = verbose) :
> Compilation ERROR, function(s)/method(s) not created!
> file11741d6932c4.cpp:19:27: fatal error: fxwithinfx/f1.h: No such file or
> directory
> compilation terminated.
> # I tried to change the reference to f1.h by changing the path, but whatever
> path I give I still get an error. For example, if I use this instead:
> f2 <- cxxfunction(signature(), body = src, plugin = "Rcpp",
> includes = "#include
> <C:/Users/Marie/Documents/R/fxwithinfx/src/f1.h>")
> # I get the following error:
>
> Error in compileCode(f, code, language = language, verbose = verbose) :
> Compilation ERROR, function(s)/method(s) not created!
> file11743a6031d8.o:file11743a6031d8.cpp:(.text+0x104): undefined reference
> to `f1'
> collect2: ld returned 1 exit status
>
> # I'm actually not sure whether I should refer the actual .h location or to
> the location within my R libraries
>
> # Here are the header and src codes for the f1
>
> f1.h:
>
> #ifndef _fxwithinfx_f1_H
> #define _fxwithinfx_f1_H
>
> #include <Rcpp.h>
>
> extern "C" int f1(double y, SEXP z) ;
>
> #endif
>
>
> f1.cpp:
>
> #include "f1.h"
>
> int f1(double y, SEXP z){
> using namespace Rcpp;
>
> NumericVector zz(z);
> zz[0] = y + 1.5;
> }
>
>
>
> Any help will be appreciated!
>
> Marie
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
More information about the Rcpp-devel
mailing list