Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rcpp codes fails to load with boost library (as implemented in BH) when using boost::iostreams::gzip_decompress

Tags:

r

boost

rcpp

I wish to read data from a .bz file. I am trying to do it using rcpp plus boost using the BH library. I am running R 3.2.0 on MacOSX 10.10.3, Rcpp version 0.11.6, and BH 1.58.0-1.

Here is my code thus far:

// [[Rcpp::depends(BH)]]

#include <Rcpp.h>
#include <iostream>
#include <fstream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/math/common_factor.hpp>

using namespace Rcpp;

// [[Rcpp::export]]
int read_file_cpp(std::string path) {
  std::ifstream file(path.c_str(), std::ios_base::in | std::ios_base::binary);
  boost::iostreams::filtering_istream in;
  in.push(boost::iostreams::gzip_decompressor());
  in.push(file);
  for(std::string str; std::getline(in, str); )
  {
    std::cout << "Processed line " << str << '\n';
  }
  file.close();
  return(0);
}

When I try to load the code through:

Rcpp::sourceCpp(file = "src/boost_test.cpp")

I get the following error:

Error in dyn.load("/var/folders/sv/rhmpnyt91kdb10d1hnz_zd1c0000gn/T//RtmpZiCsVD/sourcecpp_40e63686ad7f/sourceCpp_9777.so") : unable to load shared object '/var/folders/sv/rhmpnyt91kdb10d1hnz_zd1c0000gn/T//RtmpZiCsVD/sourcecpp_40e63686ad7f/sourceCpp_9777.so': dlopen(/var/folders/sv/rhmpnyt91kdb10d1hnz_zd1c0000gn/T//RtmpZiCsVD/sourcecpp_40e63686ad7f/sourceCpp_9777.so, 6): Symbol not found: __ZN5boost9iostreams6detail11gzip_footer5resetEv Referenced from: /var/folders/sv/rhmpnyt91kdb10d1hnz_zd1c0000gn/T//RtmpZiCsVD/sourcecpp_40e63686ad7f/sourceCpp_9777.so Expected in: flat namespace in /var/folders/sv/rhmpnyt91kdb10d1hnz_zd1c0000gn/T//RtmpZiCsVD/sourcecpp_40e63686ad7f/sourceCpp_9777.so

By some troubleshooting, the problematic line seems to be:

in.push(boost::iostreams::gzip_decompressor());

More specifically, gzip_decompressor() seems to be the root cause. If I try gzip_compressor() instead the program works fine (except I just get binary gibberish on the screen).

Looking through BH code, I confirmed that gzip_decompressor code is where it is supposed to be.

Thank you for any help.

UPDATE

Following the suggestions from Dirk and nrussell, this is how I solved the issue.

Downloaded boost from here.

Read instructions here.

Unpacked, and ran the following:

./bootstrap.sh --help

Read the instructions, and decided I only needed to compile IOstreams. So, ran the following:

./bootstrap.sh --with-libraries=iostreams
./b2

Figured out the path where the compiler looks for libraries. I am sure there is a smarter way of doing it, but I ran the following from the R prompt:

Rcpp::sourceCpp('src/boost_test.cpp', verbose = T, rebuild = T)

From the ensuing text, noticed that '/usr/local/lib' was in the path. So, from the command prompt, I created a link to the library in the correct path:

ln -s /Users/andersgs/Downloads/boost_1_58_0/stage/lib/libboost_iostreams.dylib /usr/local/lib/

This will obviously need to be corrected later, as it won't be staying in my Downloads folder.

Then, followed the instructions by Dirk, with nrussell's recommendation. So, back in R:

Sys.setenv("PKG_LIBS"="-lboost_iostreams")
Rcpp::sourceCpp('src/boost_test.cpp', verbose = T, rebuild = T)

Success!!! Correctly compiled. Works too.

Thank you both.

Anders.

like image 885
Anders Goncalves da Silva Avatar asked Oct 19 '22 10:10

Anders Goncalves da Silva


1 Answers

Are you sure that this works "headers-only"?

If not, you need to make sure you provide the proper link arguments -- see the Rcpp Gallery example on Boost regex has a worked example.

like image 131
Dirk Eddelbuettel Avatar answered Oct 22 '22 01:10

Dirk Eddelbuettel