简体   繁体   English

使用R中的Rcpp时出错

[英]Error using Rcpp from R

I'm trying to call a C++ code from R. I've installed the packages Rtools and Rcpp. 我试图从R调用C ++代码。我已经安装了Rtools和Rcpp软件包。 And I set up the 2 environment variables for g++. 我为g ++设置了2个环境变量。

But when I run this code: 但是当我运行这段代码时:

library(inline) 
library(Rcpp)
src <- ' 
  std::vector<std::string> s; 
  s.push_back("hello");
  s.push_back("world");
  return Rcpp::wrap(s);
'
hellofun <- cxxfunction(body = src, includes = '', plugin = 'Rcpp', verbose = FALSE)
cat(hellofun(), '\n')

I get: 我得到:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! 
> cat(hellofun(), '\n') 
Error in cat(hellofun(), "\n") : could not find function "hellofun"

Though, g++ is detected: 不过,可以检测到g ++:

> system('g++ -v')
Using built-in specs.
COLLECT_GCC=C:\Rtools\GCC-46~1.3\bin\G__~1.EXE
COLLECT_LTO_WRAPPER=c:/rtools/gcc-46~1.3/bin/../libexec/gcc/i686-w64-mingw32/4.6.3/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: /data/gannet/ripley/Sources/mingw-test3/src/gcc/configure --host=i686-w64-mingw32 --build=x86_64-linux-gnu --target=i686-w64-mingw32 --with-sysroot=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --prefix=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --with-gmp=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpfr=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpc=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --disable-shared --enable-static --enable-targets=all --enable-languages=c,c++,fortran --enable-libgomp --enable-sjlj-exceptions --enable-fully-dynamic-string --disable-nls --disable-werror --enable-checking=release --disable-win32-registry --disable-rpath --disable-werror CFLAGS='-O2 -mtune=core2 -fomit-frame-pointer' LDFLAGS=
Thread model: win32
gcc version 4.6.3 20111208 (prerelease) (GCC)

What's the problem? 有什么问题?

Using cppFunction , this works for example: 使用cppFunction ,例如,它可以工作:

library(inline) 
library(Rcpp)
src <- ' 
SEXP hellofun(){
 std::vector<std::string> s; 
  s.push_back("hello");
s.push_back("world");
return Rcpp::wrap(s);
}'
hello_fun <- cppFunction(src)

hello_fun()
[1] "hello" "world"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM