1
votes

Using the default hello world program through Rcpp.package.skeleton I 'sometimes' get this error on install.

Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/home/usrname/R/x86_64-pc-linux-gnu-library/3.0/helloWorld/libs/helloWorld.so': /home/usrname/R/x86_64-pc-linux-gnu-library/3.0/helloWorld/libs/helloWorld.so: undefined symbol: _ZTIN4Rcpp7RObjectE.

By sometimes I mean, in about 10 tries only once did it get that error on the first time installing although in the end after some tweaks every package eventually gets the error and even if all changes are removed from the code and all the .o,.so and the tar file deleted, and the library uninstalled in R, the package will not build again.

My end goal is to include some zlib functions into some code using the -lz compile option so when I say 'some tweaks' to the hello_world they would be:
1. add -lz to the PKG_LIBS var in Makevars
2. Add #include to the .cpp file
3. Create a const char* a
4. attempt to use a to call gzopen(a,a)

My process is
1. in R: Rcpp.package.skeleton("testPackage", attributes = TRUE)
2. in terminal: R CMD build testPackage
3. in terminal: R CMD INSTALL testPackage

Just to test one last time, I compiled okay, I added the -lz and it was okay, then I added #include and I get the error. I remove it and still get the error.

So:
1. Does anyone know what is causing this error?
2. Is there away to repair it so that reverted code installs again?
3. If not, is there another way to build zlib into code (sourceCpp seems to work fine).

I'm on Ubuntu 12.04.2 LTS
R version 3.01
Rcpp_0.10.3

Update with solution:
When editing Makevars I was inadvertently adding an extra set of quotes around the variable as such:
PKG_LIBS = "$/usr/bin/Rscript -e "Rcpp:::LdFlags()" -lz"
The correct way to include zlib is to just append -lz to the line:
PKG_LIBS = $/usr/bin/Rscript -e "Rcpp:::LdFlags()" -lz
It still remains true that once you get the error, if you try to revert Makevars back to the correct format, you will continue to get the error on install. And it also remains true that sometimes (2/6 from my test runs) it will install with the error only to later pop up when including or adding zlib functions into the code.

1

1 Answers

3
votes

My RcppCNPy package uses zlib. It uses

 PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` -lz

and has code as eg

cnpy::NpyArray cnpy::npy_gzload(std::string fname) {
    gzFile fp = gzopen(fname.c_str(), "rb");
    if(!fp) {
        Rf_error("npy_gzload: Error! Unable to open file %s!\n",fname.c_str());
    }
    NpyArray arr = gzload_the_npy_file(fp);
    gzclose(fp);
    return arr;
}

Maybe this can serve as a model for you.