Crt Crt - 1 month ago 16
C++ Question

Current Working Directory for Header Files C++

I am compiling some code but, I am having problems with the path to custom header files.

Here is the structure of the repository.

temp
seqLib
headerFiles
SeqLibCommon.h
src
hello_world.cpp


In the hello_world.cpp file is the following:

#include <iostream>
#include <Rcpp.h>
#include "../headerFiles/SeqLibCommon.h"


This compiles and works but, in C++ packages, the path to SeqLibCommon.h would be referenced as such:

#include "headerFiles/SeqLibCommon.h"


Why are the two different?

Answer

An R package compilation is always relative to src/ which becomes the working directory for the build.

If you want to add that directory explicitly you can add

PKG_CXXFLAGS = -I.

And if you want to add the directory inst/include/ you would have to add

PKG_CXXFLAGS = -I../inst/include

as you need to be relative to src/. You can combine both.

As for your SeqLib sources: you can move things around, or adjust your src/Makevars as needed.

Comments