r语言 - cppp错误中的函数

r - cppFunction in Rcpp error

本文关键字:函数 错误 cppp 语言      更新时间:2023-10-16

我以前使用Rcpp和RcppArmadillo很流畅,直到大约3-4个月前。我今天又试了一次,由于某种原因,我一直得到以下错误:

> blasso_Rcpp <- cppFunction(depends='RcppArmadillo', code ='
+     arma::mat blasso_cpp(arma::mat x)
+     {
+     return(x);
+ 
+     }
+ 
+ 
+ 
+ ')
Error in dyn.load("/var/folders/6h/ckff_c490ml7_lmhj4xqv3lw0000gp/T//RtmpnVptIY/sourcecpp_553970870d16/sourceCpp_3.so") : 
  unable to load shared object '/var/folders/6h/ckff_c490ml7_lmhj4xqv3lw0000gp/T//RtmpnVptIY/sourcecpp_553970870d16/sourceCpp_3.so':
  dlopen(/var/folders/6h/ckff_c490ml7_lmhj4xqv3lw0000gp/T//RtmpnVptIY/sourcecpp_553970870d16/sourceCpp_3.so, 6): Symbol not found: ___addtf3
  Referenced from: /usr/local/lib/libquadmath.0.dylib
  Expected in: /usr/local/lib/libgcc_s_x86_64.1.dylib
 in /usr/local/lib/libquadmath.0.dylib
> 

当我不使用RcppArmadillo时,我没有得到错误。

> blasso_Rcpp <- cppFunction(code ='
+     NumericMatrix blasso_cpp(NumericMatrix x)
+     {
+     return(x);
+ 
+     }
+  
+ ')

如果我单独保存cpp文件并使用sourceCpp进行源,我也会遇到同样的问题。我重新安装了Xcode, Rcpp, RcppArmadillo。这是我的会话信息。

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
[1] RcppArmadillo_0.6.100.0.0 Rcpp_0.12.1              
loaded via a namespace (and not attached):
[1] tools_3.2.2

知道为什么会这样吗?

Edit:添加gcc和gfortran信息

$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/gfortran/libexec/gcc/x86_64-apple-darwin14/5.1.0/lto-wrapper
Target: x86_64-apple-darwin14
Configured with: ../gcc-5.1.0/configure --prefix=/usr/local/gfortran --with-gmp=/Users/fx/devel/gcc/deps-static/x86_64 --enable-languages=c,c++,fortran,objc,obj-c++ --build=x86_64-apple-darwin14
Thread model: posix
gcc version 5.1.0 (GCC) 

下面是从源代码

安装RcppArmadillo时的错误
> install.packages("RcppArmadillo", type = "source")
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://mirrors.nics.utk.edu/cran/src/contrib/RcppArmadillo_0.6.100.0.0.tar.gz'
Content type 'application/x-gzip' length 981341 bytes (958 KB)
==================================================
downloaded 958 KB
* installing *source* package ‘RcppArmadillo’ ...
** package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
* checking LAPACK_LIBS: divide-and-conquer complex SVD available via R-supplied LAPACK
** libs
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include"  -I../inst/include -fPIC  -Wall -mtune=core2 -g -O2  -c RcppArmadillo.cpp -o RcppArmadillo.o
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include"  -I../inst/include -fPIC  -Wall -mtune=core2 -g -O2  -c RcppExports.cpp -o RcppExports.o
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include"  -I../inst/include -fPIC  -Wall -mtune=core2 -g -O2  -c fastLm.cpp -o fastLm.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o RcppArmadillo.so RcppArmadillo.o RcppExports.o fastLm.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/libs/RcppArmadillo.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/libs/RcppArmadillo.so, 6): Symbol not found: ___addtf3
  Referenced from: /usr/local/lib/libquadmath.0.dylib
  Expected in: /usr/local/lib/libgcc_s_x86_64.1.dylib
 in /usr/local/lib/libquadmath.0.dylib
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo’
The downloaded source packages are in
    ‘/private/var/folders/6h/ckff_c490ml7_lmhj4xqv3lw0000gp/T/Rtmp82GBGH/downloaded_packages’
Warning message:
In install.packages("RcppArmadillo", type = "source") :
  installation of package ‘RcppArmadillo’ had non-zero exit status

这里可以正常工作:

R> Rcpp::cppFunction('arma::mat blasso_cpp(arma::mat x) { return x; }',
+                    depends='RcppArmadillo')
R> blasso_cpp(matrix(1:4,2))
     [,1] [,2]
[1,]    1    3
[2,]    2    4
R> 

可能有东西不小心踩坏了您的RcppArmadillo安装。也许重新安装一下?

同样,您不需要分配来自cppFunction()的结果——它创建的函数会自动分配给代码中找到的相同标识符。