2013-12-11 70 views
1

我正在尝试从R调用C++代码。我已经安装了软件包Rtools和Rcpp。我为g ++设置了2个环境变量。使用Rcpp从R发生错误

但是当我运行这段代码:

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') 

我得到:

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" 

虽然,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) 

什么问题?

+3

可能是冗长设置为TRUE,以获取有关什么地方出了错更多有用的信息。另外,从内联使用'cxxfunction'是一种古老的方式。现在,我们使用更好的'cppFunction'或'sourceCpp'。 –

+0

你的代码的复制/粘贴在'W7_64''R版本3.0.1(2013-05-16)','Rcpp'0.10.4''上适用(是的,是的,我知道我在后面!) 。有关您的设置的更多信息将是必需的。 ('sessionInfo()'等)。 –

+0

我在这里找到了这个例子(https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started)。我的目标是使用Rstan,但它基于Rcpp。你能告诉我如何设置详细模式吗? –

回答

0

使用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" 
+0

当我尝试你的代码,我得到以下错误: MAKE 5.2版版权所有(C)1987,2000 Borland公司 错误sourceCpp(代码=代码,ENV = ENV,重建=重建,showOutput = showOutput,:发生 错误建立共享库 –

+1

所以你让Borland在Rtools之前的Rtools之前在你的PATH中做出了那个,没有,没有,工作 –

+0

好吧,在把所有其他的路径放在其他路径之前,它工作了!运行rstan(失败),行cppFunction(src)不再编译...错误太长,无法在注释中报告。 –