15

Although a few solutions exist on the internet, I found none of those suitable for the problem I'm curerntly facing (though maybe I'm simply too dumb):

I'm trying to build an R package which makes extensive use of a shared object compiled by a Makefile (yes, bad practice, I know, but a Makevars file just can't be told to compile C and Fortran code into one shared object) from code in the package's src directory. No matter where I compile that .so to (I tried the src, libs and package base folders) or how I name it (as one of the solutions mentioned above states it must be named like the package it's contained in), R CMD check exits with

** testing if installed package can be loaded
Error in library.dynam(lib, package, package.lib) : 
shared object ‘SoMNibEN.R.so’ not found

due to the useDynLib(SoMNibEN.R) instruction in my NAMESPACE file (where SoMNibEN.R is my package's name, but it didn't work with the original name, either)

My assumption is that I'm either using that useDynLib() command wrong or I'm doing something wrong with my Makefile (although the compilation works pretty well and the shared object is created in my project folder - I just don't know whether it gets copied to the package installation directory successfully).

So, if anyone knows what I might be doing wrong here, please let me know!

Sty
  • 760
  • 1
  • 9
  • 30

1 Answers1

10

You want the name of the package as the argument, as that is the name of the shared object built by R, eg useDynLib("chron"). The quotes are optional (as they are for library() etc).

I also recommend not using a Makefile, but simply dropping the C and Fortran files in the src/ directory. R is generally smart enough to know what to. If you need -I etc switches you can set them there.

Lastly, use CRAN. There are hundreds of packages with compiled sources, and some are bound to be similar in structure to your question.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Actually, 'generally smart enough' is somewhat misleading. After removing the Makefile and renaming one of my files so that it is compiled earlier (due to the alphabetical order of files in my `src/` directory), my package compilation succeeded. I wouldn't call "compiling all files in `src/` in alphabetical order" _smart_, though. Thanks for helping again, anyway. – Sty Oct 22 '12 at 12:20
  • Since when does compile order matter? Linker does at times, but not cipiler. Anyway... – Dirk Eddelbuettel Oct 22 '12 at 12:22
  • Obviously it does in my program... it consists of two F90 files, one originally being a configutaion script and the other one the actual calculation - which uses a handful variables from the configuration script, though. That results in the compiler complaining about a missing .mod file from the configuration script, in case it hasn't been compiled before. I honestly don't know enough Fortran to code this stuff in a way that both files may be compiled in any order. – Sty Oct 23 '12 at 11:34
  • Ack -- that has no correspondence in the C / C++ languages I am more familiar with. In that case, you want these dependencies declared in a file Makevars, but do not provide all the targets already filled in by R. This is not so well documented. If in doubt, ask on r-devel. – Dirk Eddelbuettel Oct 23 '12 at 11:42