我正在尝试构建一个R包,并且在我的一个函数中使用Rcpp。我可以使用sourceCpp("~/Desktop/trial/src/code.cpp")加载函数,并且使用函数本身没有任何问题,但在尝试添加它和构建包时出现错误。当我运行devtools::load_all()时,我得到以下错误:
System command 'R' failed, exit status: 1, stdout + stderr (last 10 lines):
E> Rcpp::traits::input_parameter< string >::type delim(delimSEXP);
E> ^~~~~~
E> String
E> /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/String.h:49:11: note: 'String' declared here
E> class String {
E> ^
E> 12 errors generated.
E> make: *** [RcppExports.o] Error 1
E> ERROR: compilation failed for package ‘trial’
E> * removing ‘/private/var/folders/52/y1qz8q711pd8cv60r_687c6m0000gn/T/RtmpOO3W0e/devtools_install_aae030821fbc/trial’ 我正在使用的C++代码可以在https://wckdouglas.github.io/2015/05/string-manipulation中找到。为了构建我运行的包
devtools::create("trial")
setwd("~/trial")
usethis::use_rcpp() #At this point I added the cpp file to the src directory
Rcpp::compileAttributes()
devtools::load_all()cpp代码中是否存在与将其添加到R包中的冲突?任何帮助都将不胜感激!谢谢!
发布于 2020-12-03 18:50:24
在Linux上使用gcc时,我得到了不同的错误。但最重要的是,编译器报告的第一个错误很能说明问题:
RcppExports.cpp:9:1: error: ‘stringList’ does not name a type
9 | stringList string_split(stringList x, string sep, int start, int frag);
| ^~~~~~~~~~现在这是有意义的,因为stringList是原始代码中的typedef,不会自动传播到RcppExports.cpp。可以在Rcpp属性vignette的第2.5节中找到解决方案:创建一个文件src/trial_types.h (还有其他可能的名称和位置,请参阅文档):
#include <string>
#include <vector>
using namespace std;
typedef vector<string> stringList;
typedef vector<int> numList;并将C++代码中的typedef替换为#include "trial_types.h"。
顺便说一句,在包代码中我不会使用using namespace std;和using namespace Rcpp;。
https://stackoverflow.com/questions/64977047
复制相似问题