最近在使用R studio的时候遇到一个奇怪的bug,想装某些需要gcc编译的R包却装不上,google一通后莫名其妙地修复了这个bug,这里记录一下,也想请懂原理的老哥们指点一下。
R version: R 4.1.2
OS: windows 10 20H2
bug示例
例如我想安装cli这个R包,需要对binary source code进行编译,运行install.packages之后显示如下报错
>install.packages("cli")
有二进制版本的,但源代码版本是后来的:
binary source needs_compilation
cli 3.1.1 3.2.0 TRUE
安装源码包‘cli’
trying URL 'https://cran.rstudio.com/src/contrib/cli_3.2.0.tar.gz'
Content type 'application/x-gzip' length 493192 bytes (481 KB)
downloaded 481 KB
* installing *source* package 'cli' ...
** package 'cli' successfully unpacked and MD5 sums checked
** using staged installation
Warning in parse(con, encoding = "UTF-8") :
argument encoding="UTF-8" is ignored in MBCS locales
** libs
*** arch - i386
"c:/rtools40/mingw32/bin/"gcc -I"E:/R-4.1.2/include" -DNDEBUG -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c ansi.c -o ansi.o
sh: c:/rtools40/mingw32/bin/gcc: No such file or directory
make: *** [E:/R-4.1.2/etc/i386/Makeconf:238: ansi.o] Error 127
ERROR: compilation failed for package 'cli'
* removing 'E:/R-4.1.2/library/cli'
Warning in install.packages :
installation of package ‘cli’ had non-zero exit status
比较关键的一行报错是
sh: c:/rtools40/mingw32/bin/gcc: No such file or directory
似乎是R的默认编译器路径不存在,查看了一下C盘,也确实没有这个文件夹,然后我用sys.which(‘make’)查看了一下我的make.exe的路径,发现被安装到E盘去了,因为是很久之前装的R 3.6.3,后来我update到了R 4.1.2,并不清楚为啥Rtools会在E盘,在stack overflow上搜了一下,发现了这两篇帖子
Why can’t I install properly rtools40 x64 Widows?
Rtools not being detected by R
于是按照了里面的操作,修改了R 4.1.2安装路径里的makeconf文件,解决了这个bug。
解决方法
makeconf在(R_HOME)/etc/i386和(R_HOME)/etc/x64这两个文件夹下,(R_HOME)代指你的R安装路径,例如我的就是E:/R-4.1.2,用记事本打开i386文件夹下的makeconf文件,找到如下代码段(应该就在刚开头10几行的样子)
## The rtools40 installer sets RTOOLS40_HOME, default to standard install path
RTOOLS40_HOME ?= c:/rtools40
RTOOLS40_ROOT ?= $(subst \,/,$(RTOOLS40_HOME))
## Things which are substituted by fixed/Makefile (and also -O2 -> -O2)
WIN = 32
MINGW_PREFIX = /mingw$(WIN)
BINPREF ?= "$(RTOOLS40_ROOT)/mingw32/bin/"
COMPILED_BY = gcc-8.3.0
修改RTOOLS40_HOME的路径为Rtools的实际安装路径,并把BINPREF的路径也修改成对应的路径
## The rtools40 installer sets RTOOLS40_HOME, default to standard install path
RTOOLS40_HOME ?= E:/Rtools
RTOOLS40_ROOT ?= $(subst \,/,$(RTOOLS40_HOME))
## Things which are substituted by fixed/Makefile (and also -O2 -> -O2)
WIN = 32
MINGW_PREFIX = /mingw$(WIN)
BINPREF ?= "$(RTOOLS40_ROOT)/mingw_32/bin/"
COMPILED_BY = gcc-8.3.0
同样地,也需要修改x64文件夹下的makeconf文件,修改完后如下所示,区别在于一个是mingw_32,一个是mingw_64,分别对应32位和64位的,原帖中说如果安装的是32位就修改i386下的makeconf,如果是64位就修改x64,但实测需要两个都修改才能起效。
## The rtools40 installer sets RTOOLS40_HOME, default to standard install path
RTOOLS40_HOME ?= E:/Rtools
RTOOLS40_ROOT ?= $(subst \,/,$(RTOOLS40_HOME))
## Things which are substituted by fixed/Makefile (and also -O2 -> -O2)
WIN = 64
MINGW_PREFIX = /mingw$(WIN)
BINPREF ?= "$(RTOOLS40_ROOT)/mingw_64/bin/"
COMPILED_BY = gcc-8.3.0
修改完成后,重启一下R studio(或者restart r session),再次安装R包并编译
>install.packages("cli")
有二进制版本的,但源代码版本是后来的:
binary source needs_compilation
cli 3.1.1 3.2.0 TRUE
安装源码包‘cli’
trying URL 'https://cran.rstudio.com/src/contrib/cli_3.2.0.tar.gz'
Content type 'application/x-gzip' length 493192 bytes (481 KB)
downloaded 481 KB
* installing *source* package 'cli' ...
** package 'cli' successfully unpacked and MD5 sums checked
** using staged installation
Warning in parse(con, encoding = "UTF-8")