调用“引用了外部库的多文件C ++代码".在R中 [英] Call "multi file C++ code with external libraries referenced" in R
问题描述
我已经用C ++编写了一个代码,该代码使用了boost库并且还使用了多个文件.我正在尝试在R中执行该功能.
I have written a code in C++ which uses boost library and also uses multiple files. I am trying to execute the function in R.
这是我的C ++函数原型:
Here is my C++ function prototype:
extern "C" double function1(double rho, double mu, double limit);
这是我的R函数:
dyn.load('path_to_dll\\x64\\Debug\\project.dll')
.Call("function1", as.double(1.0), mu = as.double(1.0/20.0), limit = as.double(3413.85))
当我调用函数时,它给我以下错误:
When I am calling the function, it gives me following error:
Error in .Call("function1", as.double(1), mu = as.double(1/20), :
C symbol name "function1" not in load table
我已遵循此但是没有运气.我要去哪里错了?
I have followed this But no luck. Where am I going wrong?
推荐答案
由于这是一个多文件C ++项目,因此应将代码打包为R包.使用Rcpp实际上很容易,请参见例如此答案. Rcpp-package 插图包含更多信息.另外,使用 boost
的一种简单方法是通过 BH软件包.您可以研究很多在CRAN上使用Rcpp + BH的示例程序包,例如我自己的 dqrng .您还可以在 R包以及官方文档中找到更多详细信息.编写R扩展.
Since this a multi-file C++ project, you should package the code as an R package. It is actually quite easy to do using Rcpp, see for example this answer. The Rcpp-package vignette contains further information. In addition, an easy way to make use of boost
is via the BH package. There are many example packages for using Rcpp + BH on CRAN which you can study, e.g. my own dqrng. Some more details can also be found in the R Packages book and of course in the official documentation Writing R Extensions.
使用BH软件包的关键是 DESCRIPTION
文件中的 LinkingTo:BH
.这确保了 -I< path_to_BH_package>/include
是编译器标志的一部分.为了使用放置在 inst/include
中的头文件,应将 PKG_CPPFLAGS = -I ../inst/include
添加到 src/Makevars 代码>.
The crucial thing for using the BH package is LinkingTo: BH
in the DESCRIPTION
file. This ensures that -I<path_to_BH_package>/include
is part of the compiler flags. In order to use header files that you place in inst/include
, you should add PKG_CPPFLAGS = -I../inst/include
to src/Makevars
.
这篇关于调用“引用了外部库的多文件C ++代码".在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!