Windows环境下RcppParallel编译失败,寻求解决方案
问题背景
已安装Rcpp、RcppParallel包,测试如下并行求和的.cpp代码:
#include <RcppParallel.h> #include <Rcpp.h> #include <vector> using namespace RcppParallel; using namespace Rcpp; struct Sum : public Worker { const std::vector<int> &input; int value; Sum(const std::vector<int> &input) : input(input), value(0) {} Sum(const Sum &sum, Split) : input(sum.input), value(0) {} void operator()(std::size_t begin, std::size_t end) { value += std::accumulate(input.begin() + begin, input.begin() + end, 0); } void join(const Sum &rhs) { value += rhs.value; } }; // [[Rcpp::export]] int parallelSum(const std::vector<int> &x) { Sum sum(x); parallelReduce(0, x.size(), sum); return sum.value; }
执行sourceCpp("UntitledCPP.cpp")时,出现头文件找不到的错误:
sourceCpp("UntitledCPP.cpp") using C++ compiler: 'G__~1.EXE (GCC) 13.2.0' g++ -std=gnu++17 -I"C:/PROGRA~1/R/R-44~1.1/include" -DNDEBUG -I"C:/Users/callimae/AppData/Local/R/win-library/4.3/Rcpp/include" -I"C:/Users/callimae/Documents/Projects/NewEM_conjugate_base" -I"C:/rtools44/x86_64-w64-mingw32.static.posix/include" -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c UntitledCPP.cpp -o UntitledCPP.o UntitledCPP.cpp:1:10: fatal error: RcppParallel.h: No such file or directory 1 | #include <RcppParallel.h> | ^~~~~~~~~~~~~~~~ compilation terminated. make: *** [C:/PROGRA~1/R/R-44~1.1/etc/x64/Makeconf:296: UntitledCPP.o] Error 1 Error in sourceCpp("UntitledCPP.cpp") : Error 1 occurred building shared library.
手动指定RcppParallel头文件路径后,又出现tinythread相关错误。当前环境:
sessionInfo() R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22631) locale: [1] LC_COLLATE=Polish_Poland.utf8 LC_CTYPE=Polish_Poland.utf8 LC_MONETARY=Polish_Poland.utf8 LC_NUMERIC=C [5] LC_TIME=Polish_Poland.utf8 time zone: Europe/Warsaw tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] RcppArmadillo_14.0.0-1 RcppParallel_5.1.8 Rcpp_1.0.12 loaded via a namespace (and not attached): [1] compiler_4.4.1 magrittr_2.0.3 cli_3.6.3 tools_4.4.1 fs_1.6.4 glue_1.7.0 rstudioapi_0.16.0 vctrs_0.6.5 [9] usethis_2.2.3 lifecycle_1.0.4 rlang_1.1.4 purrr_1.0.2
已重装所有Rcpp相关包,求解决思路。
解决思路
让sourceCpp自动识别RcppParallel路径
不要手动指定路径,在cpp代码开头添加// [[Rcpp::depends(RcppParallel)]]注释,这样sourceCpp会自动加载RcppParallel的头文件和链接库路径,示例:// [[Rcpp::depends(RcppParallel)]] #include <RcppParallel.h> #include <Rcpp.h> // 后续代码不变检查Rtools与R版本匹配性
确认Rtools44是对应R4.4.x的版本,且安装时勾选了“Add rtools to system PATH”选项。可以在R中运行Sys.getenv("PATH")查看是否包含Rtools的bin和mingw64/bin路径。清理旧编译缓存
运行Rcpp::cleanup()清理之前的编译缓存,然后重新执行sourceCpp命令,避免旧缓存干扰。手动指定链接库(如果自动依赖失效)
如果添加depends注释后仍有tinythread问题,可在cpp代码中添加链接指令:// [[Rcpp::depends(RcppParallel)]] // [[Rcpp::plugins(cpp17)]] #include <RcppParallel.h> #include <Rcpp.h> // 后续代码不变同时确保编译时使用C17标准(你的日志里已经用了gnu17,这步主要是确认标准一致性)。
检查包安装路径权限
确认RcppParallel的安装路径(system.file(package = "RcppParallel"))下的include文件夹存在且有读写权限,Windows下如果路径含中文或特殊字符,可能导致识别问题,建议把R包安装到默认的无特殊字符路径。
内容的提问来源于stack exchange,提问作者Kamat

