链接到外部 C 库的 R 包 [英] R package that links to external C library

查看:38
本文介绍了链接到外部 C 库的 R 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用 igraph 库的 c 代码.我想在它周围放一个 R 包装器,然后将它作为 R 包发送给 CRAN.

I have some c code that utilizes the igraph library. I would like to put an R wrapper around it and send it off to CRAN as an R package.

igraph 已经在 CRAN 上有一个 R 端口,所以我的 R 包 'foo' 依赖于 R 的 igraph 是有意义的.由于 foo 使用它自己的依赖于 C igraph 的 C 代码,我如何将我的 C 函数链接到原始 igraph 库?我读到这是在一个名为 Makevars 的文件中完成的,但是链接到外部库非常麻烦.

igraph already has an R port on CRAN, so it would make sense for my R package 'foo' to depend on R's igraph. Since foo uses its own C code that depends on the C igraph, how can I link my C functions to the original igraph library? I've read that this is done in a file called Makevars, but linking to an external library is very hairy.

如果这是不可能的,是否最好只复制整个 igraph 源代码并将整个内容放入我的/src 目录中?R igraph 包已经有一个名为 Makevars 的文件,但我不明白所有 c 文件是如何构建的 - 通常在我的 Makefile 中,我有类似 gcc(一些 .c 源文件列表)-o 之类的东西,但 Makevar 只包含

If this isn't possible, is it better to just copy the entire igraph source code and put the whole thing into my /src directory? The R igraph package already has a file called Makevars, but I don't understand how all the c files get built - normally in my Makefile I have something like gcc (some list of .c source files) -o, but Makevar only contains

PKG_CFLAGS=-DUSING_R -I. -Ics -Iglpk -Iglpk/amd -Iglpk/colamd 
-g -O2 -I/usr/include/libxml2 -g -O2 -I/usr/include/libxml2 -DNDEBUG 
-DPACKAGE_VERSION="0.6" -DINTERNAL_ARPACK 
-DIGRAPH_THREAD_LOCAL=/**/
PKG_CXXFLAGS= -DUSING_R -DIGRAPH_THREAD_LOCAL=/**/ -DNDEBUG
PKG_LIBS=-lxml2 -lz -lpthread -licucore -lm -lgmp  $(FLIBS) $(LAPACK_LIBS) $(BLAS_LIBS)

all: $(SHLIB)

并且没有其他 Makefile.总之,如何将 C 代码放入依赖于另一个 C 库的 R 包中,以及如何编写相应的 Makevar(或 Makefile)以合并 C 函数?

and there is no other Makefile. In summary, how do I go about putting C code into an R package that depends on another C library, and how do I write the corresponding Makevars (or Makefile) to incorporate the C functions?

发布了一个较旧的问题 此处 但似乎只是链接以帮助您编写不依赖任何东西的自己的 C 代码.

An older question was posted here but only seems to link to help on writing your own C code that does not depend on anything.

推荐答案

这里有几个问题:

  1. 包foo"能否可靠地链接到包bar"?正如 Gabor 在他之前的评论中所暗示的那样,编写 R 扩展",在第 5.8 节链接到其他包"的开头说不,不是一般的".

  1. Can a package 'foo' reliably link to a package 'bar'? "Writing R Extensions", at the beginning of Section 5.8 on "Linking to Other Packages" says "no, not generally" as Gabor hinted too in his earlier comment.

能否将 C 源代码放入一个包中以构建一个依赖于另一个库的包:是的,当然,CRAN 上的许多包都这样做.例如,根据 GSL、JPEG/TIFF 图形库、XML 或……选择一个包示例,您可以研究这些来源.这并非易事,但如果您研究这些包、编写 R 扩展中的文档以及此处相关问题的其他答案,您应该会有所收获.

Can C source code put into a package to build a package that depends on another libary: Yes, of course, and lots of packages on CRAN do it. Pick eg an example of a package depending on the GSL, or the JPEG/TIFF graphics libraries, or XML, or ... You can study these sources. That is not trivial easier, but if you study these packages, the documentation in Writing R Extensions and the other answers to related questions here you should get there.

这篇关于链接到外部 C 库的 R 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆