user3194934
user3194934

Reputation: 57

How to include and link with sourceCpp in R Rcpp

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:

  1. I have a raw c program consisting of a .h file and and .c file

headerfile: add.h

int add(int a,int b);

cfile: add.c

int add(int a,int b){
  return a+b;
}
  1. An 'interface' c++ function that should connect my R call with the functionality in my c file

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;
}
  1. The Rcode that tries to use the 'monkey' function

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

Answers (1)

Dirk is no longer here
Dirk is no longer here

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

Related Questions