无法使用Rcpp编译R包



我正在尝试构建一个R包,并且在我的一个函数中使用Rcpp。我可以用sourceCpp("~/Desktop/tril/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包相冲突的内容?任何帮助都将不胜感激!谢谢

在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属性小插曲的第2.5节中找到:创建一个文件src/trial_types.h(还有其他可能的名称和位置,请参阅文档(:

#include <string>
#include <vector>
using namespace std;
typedef vector<string> stringList;
typedef vector<int> numList;

并将C++代码中的typedefs替换为#include "trial_types.h"

顺便说一句,在程序包代码中,我不会使用using namespace std;using namespace Rcpp;

最新更新