I took this example from a different question. I am building an R package with Rcpp. I have a function like fun1
(below) that I want to put into its own .cpp
file. Then I want to call fun1
with other functions (like fun()
does below). I want fun1
in a separate file because I am going to call it from several Rcpp functions that are in different .cpp
files. Are there certain include statements and things I need to do to make the fun1
function accessible in the .cpp
where fun()
is located? Thank you.
library(inline)
library(Rcpp)
a = 1:10
cpp.fun = cxxfunction(signature(data1="numeric"),
plugin="Rcpp",
body="
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
")
So for my code I will have two .cpp
files:
#include <Rcpp.h>
using namespace Rcpp;
// I think I need something here to make fun1.cpp available?
// [[Rcpp::export]]
Rcpp::NumericVector fun(Rcpp::NumericVector data1)
{
NumericVector fun_data = data1;
int n = data1.size();
for(i=0;i<n;i++){
fun_data[i] = fun1(fun_data[i]);
}
return(fun_data);
}
And a second .cpp
file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int fun1( int a1)
{int b1 = a1;
b1 = b1*b1;
return(b1);
}