Reputation: 1041
I have a header file called coolStuff.h
that contains a function awesomeSauce(arg1)
that I would like to use in my cpp source file.
Directory Structure:
The Code:
#include <Rcpp.h>
#include <cppHeaders/coolStuff.h>
using namespace Rcpp;
// [[Rcpp::export]]
double someFunctionCpp(double someInput){
double someOutput = awesomeSauce(someInput);
return someOutput;
}
I get the error:
theCppFile.cpp:2:31: error: cppHeaders/coolStuff.h: No such file or directory
I have moved the file and directory all over the place and can't seem to get this to work. I see examples all over the place of using 3rd party headers that say just do this:
#include <boost/array.hpp>
(Thats from Hadley/devtools)
https://github.com/hadley/devtools/wiki/Rcpp
So what gives? I have been searching all morning and can't find an answer to what seems to me like a simple thing.
Ok now that I have figured out how to build packages that use Rcpp in Rstudio let me rephrase the question. I have a stand alone header file coolStuff.h that contains a function I want to use in my cpp code.
1) Where should I place coolStuff.h in the package directory structure so the function it contains can be used by theCppFile.cpp?
2) How do I call coolStuff.h in the cpp files? Thanks again for your help. I learned a lot from the last conversation.
Note: I read the vignette "Writing a package that uses Rcpp" and it does not explain how to do this.
Ok let me summarize the answer to my question since it is scattered across this page. If I get a detail wrong feel free to edit this or let me know and I will edit it:
So you found a .h
or .cpp
file that contains a function or some other bit of code you want to use in a .cpp
file you are writing to use with Rcpp
.
Lets keep calling this found code coolStuff.h
and call the function you want to use awesomeSauce()
. Lets call the file you are writing theCppFile.cpp
.
(I should note here that the code in .h files and in .cpp files is all C++ code and the difference between them is for the C++ programer to keep things organized in the proper way. I will leave a discussion of the difference out here, but a simple search here on SO will lead you to discussion of the difference. For you the R programer needing to use a bit o' code you found, there is no real difference.)
IN SHORT: You can use a file like coolStuff.h
provided it calls no other libraries, by either cut-and-pasteing into theCppFile.cpp
, or if you create a package you can place the file in the \src
directory with the theCppFile.cpp
file and use #include "coolStuff.h"
at the top of the file you are writing. The latter is more flexible and allows you to use functions in coolStuff.h
in other .cpp
files.
DETAILS:
1) coolStuff.h
must not call other libraries. So that means it cannot have any include statements at the top. If it does, what I detail below probably will not work, and the use of found code that calls other libraries is beyond the scope of this answer.
2) If you want to compile the file with sourceCpp()
you need to cut and paste coolStuff.h
into theCppFile.cpp
. I am told there are exceptions, but sourceCpp()
is designed to compile one .cpp
file, so thats the best route to take.
(NOTE: I make no guarantees that a simple cut and paste will work out of the box. You may have to rename variables, or more likely switch the data types being used to be consistent with those you are using in theCppFile.cpp
. But so far, cut-and-paste has worked with minimal fuss for me with 6 different simple .h
files)
3) If you only need to use code from coolStuff.h
in theCppFile.cpp
and nowhere else, then you should cut and paste it into theCppFile.cpp
.
(Again I make no guarantees see the note above about cut-and-paste)
4) If you want to use code contained in coolStuff.h
in theCppFile.cpp
AND other .cpp
files, you need to look into building a package. This is not hard, but can be a bit tricky, because the information out there about building packages with Rcpp ranges from the exhaustive thorough documentation you want with any R package (but that is above your head as a newbie), and the newbie sensitive introductions (that may leave out a detail you happen to need).
Here is what I suggest:
A) First get a version of theCppFile.cpp
with the code from coolStuff.h
cut-and-paste into theCppFile.cpp
that compiles with sourceCpp()
and works as you expect it to. This is not a must, but if you are new to Rcpp OR packages, it is nice to make sure your code works in this simple situation before you move to the more complicated case below.
B) Now build your package using Rcpp.package.skeleton()
or use the Build functionality in RStudio (HIGHLY recommended). You can find details about using Rcpp.package.skeleton()
in hadley/devtools or Rcpp Attributes Vignette. The full documentation for writing packages with Rcpp is in the Writing a package that uses Rcpp, however this one assumes you know your way around C++ fairly well, and does not use the new "Attributes" way of doing Rcpp.
Don't forget to "Build & Reload" if using RStudio or compileAttributes()
if you are not in RStudio.
C) Now you should see in your \R directory a file called RcppExports.R
. Open it and check it out. In RcppExports.R
you should see the R wrapper functions for all the .cpp files you have in your \src
directory. Pretty sweet.
D) Try out the R function that corresponds to the function you wrote in theCppFile.cpp
. Does it work? If so move on.
E) With your package built you can move coolStuff.h
into the src
folder with theCppFile.cpp
.
F) Now you can remove the cut-and-paste code from theCppFile.cpp
and at the top of theCppFile.cpp
(and any other .cpp file you want to use code from coolStuff.h) put #include "coolStuff.h"
just after #include <Rcpp.h>
. Note that there are no brackets around ranker.h, rather there are "". This is a C++ convention when including local files provided by the user rather than a library file like Rcpp or STL etc...
G) Now you have to rebuild the package. In RStudio this is just "Build & Reload" in the Build menu. If you are not using RStudio you should run compileAttributes()
H) Now try the R function again just as you did in step D), hopefully it works.
Upvotes: 44
Views: 12799
Reputation: 2026
This worked for me in Windows:
Sys.setenv("PKG_CXXFLAGS"='-I"C:/boost/boost_1_66_0"')
Edit: Actually you don't need this if you use Boost Headers (thanks to Ralf Stubner):
// [[Rcpp::depends(BH)]]
Upvotes: 1
Reputation: 8803
We can add it by writing path to the header in the PKG_CXXFLAGS
variable of the .R/Makevars
file as shown below. The following is an example of adding header file of xtensor
installed with Anaconda in macOS.
⋊> ~ cat ~/.R/Makevars
CC=/usr/local/bin/gcc-7
CXX=/usr/local/bin/g++-7
CPLUS_INCLUDE_PATH=/opt/local/include:$CPLUS_INCLUDE_PATH
PKG_CXXFLAGS=-I/Users/kuroyanagi/.pyenv/versions/miniconda3-4.3.30/include
LD_LIBRARY_PATH=/opt/local/lib:$LD_LIBRARY_PATH
CXXFLAGS= -g0 -O3 -Wall
MAKE=make -j4
Upvotes: 1
Reputation: 41
I was able to link a boost library using the following global command in R before calling sourceCpp
Sys.setenv("PKG_CXXFLAGS"="-I \path-to-boost\")
Basically mirroring this post but with a different compiler option: http://gallery.rcpp.org/articles/first-steps-with-C++11/
Upvotes: 4
Reputation: 81
I was able to link any library (MPFR in this case) by setting two environment variables before calling sourceCpp:
Sys.setenv("PKG_CXXFLAGS"="-I/usr/include")
Sys.setenv("PKG_LIBS"="-L/usr/lib/x86_64-linux-gnu/ -lm -lmpc -lgmp -lmpfr")
The first variable contains the path of the library headers. The second one includes the path of the library binary and its file name. In this case other dependent libraries are also required. For more details check g++ compilation and link flags. This information can usually be obtained using pkg-config:
pkg-config --cflags --libs mylib
For a better understanding, I recommend using sourceCpp with verbose output in order to print the g++ compilation and linking commands:
sourceCpp("mysource.cpp", verbose=TRUE, rebuild=TRUE)
Upvotes: 8
Reputation: 351
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
); or
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).
Upvotes: 25
Reputation: 368201
Couple of things:
"Third party header libraries" as in your subject makes no sense.
Third-party headers can work via templated code where headers are all you need, ie there is only an include step and the compiler resolves things.
Once you need libraries and actual linking of object code, you may not be able the powerful and useful sourceCpp
unless you gave it meta-information via plugins (or env. vars).
So in that case, write a package.
Easy and simple things are just that with Rcpp and the new attributes, or the older inline and cxxfunction
. More for complex use --- and external libraries is more complex you need to consult the documentation. We added several vignettes to Rcpp for that.
Upvotes: 2
Reputation: 16582
Angle brackets <> are for system includes, such as the standard libs.
For files local to your own project, use quotes: "".
Also, if you are placing headers in a different directory, the header path should be specified local to the source file including it.
So for your example this ought to work:
#include "../cppHeaders/coolStuff.h"
You can configure the search paths such that the file could be found without doing that, but it's generally only worth doing that for stuff you want to include across multiple projects, or otherwise would expect someone to 'install'.
Upvotes: 1