gpt4 book ai didi

rcpp - 使用 Rcpp 在 R 包中添加外部库

转载 作者:行者123 更新时间:2023-12-04 23:40:56 30 4
gpt4 key购买 nike

我正在尝试开发一个使用 Sundials 的 R 包用于求解微分方程的 C 库。为了不让用户安装库,我将库的源代码放在我的包中。

我已将库中的所有头文件放入 /inst/include/sundials-2.6.2.c src/sundials-2.6.2 中的文件我的包文件夹。

从我阅读有关此主题的 SO 帖子来看,sourceCpp多个文件中的代码(例如,单独的 .h.cpp 文件应该可以工作,如果它们被构造为包的一部分。我正在尝试从 Sundials 包中运行示例代码文件

我的代码(只有开始部分)看起来像

#include <Rcpp.h>

#include "../inst/include/sundials-2.6.2/cvode/cvode.h" /* prototypes for CVODE fcts., consts. */
#include "../inst/include/sundials-2.6.2/nvector/nvector_serial.h" /* serial N_Vector types, fcts., macros */
#include "../inst/include/sundials-2.6.2/cvode/cvode_dense.h" /* prototype for CVDense */
#include "../inst/include/sundials-2.6.2/sundials/sundials_dense.h" /* definitions DlsMat DENSE_ELEM */
#include "../inst/include/sundials-2.6.2/sundials/sundials_types.h" /* definition of type realtype */

但是,我收到一个错误
fatal error: sundials/sundials_nvector.h: No such file or directory

我在以下 github 存储库中做了类似的事情的例子

Rcppsundials - https://github.com/AleMorales/RcppSundials.R/blob/master/src/cvode.cpp

它使用调用头文件
#include <cvodes/cvodes.h>           // CVODES functions and constants
#include <nvector/nvector_serial.h> // Serial N_Vector
#include <cvodes/cvodes_dense.h> // CVDense

并在 /inst/include/下合并了头文件文件夹。

这是我尝试开发的第一个包,而且我还没有广泛使用 C/C++,所以在我尝试编译这个程序的过程中可能会有一些非常愚蠢的事情。

只是一个旁注 - 我能够在我的 OSX 机器上安装和运行一个示例,但目前我正在一台没有安装日晷的 Windows 机器上工作。它确实有 Rtools安装,所以我可以编译和运行示例 Rcpp程式。

谢谢
SN

最佳答案

外部库链接应通过以下设置完成:

R/
inst/
|- include/
|- sundials/
|- header.h
src/
|- sundials/
|- Makevars
|- Makevars.win
|- action.cpp
man/
DESCRIPTION
NAMESPACE

然后添加以下内容:
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_CPPFLAGS = -I../inst/include/ -I src/sundials

MakevarsMakevars.win
在这里,我选择从文件夹名称中删除日晷版本号。

编辑

我已经进行了编译包所需的修复:

https://github.com/sn248/Rcppsbmod/pull/1

笔记:

结构是:
inst/
|- include/
|- sundials/
|- arkode/
.....
|- nvector/
|- sundials/
|- header.h

这将迫使包含语句:
#include <sundials/cvodes/cvodes.h>           // CVODES functions and constants
#include <sundials/nvector/nvector_serial.h> // Serial N_Vector
#include <sundials/cvodes/cvodes_dense.h> // CVDense

我改变了它,以便:
inst/
|- include/
|- arkode/
.....
|- nvector/
|- sundials/
|- header.h

因此,语句将始终是:
#include <cvodes/cvodes.h>           // CVODES functions and constants
#include <nvector/nvector_serial.h> // Serial N_Vector
#include <cvodes/cvodes_dense.h> // CVDense

关于rcpp - 使用 Rcpp 在 R 包中添加外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37753149/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com