GNU Guix系统R安装Rcpp/ggplot2时std::__throw_bad_array_new_length错误求助
在GNU Guix Linux-libre系统中安装R包时遇到
std::__throw_bad_array_new_length()编译错误 我在GNU Guix Linux-libre系统中安装了R 4.2.2版本,尝试安装Rcpp包时出现编译错误,提示std::__throw_bad_array_new_length()不是std的成员;安装ggplot2时也触发相同错误(实际是依赖的farver包编译失败)。怀疑系统缺少R依赖的必要包,但不清楚具体是哪个,相关报错及系统环境信息如下:
安装Rcpp时的报错
> install.packages("Rcpp") Installing package into ‘/home/guix/R/x86_64-unknown-linux-gnu-library/4.2’ (as ‘lib’ is unspecified) --- Please select a CRAN mirror for use in this session --- trying URL 'https://cloud.r-project.org/src/contrib/Rcpp_1.0.10.tar.gz' Content type 'application/x-gzip' length 2936173 bytes (2.8 MB) ================================================== downloaded 2.8 MB * installing *source* package ‘Rcpp’ ... ** package ‘Rcpp’ successfully unpacked and MD5 sums checked ** using staged installation ** libs g++ -std=gnu++14 -I"/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/include" -DNDEBUG -I../inst/include/ -I/usr/local/include -fpic -g -O2 -c api.cpp -o api.o In file included from /gnu/store/fyc3x8nd2ln090y4ng020rvspwl5pc78-gcc-12.2.0/include/c++/x86_64-unknown-linux-gnu/bits/c++allocator.h:33, from /home/guix/.guix-profile/include/c++/bits/allocator.h:46, from /home/guix/.guix-profile/include/c++/unordered_map:40, from ../inst/include/Rcpp/platform/compiler.h:153, from ../inst/include/Rcpp/r/headers.h:62, from ../inst/include/RcppCommon.h:30, from ../inst/include/Rcpp.h:27, from api.cpp:26: /home/guix/.guix-profile/include/c++/bits/new_allocator.h: In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*)’: /home/guix/.guix-profile/include/c++/bits/new_allocator.h:125:20: error: ‘__throw_bad_array_new_length’ is not a member of ‘std’; did you mean ‘bad_array_new_length’? 125 | std::__throw_bad_array_new_length(); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | bad_array_new_length make: *** [/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/etc/Makeconf:178: api.o] Error 1 ERROR: compilation failed for package ‘Rcpp’ * removing ‘/home/guix/R/x86_64-unknown-linux-gnu-library/4.2/Rcpp’ The downloaded source packages are in ‘/tmp/RtmptAJQ9x/downloaded_packages’ Warning message: In install.packages("Rcpp") : installation of package ‘Rcpp’ had non-zero exit status
安装ggplot2时的报错(触发farver包错误)
* installing *source* package ‘farver’ ... ** package ‘farver’ successfully unpacked and MD5 sums checked ** using staged installation ** libs g++ -std=gnu++11 -I"/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c ColorSpace.cpp -o ColorSpace.o g++ -std=gnu++11 -I"/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c Comparison.cpp -o Comparison.o g++ -std=gnu++11 -I"/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c Conversion.cpp -o Conversion.o g++ -std=gnu++11 -I"/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/include" -DNDEBUG -I/usr/local/include -fpic -g -O2 -c encode.cpp -o encode.o In file included from /gnu/store/fyc3x8nd2ln090y4ng020rvspwl5pc78-gcc-12.2.0/include/c++/x86_64-unknown-linux-gnu/bits/c++allocator.h:33, from /home/guix/.guix-profile/include/c++/bits/allocator.h:46, from /home/guix/.guix-profile/include/c++/unordered_map:40, from encode.h:4, from encode.cpp:1: /home/guix/.guix-profile/include/c++/bits/new_allocator.h: In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*)’: /home/guix/.guix-profile/include/c++/bits/new_allocator.h:125:20: error: ‘__throw_bad_array_new_length’ is not a member of ‘std’; did you mean ‘bad_array_new_length’? 125 | std::__throw_bad_array_new_length(); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | bad_array_new_length make: *** [/gnu/store/kjd4gfz8frchb8n4ada2l6nssa9pnin6-r-minimal-4.2.2/lib/R/etc/Makeconf:178: encode.o] Error 1 ERROR: compilation failed for package ‘farver’ * removing ‘/home/guix/R/x86_64-unknown-linux-gnu-library/4.2/farver’
系统环境信息
> sessionInfo() R version 4.2.2 (2022-10-31) Platform: x86_64-unknown-linux-gnu (64-bit) Running under: Guix System Matrix products: default BLAS/LAPACK: /gnu/store/xghpgl3fnr603z16vg7hldmpxygriqm2-openblas-0.3.20/lib/libopenblasp-r0.3.20.so locale: [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C [3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8 [5] LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8 [7] LC_PAPER=en_US.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_4.2.2 tools_4.2.2 tcltk_4.2.2
解决方法
- 使用Guix官方包安装(推荐)
Guix的包管理系统会自动处理所有依赖,避免环境冲突。直接在终端执行以下命令安装所需R包:
guix install r-rcpp r-ggplot2
安装完成后,启动R即可直接调用这些包。
- 修复编译环境冲突
如果坚持要通过R的install.packages安装,需要确保编译环境的一致性:
- 先进入包含R和对应GCC版本的隔离环境:
guix environment r-minimal --ad-hoc gcc-toolchain
- 在该环境中启动R,再执行
install.packages("Rcpp")或install.packages("ggplot2")
错误根源是系统中混合了不同路径的C++标准库头文件(系统GCC的头文件和用户profile中的头文件版本不匹配),导致编译器无法找到内部函数std::__throw_bad_array_new_length()。Guix的隔离环境可以彻底解决这类依赖冲突问题。
内容的提问来源于stack exchange,提问作者Давид Пирић
相关产品推荐
相关产品推荐

