经过一系列更新后,Rcpp 无法编译 C++ 代码

Ale*_*x L 5 macos r clang

我希望使我的操作系统、R 和 R 软件包保持最新。我不知不觉地升级到 OSX 10.15.6 并升级到 R 4.0.2,陷入了深渊。目前,Rcpp无法编译 C++ 代码,我认为这会导致某些软件包的 CRAN 安装失败(例如glmmTMB),并且还会导致从源安装失败。我将描述我所做的事情,希望有人能够阐明解决方案。

\n
\n

粗略地说,这就是我所做的:

\n
    \n
  1. 已安装 OSX 10.15.6
  2. \n
\n

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

\n
    \n
  1. glmmTMB尝试运行一些旧模型时遇到问题
  2. \n
\n

确切的错误与此问题相同。我遵循了该线程上的各种解决方案,但无济于事。也许最令人沮丧的是当尝试从源代码安装时,它无法编译一些 C++。

\n

这不是我第一次看到类似的错误。当我尝试安装时发生了类似的情况,因此我在类似问题rstan下向他们的存储库寻求一些帮助。没有任何帮助。

\n

这是 R 3.6 上的,所以我想也许值得更新 R 和glmmTMB. 在执行此操作之前,我想安装 Xcode,而不仅仅是安装命令行开发工具,希望安装后能够确保我能将与 clang 和 c++ 编译器相关的所有内容都解决掉。

\n
    \n
  1. 安装了 Xcode 以帮助设置基线 C++ 环境
  2. \n
\n

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

\n
    \n
  1. 已安装 R 4.0.2
  2. \n
\n

我从 r-project 站点安装了 R-4.0.2.pkg,没有出现任何问题,并且使用默认安装设置。根据R 项目工具网站,我对安装 Xcode 感到放心,并继续安装 GNU Fortran 8.2 安装程序。

\n
    \n
  1. 遵循 @coatless\'s 指南安装 Rcpp
  2. \n
\n

发现了本指南并遵循了每个步骤(与 相关的步骤除外)xcode-select

\n
    \n
  1. 从指南测试 Rcpp 和 RcppArmadillo
  2. \n
\n

最后步骤之一是通过简单的helloworld.cpp测试来测试安装。由于此错误而失败(为清楚起见进行了删节):

\n
> Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp")\nIn file included from helloworld.cpp:1:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadillo.h:31:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadilloForward.h:26:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/RcppCommon.h:29:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/r/headers.h:67:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:\n/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\nusing ::signbit;\n      ~~^\nfatal error: too many errors emitted, stopping now [-ferror-limit=]\n20 errors generated.\nmake: *** [helloworld.o] Error 1\nclang++ -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\nError in Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp") : \n  Error 1 occurred building shared library.\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 尝试调试Rcpp无果
  2. \n
\n

发现了一些有些相关的报告,例如Packages Cannot build from source due to math.h not findRcpp does\t under macOS、 math.h not findSource Cpp file failed due to math.h not findCannot compile更新到 macOS Catalina 后,R 会使用 C++ 代码进行打包

\n

这些答案中的解决方案都没有任何帮助。给我反馈的唯一解决方案是使用installer安装macOS_SDK_headers_for_macOS_10.14.pkg,随后给了我installer: Error - the package path specified was invalid(不知道这有什么影响)。

\n
    \n
  1. 从源安装glmmTMB失败
  2. \n
\n

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

\n
* installing *source* package \xe2\x80\x98glmmTMB\xe2\x80\x99 ...\n** package \xe2\x80\x98glmmTMB\xe2\x80\x99 successfully unpacked and MD5 sums checked\n** using staged installation\n** libs\nclang++ -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\nIn file included from glmmTMB.cpp:1:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/TMB/include/TMB.hpp:53:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:\nIn file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:\nIn file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex:245:\n/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\nusing ::signbit;\n      ~~^\nfatal error: too many errors emitted, stopping now [-ferror-limit=]\n13 warnings and 20 errors generated.\nmake: *** [glmmTMB.o] Error 1\nERROR: compilation failed for package \xe2\x80\x98glmmTMB\xe2\x80\x99\n* removing \xe2\x80\x98/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB\xe2\x80\x99\n* restoring previous \xe2\x80\x98/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB\xe2\x80\x99\nWarning in install.packages :\n  installation of package \xe2\x80\x98glmmTMB\xe2\x80\x99 had non-zero exit status\n
Run Code Online (Sandbox Code Playgroud)\n
\n

总的来说,我仍然无法运行我的模型(并且我认为也glmmTMB无法构建)。rstan我的 c++ 编译器的某些部分必须关闭,但我是 c++ 初学者,不确定如何解释我遇到的错误。头文件有问题,但看起来可能不是因为math.h丢失(就像大多数其他帖子一样)。不言而喻,但任何帮助将不胜感激!

\n
\n

编辑1

\n

我试图缩小错误范围,因此按照 @coatless 指南中的每个步骤进行操作,完全按照其中列出的内容并根据 R 安装文档进行操作。我的具体步骤:

\n
    \n
  1. 已删除gfortran 8.2。删除了~/.Renviron~/.R/Makevars。使用 .Rstudio 取消这两个文件的链接unlink()

    \n
  2. \n
  3. Ran xcode-select --install,返回“使用“软件更新”安装更新”。因此,softwareupdate --list估计没有什么需要更新的。无论如何,我卸载了命令行工具并sudo rm -rf /Library/Developer/CommandLineTools使用xcode-select \xe2\x80\x94install. 到目前为止,一切都按预期进行。gcc和 都可以毫无问题地编译小型 C++ 程序clang++

    \n
  4. \n
  5. 根据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。这里没有什么大问题,尽管我不太确定如何验证这些是否按预期工作。

    \n
  6. \n
  7. 在 RStudio 中重新启动 R 会话。

    \n
  8. \n
  9. 安装了 Rcpp 和 RcppArmadilloinstall.packages(c(\xe2\x80\x98Rcpp\xe2\x80\x99, \xe2\x80\x98RcppArmadillo\xe2\x80\x99))

    \n
  10. \n
  11. ~/helloworld.cpp使用评论中提到的幻灯片 15 中的代码创建了一个。

    \n
  12. \n
  13. 运行Rcpp::sourceCpp("~/helloworld.cpp")失败,并出现帖子正文中提到的确切错误。同样evalCpp()失败并出现类似错误。

    \n
  14. \n
\n

Ale*_*x L 3

经过一番挖掘后,发现这个问题既不是glmmTMBRcpp也不是任何高级别的问题。据我了解:当R用来Rcpp编译c++代码时,一组特定的参数被传递给系统c++编译器。就我而言,这个编译器是 clang++,因为我刚刚安装了 Xcode。C++ 编译器依赖于一组按正确顺序包含的C++ 头文件。似乎我的顺序不正确,从而导致了一系列有关“缺少模板定义”的错误。

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

我曾经找到此解决方案的一些线程:Cannot compile R packages with c++ code after update to macOS Catalina and Catalina C++:Using <cmath> headers yield error: no member named 'signbit' in the global namespace

对我有用的:

  1. 注意当前头文件搜索

clang++ -Wp,-v -E -

#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)
Run Code Online (Sandbox Code Playgroud)
  1. 记下 OSX SDK 路径

xcrun --show-sdk-path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
Run Code Online (Sandbox Code Playgroud)
  1. 修改R配置

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

CPPFLAGS = -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include
Run Code Online (Sandbox Code Playgroud)

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

  1. glmmTMB从源重新安装

FreeADFunObject我在尝试拟合模型时仍然看到错误glmmTMB,所以我回到这个 GH 线程并遵循第一个建议:从源代码重新安装。这之前不起作用,因为我需要正确设置的 C++ 配置,我在最后两个步骤中修复了该配置。

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


为了后代:

> 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
Run Code Online (Sandbox Code Playgroud)