编写 R 包时加载动态链接库(即 .so
文件)的正确方法是什么?到目前为止,唯一对我有用的解决方案是指定 .so
文件的完整路径,例如:
dyn.load('/FULL/PATH/TO/MY/R_PACKAGE/src/my_file.so')
显然,这种方法不适用于 CRAN/Bioconductor 提交,因为无法找到 .so 文件。因此,我(但没有成功)尝试了以下替代方案:
1) library.dynam()
2) library.dynam('my_file.so')
3) library.dynam('my_file.so', 'R_PACKAGE')
4) system.file("src", "my_file.so", package = "R_PACKAGE")
相关链接:R: C symbol not in load table , R: C symbol name not in load table - error in linking with external .c files .
为了清楚起见,我的 R 包的用户显然可以在他们的计算机上设置任意工作目录。完整路径方法(如上所示)工作的唯一方法是将工作目录设置为/FULL/PATH/TO/MY/R_PACKAGE/src ,这(当然)是不切实际的.
最佳答案
执行此操作的标准方法,如 Writing R Extensions 中所述,是:
1.5.4 useDynLib
A
NAMESPACE
file can contain one or moreuseDynLib
directives which allows shared objects that need to be loaded.* The directive
useDynLib(foo)
registers the shared object
foo
** for loading withlibrary.dynam
. Loading of registered object(s) occurs after the package code has been loaded and before running the load hook function. Packages that would only need a load hook function to load a shared object can use theuseDynLib
directive instead.
*
NB: this will only be read in all versions of R if the package contains R code in a R directory.**
Note that this is the basename of the shared object, and the appropriate extension (.so or .dll) will be added.
关于c - 设置 R 包中动态链接库的目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354959/