RCPP和Boost:它应该工作,但不起作用

时间:2018-10-18 14:29:10

标签: r boost rcpp

我对以下有趣的帖子有疑问:

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.

我在做什么错?预先谢谢你。

1 个答案:

答案 0 :(得分:1)

在Linux 上按原样运行,无需设置环境变量(对我现在关闭的-pedantic标志下充满警告的页面进行模化):

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

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