Using 3Rd Party Header Files with Rcpp

Using 3rd party header files with Rcpp

The problem is that sourceCpp is expressly designed to build only a single standalone source file. If you want sourceCpp to have dependencies then they need to either be:

  1. In the system include directories (i.e. /usr/local/lib or /usr/lib); or

  2. In an R package which you list in an Rcpp::depends attribute

As Dirk said, if you want to build more than one source file then you should consider using an R package rather than sourceCpp.

Note that if you are working on a package and perform a sourceCpp on a file within the src directory of the package it will build it as if it's in the package (i.e. you can include files from the src directory or inst/include directory).

How can I include a local header file in Rcpp inline, without hardcoding the directory?

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:

  1. Use an absolute path as you have done.

  2. 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:

settings <- getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS <- paste("-I", getwd(), sep="")

and then uses settings=settings in the call to cxxfunction.

Building an R package with Rcpp which contains C source and header with restrict qualifier?

It sounds like you are making a .cpp file which does #include <x.h> where x.h is a C header which uses restrict. If that's true, I think you can modify your .cpp file to do this:

#define restrict // nothing
extern "C"
{
#include <x.h>
}

Then compilation of your C++ code will not see the restrict keyword, and also I have wrapped the header in extern "C" because if the header itself doesn't do that internally, you need to, in order that your C++ compiler will not apply C++ "name mangling" to the functions declared inside.



Related Topics



Leave a reply



Submit