简体   繁体   中英

use dyn.load or library.dynam to call a C++ function in an R package

I am very new to R. I would like to build an R package which will call a C++ function using .Call(). I have a NAMESPACE file, with

useDynLib(mypkg)

where mypkg is also the function name of my c++ code.

It works if I use this line at the begining of the mypkg.R file:

dyn.load("src/mypkg.so")

but I want to use library.dynam instead, so in the zzz.R file, I put

.onLoad<-function(libname, pkgname)
 {
 library.dynam("mypkg", pkgname, libname)
 }

It gives the error when checking the package:

...
Error in .Call("mypkg", PACKAGE = "mypkg") : 
C symbol name "mypkg" not in DLL for package "mypkg".
Error : unable to load R code in package 'mypkg'
...

It looks like the *.so file is generated in the wrong place? Why there is not /libs folder generated?

I would like to build the package to be os independent, is there a way to do it with dyn.load?

And this may be a very silly question, where did pkgname and libname get their input from?

Thank you very much for your help.

You could look at one of the many existing packages (with compiled source code) on CRAN .

Smaller and simpler is easier to grok, so you could eg look at digest which uses a NAMESPACE to load the one shared library built from the handful of C source files, and uses .Call() to access the main entry point.

And then there is of course the manual...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM