0
votes

I am developing an R-binding for a Solar PV simulation library developed by NREL. I am able to successfully compile the R-extension RcppSSC that I wrote using Rcpp. I based this on some examples from Rcpp gallery and the book Seamless R and C++ Integration with Rcpp (Use R!) by Dirk Eddelbuettel.

The ssc library from NREL has x86 and x64 .lib and .dll files. When I build and test the module in RStudio, I get the following error. I check the load dependency and RcppSCC.dll needs ssc.dll.

I think I need to modify the Makevars file to copy the ssc.lib and ssc.dll files to the appropriate location during the build. How do I do this?

Any guidance or help is greatly appreciated. I'm very comfortable with C, C++, Make etc. No experience developing R extensions or Rcpp based modules.

I ran into some issues during linking and I fixed that easily.

My Makevars.win is as follows.

PKG_CPPFLAGS += -I. -I"C:/SAM/ssc-sdk-2014-1-21"

ifeq "$(WIN)" "64"
PKG_LIBS += -L"C:/SAM/ssc-sdk-2014-1-21/win64/" -lssc
else
PKG_LIBS += -L"C:/SAM/ssc-sdk-2014-1-21/win32/" -lssc
endif

=============================RStudio error message====================================

==> Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source RcppSSC

* installing to library 'C:/Users/kumar_000/Documents/R/win-library/3.1'
* installing *source* package 'RcppSSC' ...
** libs
g++ -m64 -I"C:/PROGRA~1/R/R-31~1.1/include" -DNDEBUG -I. -I"C:/SAM/ssc-sdk-2014-1-21"   -I"C:/Users/kumar_000/Documents/R/win-library/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m64 -I"C:/PROGRA~1/R/R-31~1.1/include" -DNDEBUG -I. -I"C:/SAM/ssc-sdk-2014-1-21"   -I"C:/Users/kumar_000/Documents/R/win-library/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c RcppSSC.cpp -o RcppSSC.o
g++ -m64 -shared -s -static-libgcc -o RcppSSC.dll tmp.def RcppExports.o RcppSSC.o -LC:/SAM/ssc-sdk-2014-1-21/win64/ -lssc -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.1/bin/x64 -lR
installing to C:/Users/kumar_000/Documents/R/win-library/3.1/RcppSSC/libs/x64
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in inDL(x, as.logical(local), as.logical(now), ...) : 
  unable to load shared object 'C:/Users/kumar_000/Documents/R/win-library/3.1/RcppSSC/libs/x64/RcppSSC.dll':
  LoadLibrary failure:  The specified module could not be found.

Error: loading failed
Execution halted
ERROR: loading failed
* removing 'C:/Users/kumar_000/Documents/R/win-library/3.1/RcppSSC'

Exited with status 1.
1

1 Answers

0
votes

To a first approximation you cannot mix C++ code from Rtools (g++) and Visual Studio (the DLL you got). This has to do with C++ internals and the non-standardized mangling of symbols. This is fairly widely documented, see eg the Rcpp-FAQ.

You may be able to create a plain C layer between the two as this avoids the mangling issue, but that is a) still finicky as you mix compilers and b) foregoes many/most of the benefits of working with Rcpp / C++.

Now, I may also have misunderstood your question. If you just want copy the DLL around where R finds it in its path, one cheap (and cheating) way is to put it into one of R's bin directories. I may once have done with a vendor library for a work project, it obviously will not work something going onto CRAN -- but there you generally can't have non-open source Depends anyway.

If you actually compiled the DLL yourself, then there are existing packages such as eg RSQLite and RPostgreSQL which actually build a library on the fly for use by the R package--even on Windows.

Edit: Thanks for your follow-up comment. So here are some more bullet points:

  • I seem to recall that the DLL must be in the path, not just in the package. So to test that, just copy it into R's bin directory and see if the package loads that way.
  • I like to work by example, so have a look at some of the more complex Makevars.win from Rcpp and RInside--maybe that will give you an idea.
  • Also consider a post to rcpp-devel: a couple of savvy Windows users of Rcpp lurk there and can help; they are not likely to see this.
  • Lastly, the Rcpp Gallery will need self-contained posts that we can 'sourceCpp()' on Linux...