简体   繁体   English

R CMD SHLIB使用RcppArmadillo编译错误

[英]R CMD SHLIB compile error with RcppArmadillo

I am trying to create a shared object using RcppArmadillo and am having trouble getting the compiler to find all of the headers. 我正在尝试使用RcppArmadillo创建共享对象,并且在使编译器查找所有标头方面遇到麻烦。 This question has nothing to do with the actual use of RcppArmadillo inside my code. 这个问题与我的代码中RcppArmadillo的实际使用无关。

I'm running on Linux/Ubuntu. 我在Linux / Ubuntu上运行。

Here is a very simple cpp file that illustrates the point. 这是一个非常简单的cpp文件,说明了这一点。 This is a stripped down version of the real file. 这是真实文件的精简版本。 You'll see that I have even stripped out any use of RcppArmadillo (though it does use Rcpp). 您会看到,我什至还剔除了对RcppArmadillo的任何使用(尽管它确实使用了Rcpp)。 Since I'm including RcppArmadillo.h, I understand that I should note include Rcpp.h as well. 由于我包括RcppArmadillo.h,因此我了解到我也应该注意包括Rcpp.h。 Nevertheless, I end up with the same error. 尽管如此,我最终还是遇到了同样的错误。

//simpleexample.cpp
#include <RcppArmadillo.h>
#include <cstdlib>
#include <iostream>

using namespace Rcpp;
using namespace RcppArmadillo;
using namespace std;

RcppExport SEXP test_nnls(SEXP Ain, SEXP bin){

  int m,n;
  NumericMatrix Amat(Ain);
  NumericMatrix bvec(bin);
  m = Amat.nrow(); n = Amat.ncol();
  NumericMatrix xvec(n,1);

  return(xvec);
}

The relevant header file is located in this directory. 相关的头文件位于此目录中。

arwen$ pwd
/usr/local/linux/lib/R/Current/x86_64/site-library/RcppArmadillo/include
arwen$ ls -al
total 89
drwxr-xr-x  5 scf scfstaff    15 Feb 22 01:44 ./
drwxr-xr-x 13 scf scfstaff    18 Feb 22 01:44 ../
-rw-r--r--  1 scf scfstaff 19882 Feb 22 01:44 armadillo
drwxr-xr-x  2 scf scfstaff   390 Feb 22 01:44 armadillo_bits/
drwxr-xr-x  2 scf scfstaff     8 Feb 22 01:44 RcppArmadillo/
-rw-r--r--  1 scf scfstaff  6963 Feb 22 01:44 RcppArmadilloAs.h
-rw-r--r--  1 scf scfstaff  2763 Feb 22 01:44 RcppArmadilloConfig.h
drwxr-xr-x  2 scf scfstaff     3 Feb 22 01:44 RcppArmadilloExtensions/
-rw-r--r--  1 scf scfstaff  4529 Feb 22 01:44 RcppArmadilloForward.h
-rw-r--r--  1 scf scfstaff  2169 Feb 22 01:44 RcppArmadillo.h
-rw-r--r--  1 scf scfstaff  1084 Feb 22 01:44 RcppArmadilloLapack.h
-rw-r--r--  1 scf scfstaff  1084 Feb 22 01:44 RcppArmadilloLapack.h.in
-rw-r--r--  1 scf scfstaff  1280 Feb 22 01:44 RcppArmadilloSugar.h
-rw-r--r--  1 scf scfstaff 10817 Feb 22 01:44 RcppArmadilloWrap.h
-rw-r--r--  1 scf scfstaff   232 Feb 22 01:44 README

So now I try to create a shared object using R CMD SHLIB but get the following error. 因此,现在我尝试使用R CMD SHLIB创建共享对象,但出现以下错误。

arwen$ PKG_LIBS=`Rscript -e "Rcpp:::LdFlags()"`
arwen$ PKG_CXXFLAGS=`Rscript -e "RcppArmadillo:::CxxFlags()"`
arwen$ R CMD SHLIB simpleexample.cpp
    g++ -I/usr/share/R/include -DNDEBUG  -I"/server/linux/lib/R/3.0/x86_64/sitelibrary/RcppArmadillo/include" -fpic  -O3 -pipe  -g  -c simpleexample.cpp -o simpleexample.o
In file included from /server/linux/lib/R/3.0/x86_64/site-library/RcppArmadillo/include/RcppArmadillo.h:30:0,
             from simpleexample.cpp:2:
/server/linux/lib/R/3.0/x86_64/site-library/RcppArmadillo/include/RcppArmadilloForward.h:26:24: fatal error: RcppCommon.h: No such file or directory
compilation terminated.
make: *** [simpleexample.o] Error 1

So what's happening is that the compiler does find RcppArmadillo.h, which looks like the following: 因此,发生的情况是编译器确实找到了RcppArmadillo.h,它看起来像下面的样子:

...
#include <RcppArmadilloForward.h>
#include <Rcpp.h>
#include <RcppArmadilloWrap.h>
#include <RcppArmadilloAs.h>
#include <RcppArmadilloSugar.h>
...

It then goes to RcppArmadillo.h, which in turn looks like the following: 然后转到RcppArmadillo.h,它依次如下所示:

...
#include <RcppCommon.h>
...

and RcppCommon.h is not in /usr/local/linux/lib/R/Current/x86_64/site-library/RcppArmadillo/include . 并且RcppCommon.h不在/usr/local/linux/lib/R/Current/x86_64/site-library/RcppArmadillo/include Instead, it is in /usr/local/linux/lib/R/Current/x86_64/site-library/Rcpp/include . 相反,它位于/usr/local/linux/lib/R/Current/x86_64/site-library/Rcpp/include So it is not surprising that I run into trouble. 因此,我遇到麻烦也就不足为奇了。

Question : How do I set PKG_LIBS and PKG_CXXFLAGS (and whatever else) such that the compiler can find both directories? 问题 :如何设置PKG_LIBS和PKG_CXXFLAGS(及其他),以便编译器可以找到两个目录? If I try pointing PK_CXXFLAGS to the Rcpp directory, then the compiler cannot find RcppArmadillo.h. 如果我尝试将PK_CXXFLAGS指向Rcpp目录,则编译器将找不到RcppArmadillo.h。

Quick ones: 快速的:

  1. Why do you think using the command-line approach is better? 您为什么认为使用命令行方法更好? We have always recommended alternatives. 我们一直推荐替代品。

  2. I do understand and value the merit of doing it from the command-line, and give a full walkthrough in Chapter 2 of the Rcpp book . 我确实了解并重视从命令行执行此操作的优点,并在Rcpp的第二章中进行了完整的演练。 As it is documented there, I am not going to repeat it here. 如此处记录的那样,在这里我不再重复。

  3. You can use the inline package instead. 您可以改用内联包。 There are lots of examples in the package and other places. 包装和其他地方有很多示例。

  4. Better still, you can use Rcpp Attributes as documented in the Rcpp package vignette of the same name. 更好的是,您可以使用同名的Rcpp软件包插图中记录的Rcpp属性。 There are many working examples at the Rcpp Gallery . Rcpp画廊有很多工作示例。

To motivate, here is two-line example computing an outer-product: 为了激发动力,这是计算外部产品的两行示例:

R> cppFunction('arma::mat opg(arma::vec x) { return x*x.t(); }', 
+              depends="RcppArmadillo")
R> opg(1:3)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9
R> 

It can be that easy. 可能就这么简单。 And if you really want to know how to set the appropriate flags, run this in verbose=TRUE mode and it will tell you... 如果您真的想知道如何设置适当的标志,请在verbose=TRUE模式下运行它,它将告诉您...

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

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