首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Rcpp编译R包

无法使用Rcpp编译R包
EN

Stack Overflow用户
提问于 2020-11-24 05:53:14
回答 1查看 172关注 0票数 0

我正在尝试构建一个R包,并且在我的一个函数中使用Rcpp。我可以使用sourceCpp("~/Desktop/trial/src/code.cpp")加载函数,并且使用函数本身没有任何问题,但在尝试添加它和构建包时出现错误。当我运行devtools::load_all()时,我得到以下错误:

代码语言:javascript
复制
  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中找到。为了构建我运行的包

代码语言:javascript
复制
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包中的冲突?任何帮助都将不胜感激!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-12-03 18:50:24

在Linux上使用gcc时,我得到了不同的错误。但最重要的是,编译器报告的第一个错误很能说明问题:

代码语言:javascript
复制
   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 (还有其他可能的名称和位置,请参阅文档):

代码语言:javascript
复制
#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;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64977047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档