Hugh Warden
Hugh Warden

Reputation: 446

Error using rcpp armadillo when making R package

I am trying to get into using C++ code in my R packages. I created a test package using

usethis::create_package("~/Documents/ptest")

and I then set up the C++ necessaries using

usethis::use_rcpp_armadillo("sum_func")

I then created R/test-package.R and pasted in

## usethis namespace: start
#' @useDynLib ptest, .registration = TRUE
## usethis namespace: end
NULL

## usethis namespace: start
#' @importFrom Rcpp sourceCpp
## usethis namespace: end
NULL

In sum_func.cpp I then put

//sum.cpp
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double rcpp_sum(NumericVector v){
    double sum = 0;
    for(int i=0; i<v.length(); ++i){
        sum += v[i];
    }
    return(sum);
}

I then created R/sum__func.R and put in

#' My Sum Function
#'
#' @param vec A vector of values
#'
#' @return The sum
#' @export
#'
#' @examples
#' my_sum(1:10)
my_sum <- function(vec) {
  rcpp_sum(vec)
}

But then when I run devtools::load_all(".") I get

> ℹ Loading ptest Exports from
> /Users/hwarden/Documents/ptest/src/sum_func.cpp:    double
> rcpp_sum(NumericVector v)
> 
> /Users/hwarden/Documents/ptest/src/RcppExports.cpp updated.
> /Users/hwarden/Documents/ptest/R/RcppExports.R updated. Re-compiling
> ptest ─  installing *source* package ‘ptest’ ...    ** using staged
> installation    ** libs    clang++ -mmacosx-version-min=10.13
> -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.1/Resources/library/Rcpp/include'
> -I'/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppArmadillo/include'
> -I/usr/local/include   -fPIC  -Wall -g -O2  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o    clang++ -mmacosx-version-min=10.13 -std=gnu++11
> -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.1/Resources/library/Rcpp/include'
> -I'/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RcppArmadillo/include'
> -I/usr/local/include   -fPIC  -Wall -g -O2  -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c sum_func.cpp -o sum_func.o    clang++ -mmacosx-version-min=10.13 -std=gnu++11 -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 ptest.so RcppExports.o sum_func.o
> -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation    ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin18/8.2.0'    ld:
> warning: directory not found for option '-L/usr/local/gfortran/lib'   
> ld: library not found for -lgfortran    clang: error: linker command
> failed with exit code 1 (use -v to see invocation)    make: ***
> [ptest.so] Error 1    ERROR: compilation failed for package ‘ptest’ ─ 
> removing
> ‘/private/var/folders/n4/lwxlchrn2s7gvc878rw7ln1m0000gn/T/RtmpjJw195/devtools_install_c9e2c847f68/ptest’
> Error in `(function (command = NULL, args = character(),
> error_on_status = TRUE, …`: ! System command 'R' failed
> --- Exit status: 1 stdout & stderr: <printed>
> ---

I get an error but after googling I can't figure out how to solve it.

Upvotes: 0

Views: 273

Answers (1)

Hugh Warden
Hugh Warden

Reputation: 446

Thank you @dirk-eddelbuettel,

This article was the most useful.

I'm not an expert, but for me this worked (or at least got me to my next error).

I ran

brew reinstall gcc

to make sure I had the most up to date gfortran. Then I made a ~/.R/Makevars (as I did not have one already). I then wrote this in there

FC      = usr/local/opt/gcc/bin/gfortran
F77     = /usr/local/opt/gcc/bin/gfortran
FLIBS   = -L/usr/local/opt/gcc/lib

I restarted R and the package then loaded successfully! Many thanks!

Upvotes: 1

Related Questions