Use function defined in one cpp file in function defined in another cpp file in Rcpp

此生再无相见时 提交于 2019-12-06 14:34:33

When working with headers, you need to use an inclusion guard. Within the inclusion guard, provide the appropriate function definition. If your function has default parameters, include the default parameters only in the header file.

For example:

inst/include/add.h

#ifndef PACKAGENAME_ADD_H
#define PACKAGENAME_ADD_H

double add(double a = 0.1, double b = 0.2);

#endif

src/add.cpp

#include <Rcpp.h>
#include <add.h>

// [[Rcpp::export]]
double add(double a, double b) {
  return a + b;
}

src/multiplyandadd.cpp

#include <Rcpp.h>
#include <add.h>

// [[Rcpp::export]]
double multiplyandadd(double a, double b, double c) {
  return c*add(a, b);
}

src/Makevars

PKG_CPPFLAGS =  -I../inst/include/

OK. Figured it out... :)

add.h:

//add.h

#ifndef __ADD_H_INCLUDED__   // if add.h hasn't been included yet...
#define __ADD_H_INCLUDED__   //   #define this so the compiler knows it has been included

double add(double a, double b);

#endif

And, I need Makevars in src with content:

PKG_CXXFLAGS = -I../inst/include

I understand it is the most basic thing. Having such a basic example somewhere would make things easier for people first time using Rcpp.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!