[Rcpp-devel] Got error when trying to run example in Intro vignette

Dirk Eddelbuettel edd at debian.org
Tue Dec 21 18:33:14 CET 2010


On 21 December 2010 at 12:21, Gabor Grothendieck wrote:
| I tried copying the example in the intro vignette
|    http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf
| and get the following error. What's wrong?
| 
| > library(inline)
| > library(Rcpp)
| > funIntro <- cxxfunction(signature(a = "numeric", b = "numeric"), body = '
| + RcppExport SEXP convolve3cpp(SEXP a, SEXP b) {
| + Rcpp::NumericVector xa(a);
| + Rcpp::NumericVector xb(b);
| + int n_xa = xa.size(), n_xb = xb.size();
| + int nab = n_xa + n_xb - 1;
| + Rcpp::NumericVector xab(nab);
| + for (int i = 0; i < n_xa; i++)
| + for (int j = 0; j < n_xb; j++)
| + xab[i + j] += xa[i] * xb[j];
| + return xab;
| + }', plugin="Rcpp")
| file303dbfd.cpp: In function 'SEXPREC* file303dbfd(SEXPREC*, SEXPREC*)':
| file303dbfd.cpp:31:1: error: expected unqualified-id before string constant
| make: *** [file303dbfd.o] Error 1
| 
| ERROR(s) during compilation: source code errors or compiler
| configuration errors!
| 
| Program source:
|   1:
|   2: // includes from the plugin
|   3:
|   4: #include <Rcpp.h>
|   5:
|   6:
|   7: #ifndef BEGIN_RCPP
|   8: #define BEGIN_RCPP
|   9: #endif
|  10:
|  11: #ifndef END_RCPP
|  12: #define END_RCPP
|  13: #endif
|  14:
|  15: using namespace Rcpp;
|  16:
|  17:
|  18: // user includes
|  19:
|  20:
|  21: // declarations
|  22: extern "C" {
|  23: SEXP file303dbfd( SEXP a, SEXP b) ;
|  24: }
|  25:
|  26: // definition
|  27:
|  28: SEXP file303dbfd( SEXP a, SEXP b ){
|  29: BEGIN_RCPP
|  30:
|  31: RcppExport SEXP convolve3cpp(SEXP a, SEXP b) {
|  32: Rcpp::NumericVector xa(a);
|  33: Rcpp::NumericVector xb(b);
|  34: int n_xa = xa.size(), n_xb = xb.size();
|  35: int nab = n_xa + n_xb - 1;
|  36: Rcpp::NumericVector xab(nab);
|  37: for (int i = 0; i < n_xa; i++)
|  38: for (int j = 0; j < n_xb; j++)
|  39: xab[i + j] += xa[i] * xb[j];
|  40: return xab;
|  41: }
|  42: END_RCPP
|  43: }
|  44:
|  45:
| Error in compileCode(f, code, language = language, verbose = verbose) :
|   Compilation ERROR, function(s)/method(s) not created!
| file303dbfd.cpp: In function 'SEXPREC* file303dbfd(SEXPREC*,
| SEXPREC*)':
| file303dbfd.cpp:31:1: error: expected unqualified-id before string constant
| make: *** [file303dbfd.o] Error 1
| In addition: Warning message:
| running command 'C:\PROGRA~1\R\R-212~1.X/bin/i386/R CMD SHLIB
| file303dbfd.cpp 2> file303dbfd.cpp.err.txt' had status 1
| >

See how lines 28 _and_ 31 start a function, and 41 _and_ 43 end one? 

With inline you never give the function signature or the closing paren.

R> library(inline)
R> funIntro <- cxxfunction(signature(a = "numeric", b = "numeric"), body = '
+   Rcpp::NumericVector xa(a);
+   Rcpp::NumericVector xb(b);
+   int n_xa = xa.size(), n_xb = xb.size();
+   int nab = n_xa + n_xb - 1;
+   Rcpp::NumericVector xab(nab);
+   for (int i = 0; i < n_xa; i++)
+     for (int j = 0; j < n_xb; j++)
+       xab[i + j] += xa[i] * xb[j];
+   return xab;
+ ', plugin="Rcpp")
R> funIntro(1:10, 3:5)
 [1]   3  10  22  34  46  58  70  82  94 106  85  50
R> 

See the examples/ConvolveBenchmarks/ in source or the installed directory to
see how to do the by Makefile.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list