3

I have a question about the interesting post found at:

Rcpp with quad precision computation

I use:

// [[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>();
// }

If I understand correctly, I do not need to add:

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

Since I only want to use float128. When I compile with:

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

I get an error message:

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.

What am I doing wrong? Thank you in advance.

1 Answers1

1

Works "as is" on Linux without setting environment variables (modulo a page full of warnings under the -pedantic flag I now turned off):

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

You may need to get the quadmath library. Not sure if the Rtools g++ has it.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you Dirk. I have rechecked, and yes, there is a quadmath.h file in Rtools (in Rtools\mingw_64\lib\gcc\x86-64-w64-mingw32\4.9.3). If I have quadmath, what could be wrong, then? – Anarcocapitalista Socialdemocr Oct 18 '18 at 17:13
  • @AnarcocapitalistaSocialdemocr You may want to try running `Sys.setenv("PKG_LIBS" = "-lquadmath")` before `Rcpp::sourceCpp('/tmp/quadexp.cpp')`. It didn't work for me either (even though I'm on Linux -- Ubuntu 18.04, R 3.5.1, Rcpp 0.12.19, BH 1.66.0-1, gcc/g++ 7.3.0) until I did that. – duckmayr Oct 19 '18 at 08:56
  • @duckmayr Thank you very much for your answer. When I run `Sys.setenv("PKG_LIBS" = "-lquadmath")` and then `Rcpp::sourceCpp('/tmp/quadexp.cpp')`, it runs without errors. But then, when I execute qexp(), I get a message in RStudio about "fatal error", and RStudio shuts down completely. Do you know what could be happening? – Anarcocapitalista Socialdemocr Oct 19 '18 at 09:25
  • @AnarcocapitalistaSocialdemocr Not offhand. It might warrant a new question. – duckmayr Oct 19 '18 at 09:34
  • 1
    @duckmayr I am on 18.04 too and it works for me. Using standard packages. – Dirk Eddelbuettel Oct 19 '18 at 10:51
  • @DirkEddelbuettel Interesting. I'll have to look into it later to see what the hangup is on my end. – duckmayr Oct 19 '18 at 11:03
  • @duckmayr May I ask if there is any update from your side? – Anarcocapitalista Socialdemocr Oct 22 '18 at 09:31
  • @AnarcocapitalistaSocialdemocr I believe the issue on my side was a custom .R/Makevars (cribbed from [this Stack Overflow post](https://stackoverflow.com/questions/50658198/cran-submission-r-cmd-check-warning-compilation-flags-used) for package development reasons). If I run `Sys.setenv("PKG_LIBS" = "-lquadmath")` before compiling, it all works out fine -- it compiles and, in reference to one of your earlier comments, the function can be called without error. – duckmayr Oct 22 '18 at 10:06
  • I get a similar error even when I set PKG_LIBS on Manjaro Linux: ```Error in dyn.load("/tmp/RtmpfNmcIZ/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_229717fda3f/sourceCpp_7.so") : unable to load shared object '/tmp/RtmpfNmcIZ/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_229717fda3f/sourceCpp_7.so': /tmp/RtmpfNmcIZ/sourceCpp-x86_64-pc-linux-gnu-1.0.1/sourcecpp_229717fda3f/sourceCpp_7.so: undefined symbol: expq``` – wdkrnls May 24 '19 at 02:19
  • I needed to explicitly `#include `. – wdkrnls May 24 '19 at 02:25