1

I am trying to create an R package that has some functions in main.cpp which is dependent on an external Cpp library, libDAI. Let's say DAI1.cpp, DAI1.h, DAI2.cpp, DAI2.h and main.cpp are in the /src. I can install the package in this situation by including the libDAI source files in /src on Linux. (from main.cpp: #include "DAI1.h") However, this compiles all the libDAI components during the installation. I want to switch over to utilizing libDAI.a or libDAI.so which are already compiled in the package installation. Therefore, only main.cpp would be compiled and then linked with libDAI.a etc. during the package installation.

I have tried including it through Makevars, PKGLIBS but it didnt work as it tried to compile main.cpp first and then link libDAI.a which raised the error "could not find the header file" for #include <libDAI1.h>.

I've looked at the link below but still could not come up with a solution:

Rcpp: Install package with static libraries for platform independent usage

I thought this might also solve the problem of accessibility to the package throughout different platforms (Linux, Mac and Windows) but I guess there is not a static library solution for such a singularity etc. I've also browsed through some packages but still I am puzzled.

p.s. Currently, my Makevars only includes "PKGLIBS=-lgmp -lgmpxx". It suffices to load R, an old Boost version and have "gmp" R package (or libgmp-dev?) installed in the environment to be able to install my Rcpp package.

I have tried utilizing libDAI.a and libDAI.so through including in Makevars etc. but package works flawlessly only when I include source files in /src and they are compiled for each user during the installation.

r2evans
  • 141,215
  • 6
  • 77
  • 149
nebi
  • 13
  • 4
  • You mentioned *"at the link below"*, did you forget to add it? I suspect your question here can be resolved by looking at many similar packages and how they are linking against libraries without having to compile all of it. If you have trouble finding a particular package's source package while browsing, know that https://github.com/cran/ has the most recent source version for everything released on CRAN (even if the package as since been archived for whatever reason). – r2evans Jul 06 '23 at 20:56
  • While it might be more straight-forward on linux and macos, you may find that preparing for Windows clients takes a bit more deliberate action. While I cannot advise directly on how to do that, I'll repeat myself by suggesting you look at existing packages for how they deal with linking to external libraries. Two packages (of _many_) that come to mind include `curl` and `rgeos`, I'm confident if Dirk comments here he'll have plenty of other examples under the Rcpp umbrella (since Rcpp is generally rich with documentation and examples). – r2evans Jul 06 '23 at 21:00
  • @r2evans Thank you for pointing out to the problem with the link. Added it. The thing is there is no R implementation already and the suggested approach for Linux (leave aside the Windows and Mac for now) was to include source files and compile each time which takes a while whenever an end-user would like to install my package. I mean it takes ~5 minutes now as I use this approach. Should I just give up trying to solve this by *somehow* using a precompiled libdai.a? Then, optimize it by getting rid of internal tests of libDAI after configuration etc.? (which I dont think would help a lot) – nebi Jul 06 '23 at 21:17
  • *"compile each time which takes a while whenever an end-user would like to install my package"* is a common thing with **all packages** where CRAN does not release binary packages. – r2evans Jul 06 '23 at 21:21
  • In general CRAN will _not_ let you use a precompiled external library. So you would save this only if you detected a library (assumed common enough) during build of your lighter package -- but then you "own" _all_ the difficultiies in detecting. So in short _if_ it "just works" to include files in `src/` _and_ you are legally allowed to redistribute the files _then_ that is likely what I would do in a first instance. You can always get fancier later. – Dirk Eddelbuettel Jul 06 '23 at 22:15

0 Answers0