在 Windows 上从 C++ 调用 R 函数

2024-04-03

我正在尝试在 Windows 上从 C++ 调用 R 函数。我正在使用 MinGW 来编译程序,但它在编译时抛出错误。代码(取自Dirk)和编译错误如下:

#include <iostream>
using namespace std;
#include "RInside.h"  // for the embedded R via RInside


Rcpp::NumericMatrix createMatrix(const int n) {
  Rcpp::NumericMatrix M(n,n);
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      M(i,j) = i*10+j;
    }
  }
  return(M);
}

int main(int argc, char *argv[]) {
  const int mdim = 4;                         // let the matrices be 4 by 4
  SEXP ans;


  RInside R(argc, argv);                      // create an embedded R instance

  Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix
  R["M"] = M;                                 // assign C++ matrix M to R's 'M' var

  std::string evalstr = "\
            cat('Running ls()\n'); print(ls());                    \
            cat('Showing M\n'); print(M);                          \
            cat('Showing colSums()\n'); Z <- colSums(M); print(Z); \
            Z";                     // returns Z

  ans = R.parseEval(evalstr);                 // eval the init string -- Z is now in ans

  Rcpp::NumericVector v(ans);                 // convert SEXP ans to a vector of doubles
  for (int i=0; i< v.size(); i++) {           // show the result
    std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
  }

  return 0;
}

Compile:

g++ -I "C:\ProgramFiles\R\R-2.14.0\library\RInside\include" -I "C:\Progra
mFiles\R\R-2.14.0\library\Rcpp\include" -I "C:\ProgramFiles\R\R-2.14.0\include"
RFunctions.cpp -o sh1.exe

Error:

C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x19a): und
efined reference to `RInside::RInside(int, char const* const*, bool)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x1ee): und
efined reference to `RInside::operator[](std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x26d): und
efined reference to `RInside::parseEval(std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x35b): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x3e1): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0x8): undefined reference to `vtable for Rcp
p::RObject'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0xd): undefined reference to `_imp__R_NilVal
ue'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN7RInside
5ProxyD1Ev[RInside::Proxy::~Proxy()]+0xd): undefined reference to `Rcpp::RObject
::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED2Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED1Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x57): undefined ref
erence to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x66): undefined ref
erence to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ma
trixILi14EEC1ERKiS3_[Rcpp::Matrix<14>::Matrix(int const&, int const&)]+0x2c): un
defined reference to `Rcpp::Dimension::Dimension(unsigned int const&, unsigned i
nt const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp6V
ectorILi14EE4sizeEv[Rcpp::Vector<14>::size() const]+0x10): undefined reference t
o `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0xd): undefined re
ference to `TYPEOF'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0x1d): undefined r
eference to `SEXPREC* Rcpp::internal::r_true_cast<14>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x46): undefined reference to `Rcpp::Dimension::prod() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x56): undefined reference to `Rf_allocVector'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x67): undefined reference to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x7d): undefined reference to `Rcpp::Dimension::size() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0xc9): undefined reference to `Rcpp::RObject::attr(std::string const&) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x13b): undefined reference to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp11
Environment6assignINS_6MatrixILi14EEEEEbRKSsRKT_[bool Rcpp::Environment::assign<
Rcpp::Matrix<14> >(std::basic_string<char, std::char_traits<char>, std::allocato
r<char> > const&, Rcpp::Matrix<14> const&) const]+0x23): undefined reference to
`Rcpp::Environment::assign(std::string const&, SEXPREC*) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bject14AttributeProxyaSINS_9DimensionEEERS1_RKT_[Rcpp::RObject::AttributeProxy&
Rcpp::RObject::AttributeProxy::operator=<Rcpp::Dimension>(Rcpp::Dimension const&
)]+0x1c): undefined reference to `Rcpp::RObject::AttributeProxy::set(SEXPREC*) c
onst'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0xd): undefined reference to `double* Rcpp::internal::r_vector_start<1
4, double>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0x23): undefined reference to `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal21wrap_dispatch_unknownINS_9DimensionEEEP7SEXPRECRKT_NS_6traits17integral_
constantIbLb1EEE[SEXPREC* Rcpp::internal::wrap_dispatch_unknown<Rcpp::Dimension>
(Rcpp::Dimension const&, Rcpp::traits::integral_constant<bool, true>)]+0xd): und
efined reference to `Rcpp::Dimension::operator SEXPREC*() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6tr
aits14r_vector_cacheILi14EE6updateERKNS_6VectorILi14EEE[Rcpp::traits::r_vector_c
ache<14>::update(Rcpp::Vector<14> const&)]+0x15): undefined reference to `double
* Rcpp::internal::r_vector_start<14, double>(SEXPREC*)'
collect2: ld returned 1 exit status

我缺少什么想法吗?


吉姆之前的回答是正确的,但还有更多。

使用时RInside http://dirk.eddelbuettel.com/code/rinside.html,你还需要

  1. 包含并链接到Rcpp http://dirk.eddelbuettel.com/code/rcpp.html (which RInside http://dirk.eddelbuettel.com/code/rinside.html取决于)a

  2. 包含并与 R 链接(两者都依赖),当然

  3. RInside http://dirk.eddelbuettel.com/code/rinside.html自己的图书馆

最简单的方法是使用 Makefileexamples/standard目录 --- 鉴于您复制了示例之一的代码,您还应该复制构建指令。

最后,这是您最大的问题:RInside 应用程序当前无法在 Windows 上运行,这在RInside http://dirk.eddelbuettel.com/code/rinside.html页。它将构建,但启动时出现段错误。调试帮助将不胜感激,这适用于 OS X 和 Linux。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Windows 上从 C++ 调用 R 函数 的相关文章

  • 用于检查类是否具有运算符/成员的 C++ 类型特征[重复]

    这个问题在这里已经有答案了 可能的重复 是否可以编写一个 C 模板来检查函数是否存在 https stackoverflow com questions 257288 is it possible to write a c template
  • BitTorrent 追踪器宣布问题

    我花了一点业余时间编写 BitTorrent 客户端 主要是出于好奇 但部分是出于提高我的 C 技能的愿望 我一直在使用理论维基 http wiki theory org BitTorrentSpecification作为我的向导 我已经建
  • OleDbDataAdapter 未填充所有行

    嘿 我正在使用 DataAdapter 读取 Excel 文件并用该数据填充数据表 这是我的查询和连接字符串 private string Query SELECT FROM Sheet1 private string ConnectStr
  • 关于 C++ 转换:参数 1 从“[some_class]”到“[some_class]&”没有已知的转换

    我正在研究 C 并且遇到了一个错误 我不知道确切的原因 我已经找到了解决方案 但仍然想知道原因 class Base public void something Base b int main Base b b something Base
  • 将 VSIX 功能添加到 C# 类库

    我有一个现有的单文件生成器 位于 C 类库中 如何将 VSIX 项目级功能添加到此项目 最终目标是编译我的类库项目并获得 VSIX 我实际上是在回答我自己的问题 这与Visual Studio 2017 中的单文件生成器更改 https s
  • 在 ASP.NET 5 中使用 DI 调用构造函数时解决依赖关系

    Web 上似乎充斥着如何在 ASP NET 5 中使用 DI 的示例 但没有一个示例显示如何调用构造函数并解决依赖关系 以下只是众多案例之一 http social technet microsoft com wiki contents a
  • C#中如何移动PictureBox?

    我已经使用此代码来移动图片框pictureBox MouseMove event pictureBox Location new System Drawing Point e Location 但是当我尝试执行时 图片框闪烁并且无法识别确切
  • 如何绘制具有显着性水平的箱线图?

    前段时间问了一个关于绘制箱线图的问题Link1 https stackoverflow com questions 14604439 plot multiple boxplot in one graph 我有一些包含 3 个不同组 或标签
  • 使用 Bearer Token 访问 IdentityServer4 上受保护的 API

    我试图寻找此问题的解决方案 但尚未找到正确的搜索文本 我的问题是 如何配置我的 IdentityServer 以便它也可以接受 授权带有 BearerTokens 的 Api 请求 我已经配置并运行了 IdentityServer4 我还在
  • 如何设计以 char* 指针作为类成员变量的类?

    首先我想介绍一下我的情况 我写了一些类 将 char 指针作为私有类成员 而且这个项目有 GUI 所以当单击按钮时 某些函数可能会执行多次 这些类是设计的单班在项目中 但是其中的某些函数可以执行多次 然后我发现我的项目存在内存泄漏 所以我想
  • 转发声明和包含

    在使用库时 无论是我自己的还是外部的 都有很多带有前向声明的类 根据情况 相同的类也包含在内 当我使用某个类时 我需要知道该类使用的某些对象是前向声明的还是 include d 原因是我想知道是否应该包含两个标题还是只包含一个标题 现在我知
  • 如何在 C 中调用采用匿名结构的函数?

    如何在 C 中调用采用匿名结构的函数 比如这个函数 void func struct int x p printf i n p x 当提供原型的函数声明在范围内时 调用该函数的参数必须具有与原型中声明的类型兼容的类型 其中 兼容 具有标准定
  • 覆盖子类中的字段或属性

    我有一个抽象基类 我想声明一个字段或属性 该字段或属性在从该父类继承的每个类中具有不同的值 我想在基类中定义它 以便我可以在基类方法中引用它 例如覆盖 ToString 来表示 此对象的类型为 property field 我有三种方法可以
  • 链接器错误:已定义

    我尝试在 Microsoft Visual Studio 2012 中编译我的 Visual C 项目 使用 MFC 但出现以下错误 error LNK2005 void cdecl operator new unsigned int 2
  • 向现有 TCP 和 UDP 代码添加 SSL 支持?

    这是我的问题 现在我有一个 Linux 服务器应用程序 使用 C gcc 编写 它与 Windows C 客户端应用程序 Visual Studio 9 Qt 4 5 进行通信 是什么very在不完全破坏现有协议的情况下向双方添加 SSL
  • 如何从两个不同的项目中获取文件夹的相对路径

    我有两个项目和一个共享库 用于从此文件夹加载图像 C MainProject Project1 Images 项目1的文件夹 C MainProject Project1 Files Bin x86 Debug 其中有project1 ex
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • 基于 OpenCV 边缘的物体检测 C++

    我有一个应用程序 我必须检测场景中某些项目的存在 这些项目可以旋转并稍微缩放 更大或更小 我尝试过使用关键点检测器 但它们不够快且不够准确 因此 我决定首先使用 Canny 或更快的边缘检测算法 检测模板和搜索区域中的边缘 然后匹配边缘以查
  • 如何将plot中的单变量列表图表转换为ggplot2格式?

    我正在搜索 但仍然找不到一个非常简单的问题的答案 我们如何使用 R 中的 ggplot2 生成一个变量的简单线图 我正在分析时间序列数据 并且想要对图表进行更复杂的操作 我认为如果我使用 ggplot2 代替会更好plot It works
  • C++ 标准是否指定了编译器的 STL 实现细节?

    在写答案时this https stackoverflow com questions 30909296 can you put a pimpl class inside a vector我遇到了一个有趣的情况 这个问题演示了这样一种情况

随机推荐

  • 如何合并 Heroku 上具有相同架构的两个数据库?

    我在 Heroku 上创建了两个本质上相同的应用程序 他们开始时有所不同 因为我正在测试上传到 Heroku 并在调整时遇到一些挑战 但现在一切似乎都正常 但两者都有我想合并的数据 由于它们运行相同的 git 存储库 因此代码是相同的 迁移
  • 会话值随机消失/返回[重复]

    这个问题在这里已经有答案了 我们使用 Symfony 框架 V4 1 3 我们的会话值 看似 随机消失 重新出现 有时在几秒钟内它就消失 返回 有时它在几分钟后返回 消失 我们只是转储会话值 没有其他任何事情 到目前为止 它看起来是 100
  • 如何使用 ruby​​-on-rails 固定装置设置不属于 Rails 应用程序数据库的(外部)数据?

    根据我的要求 我创建了用于查询某些数据的外部数据库 与 Rails 应用程序使用的数据库不同 的模型 我正在尝试围绕这些模型编写测试 并希望将 样本测试数据 与实际测试分开 我以为我可以将数据放入 yml 文件并将其加载到哈希中 但它确实有
  • 给定一本字典和一个字母列表,找到可以用这些字母构建的所有有效单词

    暴力方式可以在 O n 内解决问题 基本上计算所有排列并在字典中检查结果 我正在寻找提高复杂性的方法 我可以考虑用字典构建一棵树 但仍然检查所有字母排列的时间复杂度为 O n 有更好的方法来解决这个问题吗 信件可以有重复项 该函数的 api
  • 通过代码在 Outlook 中发送/接收

    如果我使用 Excel VBA 创建 Outlook 2010 对象 Sub CreateOL On Error Resume Next Set myOlApp GetObject Outlook Application If Err Nu
  • 由于 .getContext() 未实现,单元测试引发错误

    我正在使用 Jest 为使用画布元素的组件编写测试 当我运行如下所示的测试时 我不断收到错误 Error Not implemented HTMLCanvasElement prototype getContext without inst
  • 将 System.out.println 记录到单个应用程序的日志文件

    我们在 tomcat 上有多个应用程序 它们使用 System out println 语句记录到 catalina out 有一个应用程序会创建大量日志语句 因此我想将这些应用程序输出记录到单独的日志文件中 我创建了一个 log4j xm
  • GridLayout 列宽

    我的目录中有 2 列GridLayout 我想要做的是让这些列各自占据屏幕宽度的一半 然后让它的子内容填充它们自己的单元格宽度 高度 我尝试让孩子们fill parent但这只会导致第一个占据整个布局 而且 GridLayout 似乎不支持
  • Ruby 类方法与特征类中的方法

    类方法和该类的特征类 或元类 中的方法只是定义一件事的两种方法吗 否则的话 有什么区别呢 class X class method def self a a end eigenclass method class lt lt self de
  • 我应该使用新的 HTML5 语义元素吗? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在升级我的网站 这涉及到使用新的 HTML5 语义元素的决定
  • 使用PHP和Javascript通过CORS实现站点之间的跨域通信

    经过几天的网络浏览并尝试了所有可能的方法后 我开始意识到我的主机服务器存在问题 它被我的主机服务器禁用 所以 我想把我的代码发布到 Stackoverflow 上 这样 SO 上的其他人就不会像我一样浪费时间了 该代码片段在 IE Safa
  • linux下获取硬盘剩余空间

    在 bash 脚本中 我需要获取总磁盘大小和整个磁盘当前使用的大小 我知道我可以使用以下命令获取总磁盘大小 而无需成为 root 用户 cat sys block sda size 该命令将输出设备 SDA 上的块数 将其乘以 512 您将
  • iOS——是否可以强制 UILabel 子类对象成为第一响应者?

    有什么办法可以做到这一点吗 我尝试将以下内容放入子类中 BOOL canBecomeFirstResponder return YES 但是当我向对象发送成为FirstResponder消息时 它仍然没有成为第一响应者 对的 这是可能的 你
  • 使用初始值设定项列表创建单项向量

    我有一个函数func这是超载采取任一std vector
  • MaterialDatePicker 仅选择 2 个日期之间的日期

    如何向 MaterialDatePicker 添加约束以强制用户仅选择 2 个日期之间的日期 val constraintBuilder CalendarConstraints Builder val materialRangeBuilde
  • 命令 test -x 在 ubuntu 中做什么?

    什么是test x从 etc cron daily logrotate 做什么 test x usr sbin logrotate exit 0 usr sbin logrotate etc logrotate conf 它是否测试是否已经
  • 如何加快 Julia 脚本的启动时间

    我编写了一个脚本来在 Julia 中绘制每日数据 但我发现它运行缓慢 似乎没有理想的方法来加速它 例如 foo jl bin bash exec julia O0 compile min BASH SOURCE 0 using Unicod
  • 异步 bash 脚本

    我见过一些例子 但无法将它们应用于我的情况 我有一个调用长时间运行的命令的脚本 但我想定期 例如每 1 秒 获取该调用的状态 例如 bin bash curl localhost 9200 my index forcemerge max n
  • FromBody不绑定字符串参数

    我有一个类似的问题ASP NET MVC 4 RC Web API 参数绑定问题 https stackoverflow com questions 11158617 asp net mvc 4 rc web api parameter b
  • 在 Windows 上从 C++ 调用 R 函数

    我正在尝试在 Windows 上从 C 调用 R 函数 我正在使用 MinGW 来编译程序 但它在编译时抛出错误 代码 取自Dirk 和编译错误如下 include