R包在Linux下编译不通过,原因是gcc版本太低怎么办?
一些有C 代码的R包可能会用到一些新的C 特性,需要C 11或者C 14。这个问题通常在CentOS/红帽系统上出现,因为系统稳定的要求,这个系列的系统它的C 版本很低。但请读者前往注意了别自己编译新版本的gcc,然后替换掉系统的。这种操作我试过几次,系统基本上就崩掉了。
正确的解决方式是安装独立的gcc,通过环境变量引用和使用它。
在Root用户下操作:
代码语言:javascript复制yum install centos-release-scl
yum install devtoolset-9
然后在你使用R的用户下操作:
代码语言:javascript复制# If you use your non-root account to install packages,
# change /root to /home/your_id in the following command
mkdir -p /root/.R
vi /root/.R/Makevars
将下面的内容写入打开的文件,然后保存:
代码语言:javascript复制CXX11=/opt/rh/devtoolset-9/root/usr/bin/g -std=c 11 -fPIC
CXX14=/opt/rh/devtoolset-9/root/usr/bin/g -std=c 14 -fPIC
如果没有root权限,可以通过conda来安装新版本的gcc,c 等:
代码语言:javascript复制1conda install gcc_linux-64
2conda install gxx_linux-64
加上conda-forge通道也可以。
另外可以一键安装常用的编译器:
代码语言:javascript复制1conda install -c conda-forge compilers
安装完成后记得添加环境变量:
代码语言:javascript复制1export CC=/path/to/anaconda/bin/x86_64-conda_cos6-linux-gnu-gcc
2export CXX=/path/to/anaconda/bin/x86_64-conda_cos6-linux-gnu-g
如果是R包编译,修改前面提到的Makevars
文件即可。
如果使用的是miniconda,这个文件的内容可能就是这样的了:
代码语言:javascript复制1CXX11=~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 11 -fPIC
2CXX14=~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC
这样就可以愉快地安装包了:
代码语言:javascript复制 1> install.packages("xgboost")
2试开URL’https://mirrors.e-ducation.cn/CRAN/src/contrib/xgboost_1.4.1.1.tar.gz'
3Content type 'application/x-gzip' length 988320 bytes (965 KB)
4==================================================
5downloaded 965 KB
6
7* installing *source* package ‘xgboost’ ...
8** 成功将‘xgboost’程序包解包并MD5和检查
9** using staged installation
10checking for x86_64-conda-linux-gnu-gcc... ~/miniconda3/bin/x86_64-conda-linux-gnu-cc
11checking whether the C compiler works... yes
12checking for C compiler default output file name... a.out
13checking for suffix of executables...
14checking whether we are cross compiling... no
15checking for suffix of object files... o
16checking whether we are using the GNU C compiler... yes
17checking whether ~/miniconda3/bin/x86_64-conda-linux-gnu-cc accepts -g... yes
18checking for ~/miniconda3/bin/x86_64-conda-linux-gnu-cc option to accept ISO C89... none needed
19checking Backtrace lib...
20checking for backtrace in -lexecinfo... no
21checking endian...
22configure: creating ./config.status
23config.status: creating src/Makevars
24** libs
25Makevars:17: -DXGBOOST_STRICT_R_MODE=1
26Makevars:17: -DDMLC_LOG_BEFORE_THROW=0
27Makevars:17: -DDMLC_ENABLE_STD_THREAD=1
28Makevars:17: -DDMLC_DISABLE_STDIN=1
29Makevars:17: -DDMLC_LOG_CUSTOMIZE=1
30Makevars:17: -DXGBOOST_CUSTOMIZE_LOGGER=1
31Makevars:17: -DRABIT_CUSTOMIZE_MSG_
32~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c xgboost_R.cc -o xgboost_R.o
33~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c xgboost_custom.cc -o xgboost_custom.o
34gcc -std=gnu99 -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fpic -g -O2 -c xgboost_assert.c -o xgboost_assert.o
35gcc -std=gnu99 -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fpic -g -O2 -c init.c -o init.o
36~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c amalgamation/xgboost-all0.cc -o amalgamation/xgboost-all0.o
37~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c amalgamation/dmlc-minimum0.cc -o amalgamation/dmlc-minimum0.o
38~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c rabit/src/engine.cc -o rabit/src/engine.o
39~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c rabit/src/c_api.cc -o rabit/src/c_api.o
40~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -I"/public/apps/R-4.0/R-4.0.3/include" -DNDEBUG -I./include -I./dmlc-core/include -I./rabit/include -I. -DXGBOOST_STRICT_R_MODE=1 -DDMLC_LOG_BEFORE_THROW=0 -DDMLC_ENABLE_STD_THREAD=1 -DDMLC_DISABLE_STDIN=1 -DDMLC_LOG_CUSTOMIZE=1 -DXGBOOST_CUSTOMIZE_LOGGER=1 -DRABIT_CUSTOMIZE_MSG_ -I/public/apps/R-4.0/zlib/include -I/public/apps/R-4.0/bzip2/include -I/public/apps/R-4.0/xz/include -I/public/apps/R-4.0/curl/include -I/public/apps/R-4.0/pcre/include -fopenmp -DDMLC_CMAKE_LITTLE_ENDIAN=1 -pthread -c rabit/src/allreduce_base.cc -o rabit/src/allreduce_base.o
41~/miniconda3/bin/x86_64-conda-linux-gnu-g -std=c 14 -fPIC -shared -L/public/apps/R-4.0/R-4.0.3/lib -L/public/apps/R-4.0/zlib/lib ....
42** R
43** data
44** demo
45** inst
46** byte-compile and prepare package for lazy loading
47** help
48*** installing help indices
49** building package indices
50** installing vignettes
51** testing if installed package can be loaded from temporary location
52** checking absolute paths in shared objects and dynamic libraries
53** testing if installed package can be loaded from final location
54** testing if installed package keeps a record of temporary installation path
55* DONE (xgboost)
56
57下载的程序包在
58 ‘/tmp/RtmpjK0jDw/downloaded_packages’里