Reputation: 318
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
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
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
object code not found
from the first error, and
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
remove the line with the #include <gsl/gsl_const_mksa.h>
from the code
assignment
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