首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一系列更新之后,Rcpp无法编译c++代码。

在一系列更新之后,Rcpp无法编译c++代码。
EN

Stack Overflow用户
提问于 2020-09-12 23:06:59
回答 1查看 1.4K关注 0票数 4

我想保持我的操作系统,R,和R包的最新。我无意中踏入深水,升级到OSX 10.15.6,升级到R 4.0.2。目前,Rcpp无法编译c++代码,我认为这会导致某些包(例如glmmTMB)的CRAN安装失败,而且还会导致从源安装失败。我将描述我所做的事情,希望有人能阐明一个解决方案。

在一个粗略的顺序中,我所做的是:

  1. 安装OSX 10.15.6

我不记得日期了,但那是最近的事。我不知道这与问题有多密切的关系,我在安装之前需要Rcpp的其他R包(例如Rcpp)时遇到了问题。

  1. 遇到了一个问题,试图运行一些旧的glmmTMB模型

准确的误差与本期完全相同。我在那条线上遵循了各种解决方案,但没有结果。也许最令人沮丧的是当试图从源代码安装时,然后无法编译一些c++。

这不是我第一次看到类似的错误。当我尝试安装rstan时,也发生了类似的事情,所以我请求在类似的问题下帮助他们进行回购。什么都帮不上忙。

这是在R3.6,所以我认为它可能是值得更新R和glmmTMB。在这样做之前,我想安装Xcode,而不是仅仅安装命令行开发工具,希望安装能够确保所有与clang和c++编译器相关的平方。

  1. 安装Xcode以帮助设置基线c++环境

相对轻松,能够构建和编译相当简单的c++项目。

  1. 已安装R 4.0.2

在r项目站点上,我安装了R-4.0.2.pkg,没有问题,并且具有默认的安装设置。根据R项目工具站点,我在安装Xcode时感到放心,并继续安装GNUFortran8.2安装程序。

  1. 遵循@coatless安装Rcpp指南

发现本指南并遵循每个步骤,但与xcode-select相关的步骤除外。

  1. 从指南中测试Rcpp和RcppArmadillo

最后一个步骤是使用一个简单的helloworld.cpp测试来测试安装。这与此错误(为清楚起见而删减)失败:

代码语言:javascript
复制
> Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp")
In file included from helloworld.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadillo.h:31:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadilloForward.h:26:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/RcppCommon.h:29:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/r/headers.h:67:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [helloworld.o] Error 1
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include   -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include" -I"/Users/alex/Documents/BergenLab/nlp_cancer_metaphor" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include   -fPIC  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -c helloworld.cpp -o helloworld.o
Error in Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp") : 
  Error 1 occurred building shared library.
  1. 试图调试Rcpp,但没有结果

找到了一些与此相关的报告,如由于math.h未找到,包无法从源构建Rcpp不在macOS下,math.h未找到由于math.h未找到源Cpp文件失败更新到c++ Catalina后无法用macOS代码编译R包

这些答案中的任何一种解决办法都没有起到任何作用。给我反馈的唯一解决方案是使用installer安装macOS_SDK_headers_for_macOS_10.14.pkg,它随后给了我installer: Error - the package path specified was invalid (不知道这意味着什么)。

  1. 从源安装glmmTMB失败

上述glmmTMB问题的解决方案之一建议从源代码安装。以下是我的尝试中的错误:

代码语言:javascript
复制
* installing *source* package ‘glmmTMB’ ...
** package ‘glmmTMB’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/TMB/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include' -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include   -fPIC  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -c glmmTMB.cpp -o glmmTMB.o
In file included from glmmTMB.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/TMB/include/TMB.hpp:53:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex:245:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
13 warnings and 20 errors generated.
make: *** [glmmTMB.o] Error 1
ERROR: compilation failed for package ‘glmmTMB’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB’
Warning in install.packages :
  installation of package ‘glmmTMB’ had non-zero exit status

总的来说,我仍然无法运行我的glmmTMB模型(而且我也无法构建rstan )。我的c++编译器必须关闭,但我是一个c++初学者,不知道如何解释我所得到的错误。头文件有些问题,但似乎并不是因为math.h丢失(与大多数其他帖子一样)。这是不言而喻的,但任何帮助都将是非常感谢的!

编辑1

我试图缩小错误范围,因此遵循了@coatless指南中的每一步,就像上面列出的那样,并根据R安装文档。我的准确步骤:

  1. 移除gfortran 8.2。去除~/.Renviron~/.R/Makevars。用unlink()从Rstudio解除这两个文件的链接。
  2. Ran xcode-select --install,返回时使用“使用”软件更新来安装更新。因此,softwareupdate --list估计什么都不需要更新。无论如何,我用sudo rm -rf /Library/Developer/CommandLineTools卸载命令行工具,用xcode-select —install安装命令行工具。到目前为止,一切都如期而至。小型c++程序正在编译中,没有gccclang++的问题。
  3. 根据R手册,从https://mac.r-project.org/libs-4/,我安装了pcre2-10.34-darwin.17-x86_64.tar.gzxz-5.2.4-darwin.17-x86_64.tar.gzreadline-5.2.14-darwin.17-x86_64.tar.gz/usr/local。这里没有什么大问题,尽管我不太确定如何验证这些工作是否如预期的那样。
  4. 在RStudio中重新启动R会话。
  5. 安装了带有RcppArmadillo的Rcpp和install.packages(c(‘Rcpp’, ‘RcppArmadillo’))
  6. 使用注释中提到的幻灯片15的代码创建了一个~/helloworld.cpp
  7. 运行Rcpp::sourceCpp("~/helloworld.cpp")失败时会出现确切的错误,如文章正文中所述。类似地,evalCpp()失败时也有类似的错误。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-14 16:44:47

经过一番深入研究,这个问题最终证明既不是glmmTMB,也不是Rcpp,也不是任何高层次的问题。据我所知:当R使用Rcpp编译c++代码时,某些参数将传递给系统c++编译器。在我的例子中,这个编译器是clang++,因为我刚刚安装了Xcode。c++编译器依赖于一组要按正确顺序包含的c++头文件。似乎我的顺序是不正确的,从而导致了一系列关于“缺少模板定义”的错误。

我研究了如何更改包含的顺序,但没有找到任何简单的解决方案。我知道Rcpp正在为clang++调用一组非常具体的参数,所以我研究了如何解释答案。我查看了clang++的手册,以了解-isysroot-I的含义,并认为它们可能在重新配置系统头文件的编译方式中发挥作用。从下面的一个线程中,我发现了$(R RHOME)/etc/Makeconf Makefile,并将OSX的目录从我认为的命令行工具路径更改为Xcode路径。这似乎解决了这个问题。

我曾经找到这个解决方案的一些线程:更新到c++ Catalina后无法用macOS代码编译R包标头产生错误:全局命名空间中没有名为“signbit”的成员

对我有用的是:

  1. 注意当前头文件搜索

来自clang++ -Wp,-v -E -

代码语言:javascript
复制
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
  1. 注意OSX路径

来自xcrun --show-sdk-path

代码语言:javascript
复制
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
  1. 修改R配置

使用vim $(R RHOME)/etc/Makeconf,将CPPFLAGS标志更改为:

代码语言:javascript
复制
CPPFLAGS = -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include

注意这个SDK目录是如何在步骤2中找到的,这显然是清理SDK头文件包含的位置和顺序。

  1. 从源重新安装glmmTMB

当我试图拟合一个FreeADFunObject模型时,我仍然看到了一个glmmTMB错误,所以我回到了这个GH螺纹,并遵循了第一个建议:从源代码重新安装。由于我需要正确设置c++配置,所以这之前没有工作,我在最后两个步骤中修复了这个配置。

因此,安装install.packages("glmmTMB", type="source")时没有问题,我现在可以构建和安装glmmTMB模型了。

为了子孙后代:

代码语言:javascript
复制
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63865695

复制
相关文章

相似问题

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