编译 Rcpp 包 [英] Compiling the Rcpp package

查看:104
本文介绍了编译 Rcpp 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我的公司正在考虑与 RStudio 一起将 R 引入我们的工作环境.作为安全检查的一部分,我们必须编译一个包中的每个不同的 C/C++ 文件,以扫描可能的安全漏洞.我知道二进制文件是可用的,我真的希望我可以使用它们,但我们必须自己编译.大约 5 年前,我没有 R 经验,也有一点 C++ 经验.我正在尝试编译只是 Rcpp C++ 代码,但我不知道这是否可行.

My company is thinking about bringing R into our work environment in conjunction with RStudio. As part of the security checks, we have to compile each of the different C/C++ files in a package to scan for possible security flaws. I know that the binaries are available, and I really wish that I could use them, but we have to compile ourselves. I have no R experience and a little experience with c++ from about 5 years ago. I'm trying to compile just the Rcpp C++ code and I don't know if this is possible.

我的尝试

我首先尝试使用 VisualStudio 进行编译,但发现它不符合 c99.然后我尝试了 cygwin,它不支持 w_strings.我现在已经安装了 ubuntu,并且在编译时遇到了很多未定义的引用"错误.我知道(或者至少我相信)Rcpp 旨在帮助在 R 中使用 C++.鉴于此,我什至可以将其中一些文件编译为独立的二进制文件吗?

I first tried to compile using VisualStudio and found it's not c99 compliant. I then tried cygwin and it didn't support w_strings. I have now installed ubuntu and am getting a lot of "undefined reference" errors when I compile. I know (or at least I believe) that Rcpp is designed to help use C++ in R. Given this, can I even compile some of these files as stand alone binary?

我在编译 api.cpp 时得到的一些空引用(我认为一个完整的列表不会提供比部分更多的信息,因为它很长):

Some of the null references that I am getting while compiling api.cpp (I don't believe a complete list would provide any more information than the partial as it's quite long):

undefined reference to 'R_NilValue'
undefined reference to 'REprintf'
undefined reference to 'R_FlushConsole'
undefined reference to 'Rf_install'
undefined reference to 'Rf_setAttrib'
undefined reference to 'Rf_mkChar'
undefined reference to 'SET_STRING_ELT'
undefined reference to 'Rf_allocVactor'
undefined reference to 'R_ExternalPtrAddr'
etc...

现在我认为这不起作用,因为我没有按照预期的方式使用 Rcpp 并传递任何值.如果我尝试使用另一个使用 Rcpp 的 R 包,会编译 Rcpp C++ 文件并保留二进制文件吗?除了从 CRAN 中删除二进制文件之外,我还有什么可以做的吗?

Right now I assume that this isn't working because I'm not using Rcpp in the way in which it is intended and passing in any values. If I tried using another R package that uses Rcpp would that compile the Rcpp C++ files and leave the binaries? Is there anything that I can do short of just taking the binaries off of CRAN?

解决方案

这实际上是一个相对简单的解决方案,我在其他任何地方都找不到,尽管大多数人只能下载二进制文件,所以这无关紧要.

This was actually a relatively easy solution that I was unable to find anywhere else, although most people will simply be able to download the binary files so it won't matter.

1) 从 CRAN 下载(不是对我不起作用的 apt-get)

1) Download from CRAN (not apt-get which did not work for me)

2) sudo tar -xvf Rcpp_X.XX.X.tar.gz

2) sudo tar -xvf Rcpp_X.XX.X.tar.gz

3) R CMD 安装 Rcpp

3) R CMD INSTALL Rcpp

这将为您创建二进制 .SO.

This will create the binary .SO for you.

推荐答案

快速解决方案:

  • 拼写为 Rcpp,即大写的 R 后跟小写的 cpp.就像文件扩展名一样.

  • It is spelled Rcpp, that is capital R followed by lowercase cpp. Like the file extension.

您大胆地假设了 Rcpp 可以由 Visual Studio [1] 编译.不能[2].我们已经记录了 ,即自 2008 年底我重新启动 Rcpp 以来的最少时间.

You made the heroic assumption that Rcpp can be compiled by Visual Studio [1]. It can't [2]. And we have documented this literally for years, i.e. at a minumum since late 2008 when I relaunched Rcpp.

参见例如Rcpp FAQ 中的条目 1.3 和 2.9每次下载 Rcpp 都附带.

See e.g. entries 1.3 and 2.9 from the Rcpp FAQ that comes with every download of Rcpp.

Cygwin 同上.R 不在 Cygwin 下构建.因此不要尝试在 Cygwin 下构建 Rcpp.

Ditto for Cygwin. R does not build under Cygwin. Hence don't try building Rcpp under Cygwin.

最后但并非最不重要的一点:不确定您是否仔细查看了 Rcpp 文档,以及您认为它的用途.它不是为您将 R 脚本转换为 Windows 二进制文件的魔法小马.不存在这样的小马.Rcpp 是 R 的扩展包,它使 R 和 C++ 的交换更容易.不多也不少.现在这里有几百个 Rcpp 问题,所以环顾四周以感受一下.

Last but not least: Not sure how closely you have looked at the Rcpp documentation, and what you think it is for. It is not some magic pony which converts R scripts into Windows binaries for you. No such pony exists. Rcpp is an extension package for R which makes R and C++ interchange easier. No more, no less. There are by now a few hundred Rcpp questions here so look around to get a feel.

这几乎肯定也是一个重复的问题,可以结束,但我现在没有时间看.

All this is also almost surely a repeat question that could be closed but I don't have time to look now.

[1] 请注意这里的单数形式,它不是Visual Studios"的复数形式,或者您可能指的是太平洋西北地区公司的编译器和 IDE 以外的东西.

[1] Note the singular here, it is not plural "Visual Studios", or maybe you are referring to something other than the compiler and IDE by a company from the Pacific Northwest.

[2] 除非您是真正的 C++-on-Windows 专家并且愿意或多或少地构建自己的构建系统 [3]

[2] Unless you are a genuine C++-on-Windows expert and willing to more-or-less build your own build system [3]

[3] 这一切主要是因为 R 无法使用 VS 工具链进行构建.$deity 知道人们已经尝试过了.这里的责任在于操作系统/编译器供应商没有遵循标准,这些标准使 R Core 团队能够在 Windows(通过 gcc)、OS X 和基本上所有 Unix 版本(包括所有 Linux 变体)上提供一致的产品.

[3] All this is essentially due to R not being able to build with the VS toolchain. And $deity knows people have tried. The blame here lies on the side of the OS / compiler vendor for not following the standards which enable the R Core team to provide a consistent product on Windows (via gcc), OS X and essentially all flavour of Unix, including all Linux variants.

这篇关于编译 Rcpp 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆