carlos1985
carlos1985

Reputation: 318

Rcpp code example cppFunction

I am trying run a code of "Seamless R and C++ Integration with Rcpp" (Page 32, Listing 2.10) but It´s giving a error. Could someone explain to me why is not working? Thanks

Code <- ' 
#include <gsL/gsl_const_mksa.h>           // decl of constants 
std::vector<double> volumes() { 
std::vector<double> v(5); 
v[0] = GSL_CONST_MKSA_US_GALLON;       // 1 US gallon 
v[1] = GSL_CONST_MKSA_CANADIAN_GALLON; // 1 Canadian gallon 
v[2] = GSL_CONST_MKSA_UK_GALLON;       // 1 UK gallon 
v[3] = GSL_CONST_MKSA_QUART;           // 1 quart 
v[4] = GSL_CONST_MKSA_PINT;            // 1 pint 
return v; 
}' 

gslVolumes <- cppFunction(code, depends="RcppGSL") 

This is the message error:

file16e2b6cb966.cpp: In function ‘SEXPREC* sourceCpp_52966_volumes()’: 
file16e2b6cb966.cpp:30: error: ‘__result’ was not declared in this scope 
make: *** [file16e2b6cb966.o] Error 1 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppGSL/include"    -fPIC  -mtune=core2 -g -O2  -c file16e2b6cb966.cpp -o file16e2b6cb966.o 
Erro em sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
  Error 1 occurred building shared library. 

Upvotes: 3

Views: 1334

Answers (2)

Romain Francois
Romain Francois

Reputation: 17642

In addition to what Dirk said, I recommend that you promote the code to a .cpp file.

// [[Rcpp::depends(RcppGSL)]]
#include <Rcpp.h>
#include <gsl/gsl_const_mksa.h>           // decl of constants 

// [[Rcpp::export]]
std::vector<double> volumes() { 
  std::vector<double> v(5); 
  v[0] = GSL_CONST_MKSA_US_GALLON;       // 1 US gallon 
  v[1] = GSL_CONST_MKSA_CANADIAN_GALLON; // 1 Canadian gallon 
  v[2] = GSL_CONST_MKSA_UK_GALLON;       // 1 UK gallon 
  v[3] = GSL_CONST_MKSA_QUART;           // 1 quart 
  v[4] = GSL_CONST_MKSA_PINT;            // 1 pint 
  return v; 
}   

Then, you can sourceCpp that file.

Upvotes: 3

Dirk is no longer here
Dirk is no longer here

Reputation: 368539

It looks like you have typos:

Code <- ' 
#include <gsL/gsl_const_mksa.h>           // decl of constants 

That should be code <- with lower-case c, and then #include <gsl/gsl_const_mksa.h> with a lower-case 'ell'.

In general, I recomment to switch on verbose mode:

gslVolumes <- cppFunction(code, depends="RcppGSL", verbose=TRUE) 

which would have told you about

  1. object code not found from the first error, and

  2. file....cpp:10:63: fatal error: gsL/gsl_const_mksa.h: No such file or directory

about the missing header.

But I do see now that with the current versions, I also get __result not declared. Will investigate.

Edit: It's a bug / change. It worked when I wrote the chapter, now you need to

  1. remove the line with the #include <gsl/gsl_const_mksa.h> from the code assignment

  2. add a new includes=... argument to the cppFunction() call as below:

Corrected call:

 gslVolumes <- cppFunction(code, depends="RcppGSL",
                           includes="#include <gsl/gsl_const_mksa.h>")

Upvotes: 5

Related Questions