简体   繁体   English

Rcpp:将包中的标头包含到cppFunction中

[英]Rcpp: Include header from package into cppFunction

I have a package that uses Rcpp . 我有一个使用Rcpp的软件包。 It has different C++ function defined int src/testr.h . 它具有int src/testr.h定义的不同C++函数。 Now at some point I need to generate Rcpp functions (using cppFunction or cxxfunction ) that use things defined in src/testr.h . 现在在某个时候,我需要生成Rcpp函数(使用cppFunctioncxxfunction ), Rcpp函数使用src/testr.h定义的东西。 How can I do that? 我怎样才能做到这一点?

I tried, 我试过了,

> cxxfunction(signature(x="list") includes = c('#include "testr.h"'), body=sprintf(template, name, name))
> Rcpp::cppFunction(depends = "testr", includes = c('#include "src/testr.h"'), sprintf(template, name, name))

But

file7086270f0da.cpp:7:10: fatal error: 'testr.h' file not found
#include "testr.h"

     ^      

Quick suggestion: 快速建议:

  1. Move the header to inst/include/testr.h ; 将标题移至inst/include/testr.h ;
  2. This requires a src/Makevars entry such as PKG_CPPFLAGS = -I../inst/include to have the compilation look there. 这需要一个src/Makevars条目(例如PKG_CPPFLAGS = -I../inst/include才能使编译看起来在那里。
  3. Now with the package installed as, say, mypkg , you can add a depends="mypkg" and R will know to set a -I... for you. 现在安装了软件包,例如mypkg ,您可以添加depends="mypkg" ,R将知道为您设置-I...

We use the same trick all the time in eg BH and various Rcpp* packages. 我们一直在例如BH和各种Rcpp*软件包中使用相同的技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM