使用 Rcpp 在 R 包中添加外部库
Adding an external library in R package using Rcpp
我正在尝试开发一个 R 包,它使用 Sundials C 库来求解微分方程。为了不让用户安装库,我把库的源代码放在我的包里。
我已将库中的所有头文件放在 /inst/include/sundials-2.6.2
中,将 .c
文件放在包文件夹的 src/sundials-2.6.2
中。
根据我对有关该主题的 SO 帖子的阅读,多个文件中的 sourceCpp
代码(例如,单独的 .h
和 .cpp
文件如果被构造为包的一部分。我正在尝试 运行 日晷包中的示例代码文件
我的代码(只有开头部分)看起来像
#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
程序。
谢谢
序列号
外部库链接应使用以下设置完成:
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
Makevars
和 Makevars.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
我正在尝试开发一个 R 包,它使用 Sundials C 库来求解微分方程。为了不让用户安装库,我把库的源代码放在我的包里。
我已将库中的所有头文件放在 /inst/include/sundials-2.6.2
中,将 .c
文件放在包文件夹的 src/sundials-2.6.2
中。
根据我对有关该主题的 SO 帖子的阅读,多个文件中的 sourceCpp
代码(例如,单独的 .h
和 .cpp
文件如果被构造为包的一部分。我正在尝试 运行 日晷包中的示例代码文件
我的代码(只有开头部分)看起来像
#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
程序。
谢谢 序列号
外部库链接应使用以下设置完成:
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
Makevars
和 Makevars.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