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:
In the system include directories (i.e.
/usr/local/lib
or/usr/lib
); orIn 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:
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:
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
Matlab VS C++ Double Precision
Boost.Asio-Based Http Client Library (Like Libcurl)
What Is the Meaning of Clang's -Wweak-Vtables
C++ Socket Server - Unable to Saturate Cpu
Multiple Definition Error Including C++ Header File with Inline Code from Multiple Sources
When Is a Function Try Block Useful
When Is a Type in C++11 Allowed to Be Memcpyed
How Typedef Works for Function Pointers
How to Rearrange a String Equation
What Is the Easiest Way to Make a C++ Program Crash
Can MACros Be Overloaded by Number of Arguments
What Is the Past-The-End Iterator in Stl C++
Convert C++ Function Pointer to C Function Pointer