I have an R script with some inline C++, and I’d like to include a header file in the same directory. I can get the following to work:
library(Rcpp)
library(inline)
code <- '
// my C++ code here
'
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS='-I /hard/coded/path/to/header/'
myfunction <- cxxfunction(signature(x="numeric"),
plugin="Rcpp", include='#include "myheader.hpp"',
settings=settings, body=code)
But it seems like there should be a convenient way to include a header file in the same directory. I just can’t see what it is. I’d really like a way to include c++ files which are located in the same directory.
(Also apologies: I’m not a c++ (or R) ninja, so I may be missing something very obvious.
Can you clarify what directory you refer to when you say “in the same directory”? Same as what?
If it is the current directory, you still need
-I.as that directory may not added as a default. But if that is indeed the directory you want, then its relative path (saying “current dir”) is more general than the absolute-I/hard/coded/path/to/header.Otherwise, what you have done is the correct way to modify the plugin to provide extra flags to the compiler.
Edit: Made a test or two and it turns out that the inline package always use R’s tempdir to compile — so
-I.makes no sense as we generally do not know where that is.That leaves you with two choices:
Use an absolute path as you have done.
Use R to read the content of the header file into a variable passed to the
include=argument.Edit 2: Turns out that we do that in one of the examples shipped with Rcpp itself:
and then uses settings=settings in the call to cxxfunction.