R中由于文件路径指定错误导致的c++/g++包编译问题的解决方案

问题描述 投票:0回答:1

当我尝试安装从源代码(特别是 rstan)编译的软件包时,我在 R/RStudio 中遇到了两次问题,因为 R 始终在错误的位置查找 g++ 编译器。这篇文章的目的是为其他人记录问题。我第一次在 R 4.3.1 中遇到这些问题,第二次在 R 4.3.2 中遇到这些问题,两者都使用 rtools43。我使用的是 Windows 10 64 位并使用 RStudio 2023.12.0.369 OCean Storm。

在这两种情况下,解决方案都是转到

C:\Program Files\R\R-4.3.2\etc\x64
中的 Makeconf 文件,并手动将 BINPREF 变量从
BINPREF ?=
修改到最新安装 rbuildtools 的适当位置。在撰写本文时,这是
C:\rtools43\x86_64-w64-mingw32.static.posix\bin
。这个解决方案是通过这个答案确定的https://stackoverflow.com/a/46619260/22555498但每次找到它都是曲折的。

第一个表现形式记录在此处:https://stackoverflow.com/a/77101426/22555498

第二个表现是在我尝试从 4.3.1 更新到 R 4.3.2 时开始的,最初是 stan 模型通过 R 编译失败。我尝试全新安装 rstan,但未能解决问题,所以我做了完全重新安装 R、RStudio、rtools 和 rstan。在这种情况下,rstan 的编译将失败,特别是在加载库的阶段,因为 R 不断寻找

C:/RBuildTools/3.5/mingw_64/bin/g++
以便使用 g++ 从源代码进行编译,而正确的路线是通过
C:\rtools43\x86_64-w64-mingw32.static.posix\bin
。错误输出看起来像

* installing *source* package 'rstan' ...
** package 'rstan' successfully unpacked and MD5 sums checked
** using staged installation
** libs
C:/RBuildTools/3.5/mingw_64/bin/g++  -std=gnu++17 -I"C:/PROGRA~1/R/R-43~1.2/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"C:/Users/there/AppData/Local/R/win-library/4.3/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_REENTRANT -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -D_HAS_AUTO_PTR_ETC=0 -DRCPP_PARALLEL_USE_TBB=1 -I'C:/Users/there/AppData/Local/R/win-library/4.3/Rcpp/include' -I'C:/Users/there/AppData/Local/R/win-library/4.3/RcppEigen/include' -I'C:/Users/there/AppData/Local/R/win-library/4.3/BH/include' -I'C:/Users/there/AppData/Local/R/win-library/4.3/StanHeaders/include' -I'C:/Users/there/AppData/Local/R/win-library/4.3/RcppParallel/include'   -I"C:/rtools43/x86_64-w64-mingw32.static.posix/include"  -DRCPP_PARALLEL_USE_TBB=1 -DSTRICT_R_HEADERS   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c Module.cpp -o Module.o
/bin/sh: line 1: C:/RBuildTools/3.5/mingw_64/bin/g++: No such file or directory
make: *** [C:/PROGRA~1/R/R-43~1.2/etc/x64/Makeconf:272: Module.o] Error 127
ERROR: compilation failed for package 'rstan'
* removing 'C:/Users/there/AppData/Local/R/win-library/4.3/rstan'
Warning in install.packages :
  installation of package ‘rstan’ had non-zero exit status

上述错误来自于通过将 Makeconf 文件恢复为具有

BINPREF ?=
来重新创建错误,并且可能与最初看到的内容不完全匹配,但看起来与我记得的一样。

通过

Sys.setenv()
函数设置 PATH 和 BINPREF 等解决方案未能纠正由于 Makeconf 文件中的硬编码而导致的问题。运行
Sys.getenv("PATH")
指向存在且正确的
c:\\rtools43
,但缺少
C:\\rtools43\\x86_64-w64-mingw32.static.posix\\bin
,我不确定这是问题所在,因为我在任何地方都找不到答案。
Sys.which("make")
指向
C:\\rtools43\\usr\\bin\\make.exe
,这也是正确的。

C:/RBuildTools/3.5/mingw_64/bin/g++
相关的搜索没有找到有用的结果,因为自从我第一次遇到问题以来,文件结构似乎已经发生了变化,现在指向
C:/rtools43
。类似的问题指向
C:/RBuildTools/4.3/mingw_64/bin/g++
,这不是我这次安装时的默认安装位置。

r g++ rtools
1个回答
0
投票

我认为解决 gcc/g++ 依赖项问题的最直接方法是更新 Rtools 安装(目前版本为 4.3,而 R 为 4.4.0),并在 PATH 环境变量中手动创建一个等于“C”的条目: tools43\x86_64-w64-mingw32.static.posix in”(使用默认安装路径),如您的问题中正确所述。

以前,Rtools 安装程序会自动为您编辑 PATH 环境变量,但现在不再这样做了。另请注意,gcc/g++ 编译器的相对路径从“rtools43\usr in”更改为“rtools43\x86_64-w64-mingw32.static.posix in”,因此即使您的 PC 上已经安装了 Rtools,并且只是将其更新到 4.3,您可能仍然需要手动编辑 PATH 变量。

© www.soinside.com 2019 - 2024. All rights reserved.