6

I am new to R package development. I am developing a package (bartpkg) that has in its src/ folder one (prime) cpp file and some helper cpp (X.Cpp, Y.Cpp) and one c file (Z.C) and their header files (X.h, Y.h and Z.h)

I am getting the following error when I do 'Build & Reload' in Rstudio.

Error in dyn.load(dllfile) :
  unable to load shared object '/Users/abcd/BART/bart_pkg1/src/bartpkg.so':
  dlopen(/Users/abcd/BART/bart_pkg1/src/bartpkg.so, 6): Symbol not found: __ZN3RNG4nfixElm
  Referenced from: /Users/abcd/BART/bart_pkg1/src/bartpkg.so
  Expected in: flat namespace
 in /Users/abcd/BART/bart_pkg1/src/bartpkg.so
Calls: suppressPackageStartupMessages ... <Anonymous> -> load_all -> load_dll -> library.dynam2 -> dyn.load
Execution halted

Exited with status 1.

I have followed the basic guidelines to build the package.

The .R file has directive #' @useDynLib bartpkg in the right place.

Also, the prime cpp file has the following tags in the right place.

#include <Rcpp.h>
using namespace Rcpp;
//' @param x A single integer.
//' @export
// [[Rcpp::export]]

And my NAMESPACE file shows useDynLib(bartpkg) correctly.

I am able to see the bartpkg.so shared object file in the src/ directory.

I tried in the terminal this command c++filt -n _ZN3RNG4nfixElm and was able to see that the symbol in the error

Symbol not found: __ZN3RNG4nfixElm' is coming from the .C file RNG.C and is because of a function 'nfix'.

But even if I remove the function 'nfix' or remove the RNG.C file altogether, the same error:

Symbol not found: __ZN3RNG4nfixElm

can it be a flag issue that my compiler is not able to compile the 'C' file? I am able to see that all the cpp files generate respective object files, but I dont see anything like that for the C file.

I have a Makevars in the src/ directory just having one line

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

which I basically just copied from another package, more because my understanding with Rcpp and the which flags to work with is really less.

I am using RStudio is the session info is

> sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] bartpkg_0.1.0        packrat_0.4.8-1      Rcpp_0.12.8          msm_1.6.4            LaplacesDemon_16.0.1

loaded via a namespace (and not attached):
 [1] roxygen2_5.0.1  lattice_0.20-34 mvtnorm_1.0-5   digest_0.6.10   grid_3.3.1      magrittr_1.5  
 [7] stringi_1.1.2   Matrix_1.2-7.1  splines_3.3.1   tools_3.3.1     stringr_1.1.0   survival_2.39-5
[13] parallel_3.3.1  rsconnect_0.5   inline_0.3.14   expm_0.999-0

I am stuck at this problem for weeks now. Trying to read the 'Writing the R extension' page and not able to figure this out. Any help would be highly appreciated.

Thank you.

coatless
  • 20,011
  • 13
  • 69
  • 84
Usernow
  • 149
  • 2
  • 8
  • 2
    Make sure you run `compileAttributes()` if you add/remove/change interface or else your `RcppExport.cpp` will be out of sync and can give such errors. – Dirk Eddelbuettel Dec 02 '16 at 04:12
  • You don't need the `PKG_LIBS = ....` anymore. That changes a few years ago. See what `Rcpp.package.skeleton()` creates for you. – Dirk Eddelbuettel Dec 02 '16 at 04:54
  • Thank you for your help, @DirkEddelbuettel _"Make sure you run compileAttributes() if you add/remove/change interface or else your RcppExport.cpp will be out of sync and can give such errors."_ > My understanding is, and I checked it too, that whenever I do Build and Reload in Rstudio, it automatically does the `Rcpp::compileAttributes()` for me, so I needn't do it myself. Also, regarding your second reply, I did not create the pacakge with the help of **Rcpp.Package.skeleton**. I created it manually. > Any suggestions? – Usernow Dec 02 '16 at 18:40
  • Indeed, RStudio will do that for you so that is not it. In which case I would then suggest to look carefully and what files are compiled and linked into the shared library -- because you do seem be missing a symbol. Sometimes it helps to start small, via `Rcpp.package.skeleton()` or the File -> NewProject -> Package -> PackageWithRcpp option in RStudio. – Dirk Eddelbuettel Dec 02 '16 at 18:44
  • @DirkEddelbuettel I just recreated the entire package with File -> NewProject -> Package -> PackageWithRcpp option in RStudio. The same error persists. Really disheartened right now. Do not know what to do. > Some extra pointers.. 1. There are no MakeVars file in the src folder. 2. I am building my Package with PackRat. 3. I can see the symbol I am missing, but I do not know why is it happening. Any help, please? – Usernow Dec 02 '16 at 19:21
  • I never use packrat, I never have the problem. But seriously: you did _not_ supply a reproducible example so _nobody can know_ what you did wrong. Start from a working example, and add one new header and source file defining a trivial function, call that. It if still works build from there. – Dirk Eddelbuettel Dec 02 '16 at 19:24
  • 1
    @DirkEddelbuettel Could it be that I have the .C and the .Cpp files in the src directory and somehow .C is not getting compiled? I think so because the missing symbol is from the .C file and also because I do not know if Rcpp is compiling the .C file at all.. – Usernow Dec 02 '16 at 19:26
  • 5
    Beginner error: If you call C functions you **must** precede their declarations with `extern "C"`. – Dirk Eddelbuettel Dec 02 '16 at 19:27
  • 8
    We all wasted hours on this because you did not bother to supply a [minimally reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Dirk Eddelbuettel Dec 02 '16 at 19:28

0 Answers0