tags:

views:

75

answers:

3

Working in Windows, I've created an r package that links to a c++ dll as a shared library. This works fine and installs without problems on Windows. When I switch to linux, however, the so is not found.

Am I right in thinking that the only file in the src directory should be the .cpp file?

Do I actually need to run the SHLIB command in that directory before I create the package?

In the NAMESPACE I use:

useDynLib(myc.cpp,my.c.function)

and in the function call:

my.r.f <- .Call(my.c.function, a, b)

On windows running R CMD check works fine. Could it be my linux R configuration that is to blame? It seems to install 3rd party packages fine.

I'm stumped!

A: 

I think you should just use useDynLib(myc)... The symbol lookup is done internally.
EDIT: The other thing is the name of this object file -- I think the standard makefile just names it with package name, so it should be rather useDynLib(<package name>). At least it always works for me.

mbq
Adding `PACKAGE="my.package.name"` to each `.Call` will avoid the symbol lookup for each function call.
Joshua Ulrich
Are you sure? I thought it is just to avoid name conflicts, while R optimizes lookup with some dictionary that is updated depending on the platform quirks and dyn.load arguments. But I am not sure at all.
mbq
"symbol lookup" is probably the wrong phrase. I don't remember the exact behavior, but including the `PACKAGE` argument prevented a lookup that drastically increased the speed repeated calls to `.Call`. It could have been `getCallingDLL` or `getDLLRegisteredRoutines`...
Joshua Ulrich
@Joshua If you say so... I'll be making some benchmarks of a new package soon, I'll check that also.
mbq
+1  A: 

There are several hundred packages on CRAN which do successfully what you attempt to do -- build a package with to-be-compiled sources on any of the supported platforms.

A strategy I quite like is to take one or more existing package and look exactly how they are set up. You can then copy the working recipe depending on how it corresponds to your setup (with or without NAMESPACE, with or without external library like libxml etc pp_)

Dirk Eddelbuettel
A: 

I took Dirk's advice and browsed a few packages on CRAN.

A common approach appears to be to use the useDynLib(package_name) as suggested by mbq in the NAMESPACE file. I then used the call:

.Call("my.c.function", a, b, package="package_name")

in the R code as suggested by Joshua.

Now installs and works fine on Linux and Windows :-)

I think I will soon switch to Rcpp as the in-R compilation and package building skeleton tools look very enticing.

Thanks all!

Chris Wheadon