1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.