Creating a r package with a C++ dll in windows and ensuring portability to linux
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 .cp开发者_如何学Cp 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!
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_)
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.
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!
精彩评论