[Rcpp-devel] Problem with NumericVector or NumericMatrix

Dirk Eddelbuettel edd at debian.org
Tue Mar 1 12:51:18 CET 2016


On 1 March 2016 at 10:29, Philippe Huber wrote:
| Hi,
| 
| I am starting to use Rcpp and got into some issues with basic examples.
| 
| I am trying to run this:
| 
| #include <Rcpp.h>
| 
| using namespace Rcpp;
| 
| // [[Rcpp::export]]
| NumericVector timesTwo(NumericVector x) {
|   return x * 2;
| }
| 
| It works fine when I use it like:
| > evalCpp(timesTwo(2))
| [1] 4
| 
| But as soon as I use a vector, I get some weird error message:
| 
| > v=as.vector(c(2,2))
| > evalCpp(timesTwo(v))
| g++ -m64 -I"C:/PROGRA~1/R/R-32~1.3/include" -DNDEBUG     -I"C:/Users/Hubert/
| Documents/R/win-library/3.2/Rcpp/include" -I"C:/Users/Hubert/AppData/Local/Temp
| /RtmpaujML3"  -I"d:/RCompile/r-compiling/local/local323/include"     -O2 -Wall
|  -mtune=core2 -c file33c329d4fd2.cpp -o file33c329d4fd2.o
| file33c329d4fd2.cpp: In function 'SEXPREC* get_value()':
| file33c329d4fd2.cpp:7:6: error: redefinition of 'SEXPREC* get_value()'
| file33c329d4fd2.cpp:6:6: error: 'SEXPREC* get_value()' previously defined here
| make: *** [file33c329d4fd2.o] Error 1
| Warning message:
| l'exécution de la commande 'make -f "C:/PROGRA~1/R/R-32~1.3/etc/x64/Makeconf"
| -f "C:/PROGRA~1/R/R-32~1.3/share/make/winshlib.mk" SHLIB_LDFLAGS='$
| (SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_10.dll" WIN=64
| TCLBIN=64 OBJECTS="file33c329d4fd2.o"' renvoie un statut 2 
| Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput =
| showOutput,  : 
|   Error 1 occurred building shared library.
| 
| I tried to find a solution but got nothing.
| 
| Can someone please help me?

help(evalCpp) tells you "Evaluates a C++ expression" which is correct. The
canonical example is something like

   evalCpp("2 + 2")

What you want here is sourceCpp() to source and run a file.  Please see the
vignette 'Rcpp Attribute' for countless examples. You can, as in the
augmented example below, even include R code to be run after the build:

  R> sourceCpp("/tmp/sourceCppEx.cpp")

  R> timesTwo(21)
  [1] 42

  R> timesTwo(c(1,3,5))
  [1]  2  6 10
  R> 

As you can see, works as expected.

Dirk

------------- example with included R snippet below -------------------------
#include <Rcpp.h>

using namespace Rcpp;

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

/***R
timesTwo(21)
timesTwo(c(1,3,5))
*/
-----------------------------------------------------------------------------


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


More information about the Rcpp-devel mailing list