Reputation: 57
I've been trying to extend the functionality of some of my c/c++ code into R using the Rcpp package.
But I'm having problems with the inclusion of headers and linking
I've made the following example that illustrates my problem:
headerfile: add.h
int add(int a,int b);
cfile: add.c
int add(int a,int b){
return a+b;
}
monkey.cpp
#include <cstdio>
#include <Rcpp.h>
#include "add.h"
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector monkey(std::string string1,std::string string2) {
//below not important
NumericVector rary(5);
for(int i=0;i<5;i++)
rary[i] = i;
return rary;
}
monkey.R
nam1 <- "nam1"
nam2 <- "nam2"
Rcpp::sourceCpp("monkey.cpp")
monkey(nam1,nam2)
q1. Is it possible to specify -I flags for the gcc compilation with the sourceCpp function?
q2. If my interface cpp file is dependant on other .o files, is it possible to link with these using the sourceCpp function?
Upvotes: 1
Views: 1320
Reputation: 368181
Quick ones:
The sourceCpp()
is for small test applications; it can use a set of extensions which provide plugins for extensions.
So you may need to write a custom plugin for your header file, and that may be overkill because ...
More complex code organization in R is typically done via a package. You should consider it.
Section 3.5 in the Rcpp Attributes vignette has all your options.
Upvotes: 2