Pedro Rafael
Pedro Rafael

Reputation: 522

Rcpp - Compiling outside the structure of a package

I'm having a question about using a C ++ code using Rcpp outside of a package structure.

To clarify my doubt, consider the C ++ code (test.cpp) below:

// [[Rcpp::depends(RcppGSL)]]

#include <Rcpp.h>
#include <numeric>
#include <gsl/gsl_sf_bessel.h>
#include <RcppGSL.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
}

// [[Rcpp::export]]
double my_bessel(double x){
  return gsl_sf_bessel_J0 (x);
}

// [[Rcpp::export]]
int tamanho(NumericVector x){
  int n = x.size();

  return n;
}

// [[Rcpp::export]]
double soma2(NumericVector x){
  double resultado = std::accumulate(x.begin(), x.end(), .0);
  return resultado; 
}

// [[Rcpp::export]]
Rcpp::NumericVector colNorm(const RcppGSL::Matrix & G) {
  int k = G.ncol();
  Rcpp::NumericVector n(k);           // to store results
  for (int j = 0; j < k; j++) {
    RcppGSL::VectorView colview = gsl_matrix_const_column (G, j);
    n[j] = gsl_blas_dnrm2(colview);
  }
  return n;                           // return vector
}

The above code works when it is inside the structure of a package. As we know, using the Rcpp::compileAttributes() the file is created RcppExports.cpp. So I will have access to the functions in the R. environment.

My interest is to use the C++ function implemented using the Rcpp outside the framework of a package. For this I compiled the C ++ code using the g ++ compiler as follows:

g++ -I"/usr/include/R/" -DNDEBUG -I"/home/pedro/R/x86_64-pc-linux-gnu-library/3.5/Rcpp/include" -I"/home/pedro/Dropbox/UFPB/Redes Neurais e Análise de Agrupamento/Rcpp" -I /home/pedro/R/x86_64-pc-linux-gnu-library/3.5/RcppGSL/include -D_FORTIFY_SOURCE=2   -fpic  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt  -c test.cpp -o test.o -lgsl -lgslcblas -lm
g++ -shared -L/usr/lib64/R/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o produto.so produto.o -L/usr/lib64/R/lib -lR -lgsl -lgslcblas -lm

The compilation occurred successfully and no warning message was issued. In this way, the test.o andtest.so files were generated. Already in R, using the .Call interface, I did:

dyn.load("test.so")

my_function <- function(x){
    .Call("soma2",x)
}

When trying to use the my_function () function, an error occurs stating that soma2 is not in the load table. Is there any way to create the RcppExports.cpp file outside the framework of a package? I guess the correct one would be to have compiled the code RcppExports.cpp and not test.cpp.

Thanks in advance.

Upvotes: 1

Views: 375

Answers (1)

Ralf Stubner
Ralf Stubner

Reputation: 26823

If you are working outside of a package you can simply use Rcpp::sourceCpp(<file>). This will take care of compilating, linking and prociding an R wrapper for you. With your file I get:

> Rcpp::sourceCpp("test.cpp")
> soma2(1:5)
[1] 15

Upvotes: 5

Related Questions