Search code examples
c++rrcppr-packagercpparmadillo

NULL value passed as symbol address with Rcpp functions


I apologize if this question is quite silly as I am new to Rcpp. I have built a package from Rcpp but when I installed it, I found all functions related to Rcpp will return the following type of error message: Error in .Call(<pointer: 0x0>, M) : NULL value passed as symbol address

Here is an example:

getEigenValue.cpp

#include <RcppArmadillo.h>
#include <Rcpp.h>

// [[Rcpp::depends("RcppArmadillo")]]
// [[Rcpp::export]]

arma::vec getEigenValue(arma::mat M) {
  arma::vec eigval;
  arma::mat eigvec;
  
  eig_sym(eigval, eigvec, M);
  return eigval;
};

RcppExports.cpp

// getEigenValue
arma::vec getEigenValue(arma::mat M);
RcppExport SEXP _mypackage_getEigenValue(SEXP MSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< arma::mat >::type M(MSEXP);
    rcpp_result_gen = Rcpp::wrap(getEigenValue(M));
    return rcpp_result_gen;
END_RCPP
}

RcppExports.R

getEigenValue <- function(M) {
    .Call(`_mypackage_getEigenValue`, M)
}

DESCRIPTION

Depends: Rcpp
LinkingTo: Rcpp, RcppArmadillo

All Rcpp functions have such an error but they would be fine if I just use sourceCpp("./src/getEigenValue.cpp") instead of using the package.

Thank you a lot!


Solution

  • Something in your package structure may be foobar'ed here, and we cannot tell what. @Joseph's comment about the namespace is good too.

    Here are a few general comments:

    1. Don't use both #include files, use only #include <RcppArmadillo.h>

    2. In a package, don't use [[Rcpp::depends("RcppArmadillo")]]

    3. Start with a clean slate:

      3.1 Run RcppArmadillo.package.skeleton("mypackage")
      3.2 Copy your file into src/
      3.3 Re-run compileAttributes()
      3.4 Build the package

    That process works for me:

    > library("mypackage")
    > getEigenValue(cbind(c(1,-1), c(-1,1)))  # same as ?eigen
         [,1]
    [1,]    0
    [2,]    2
    > 
    

    I put all relevant files here.