R包更新报错:'make_unique is not a member of std' 求助
解决Windows下编译R包ranger时
std::make_unique未定义问题 环境信息
- 系统:Windows 10
- R版本:4.1.2
- RStudio版本:2022.12.0
- Rtools版本:4.0
报错详情
编译ranger源码包时出现以下错误:
"C:/rtools40/mingw32/bin/"g++ -std=gnu++11 -I"C:/R-41~1.2/include" -DNDEBUG -DR_BUILD -DWIN_R_BUILD -I'C:/R-4.1.2/library/Rcpp/include' -I'C:/BITrusted/R-4.1.2/library/RcppEigen/include' -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c Forest.cpp -o Forest.o Forest.cpp: In member function 'std::unique_ptr<ranger::Data> ranger::Forest::loadDataFromFile(const string&)': Forest.cpp:850:19: error: 'make_unique' is not a member of 'std' result = std::make_unique<DataDouble>(); ^~~~~~~~~~~ Forest.cpp:850:19: note: 'std::make_unique' is defined in header '<memory>'; did you forget to '#include <memory>'? Forest.cpp:26:1: +#include <memory> Forest.cpp:850:19: result = std::make_unique<DataDouble>();
解决思路
1. 临时手动修改包源码编译
- 下载ranger源码包到本地:
download.packages("ranger", destdir = ".", type = "source") - 解压下载的
ranger_*.tar.gz包,找到src/Forest.cpp文件 - 在文件开头的include区域添加
#include <memory> - 回到R中安装修改后的包:
install.packages("path/to/modified/ranger", repos = NULL, type = "source")
2. 强制使用C++14编译标准
std::make_unique是C14引入的特性,当前编译命令用的是-std=gnu++11,可以强制切换到C14:
- 临时设置环境变量并安装:
Sys.setenv(CXX14 = "g++ -std=c++14") install.packages("ranger", type = "source") - 永久生效:在
C:\Users\你的用户名\.Renviron文件中添加CXX14=g++ -std=c++14,重启R后生效
3. 升级Rtools版本
Rtools4.0的编译器对部分新C++特性支持有限,尝试安装Rtools4.2(兼容R4.1.x系列),安装后确保系统环境变量中Rtools的bin目录优先级正确
4. 安装预编译二进制包
如果不需要源码编译,直接安装Windows预编译版本:
install.packages("ranger", type = "binary")
内容的提问来源于stack exchange,提问作者Stefan L.
相关产品推荐
相关产品推荐

