简体   繁体   English

制作 R package 时使用 rcpp armadillo 时出错

[英]Error using rcpp armadillo when making R package

I am trying to get into using C++ code in my R packages.我正在尝试在我的 R 包中使用 C++ 代码。 I created a test package using我创建了一个测试 package 使用

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

and I then set up the C++ necessaries using然后我使用 C++

usethis::use_rcpp_armadillo("sum_func")

I then created R/test-package.R and pasted in然后我创建了R/test-package.R并粘贴在

## 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 putsum_func.cpp然后我把

//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然后我创建了R/sum__func.R并放入

#' 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但是当我运行devtools::load_all(".")我得到

> ℹ 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.我收到一个错误,但谷歌搜索后我不知道如何解决它。

Thank you @dirk-eddelbuettel,谢谢@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 .确保我拥有最新的gfortran Then I made a ~/.R/Makevars (as I did not have one already).然后我做了一个~/.R/Makevars (因为我还没有)。 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!我重新启动了 R 和 package 然后加载成功! Many thanks!非常感谢!

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

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