【问题标题】:Rcpp and boost: it should work but it does notRcpp 和 boost:它应该可以工作,但不能
【发布时间】:2018-10-18 14:29:09
【问题描述】:

我对在以下位置找到的有趣帖子有疑问:

Rcpp with quad precision computation

我用:

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>
#include <boost/multiprecision/float128.hpp>
//#include <boost/multiprecision/mpfr.hpp>

namespace mp = boost::multiprecision;

// [[Rcpp::export]]
std::string qexp(double da = -1500.0, double db = -1501.0)
{
  mp::float128 a(da), b(db);
  mp::float128 res = mp::exp(a) / (mp::exp(a) + mp::exp(b));
  return res.convert_to<std::string>();
}

// // [[Rcpp::export]]
// std::string mpfr_exp(double da = -1500.0, double db = -1501.0)
// {
//   mp::mpf_float_100 a(da), b(db);
//   mp::mpf_float_100 res = mp::exp(a) / (mp::exp(a) + mp::exp(b));
//   return res.convert_to<std::string>();
// }

如果我理解正确,我不需要补充:

Sys.setenv("PKG_LIBS" = "-lmpfr -lgmp")

因为我只想使用 float128。当我编译时:

Rcpp::sourceCpp('/tmp/quadexp.cpp')

我收到一条错误消息:

c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~4/ROPEN~1/R-35~1.1/include" -DNDEBUG   -I"C:/Users/Jordi/Documents/R/win-library/3.5/Rcpp/include" -I"C:/Users/Jordi/Documents/R/win-library/3.5/BH/include" -I"C:/Users/Jordi/Documents"   -I"C:/swarm/workspace/External-R-3.5.1/vendor/extsoft/include"     -O2 -Wall  -mtune=core2 -c quadexp.cpp -o quadexp.o
c:/Rtools/mingw_64/bin/g++ -m64 -shared -s -static-libgcc -o sourceCpp_3.dll tmp.def quadexp.o -LC:/swarm/workspace/External-R-3.5.1/vendor/extsoft/lib/x64 -LC:/swarm/workspace/External-R-3.5.1/vendor/extsoft/lib -LC:/PROGRA~1/MICROS~4/ROPEN~1/R-35~1.1/bin/x64 -lR
quadexp.o:quadexp.cpp:(.text+0x1c9): undefined reference to `expq'
quadexp.o:quadexp.cpp:(.text+0x1df): undefined reference to `expq'
quadexp.o:quadexp.cpp:(.text+0x213): undefined reference to `expq'
quadexp.o:quadexp.cpp:(.text+0x2cc): undefined reference to `quadmath_snprintf'
quadexp.o:quadexp.cpp:(.text+0x30f): undefined reference to `quadmath_snprintf'
collect2.exe: error: ld returned 1 exit status
Error in Rcpp::sourceCpp("C:/Users/Jordi/Documents/quadexp.cpp") : 
  Error occurred building shared library.

我做错了什么?提前谢谢你。

【问题讨论】:

    标签: r boost rcpp


    【解决方案1】:

    在 Linux 上“按原样”工作无需设置环境变量(以我现在关闭的 -pedantic 标志下的充满警告的页面为模):

    R> Rcpp::sourceCpp("/tmp/52876341/q.cpp")
    [1] "0.731058578630004879251159241821836351"
    R> qexp()
    [1] "0.731058578630004879251159241821836351"
    R>
    

    您可能需要获取 quadmath 库。不确定 Rtools g++ 是否有。

    【讨论】:

    • 谢谢德克。我重新检查了,是的,Rtools 中有一个 quadmath.h 文件(在 Rtools\mingw_64\lib\gcc\x86-64-w64-mingw32\4.9.3 中)。如果我有 quadmath,那会是什么问题呢?
    • @AnarcocapitalistaSocialdemocr 您可能想在Rcpp::sourceCpp('/tmp/quadexp.cpp') 之前尝试运行Sys.setenv("PKG_LIBS" = "-lquadmath")。在我这样做之前,它也对我不起作用(即使我在 Linux 上——Ubuntu 18.04、R 3.5.1、Rcpp 0.12.19、BH 1.66.0-1、gcc/g++ 7.3.0)。
    • @duckmayr 非常感谢您的回答。当我运行Sys.setenv("PKG_LIBS" = "-lquadmath") 然后Rcpp::sourceCpp('/tmp/quadexp.cpp') 时,它运行没有错误。但是,当我执行 qexp() 时,我在 RStudio 中收到一条关于“致命错误”的消息,并且 RStudio 完全关闭。你知道会发生什么吗?
    • @AnarcocapitalistaSocialdemocr 不是随便的。这可能需要一个新问题。
    • @duckmayr 我也在 18.04,它对我有用。使用标准包。
    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 2020-06-30
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2022-11-14
    相关资源
    最近更新 更多