Structuring a package embedding a C++ library containing cpp and hpp files

I'm working on exposing a C++ library that contains cpp and corresponding hpp files (essentially 1 hpp per cpp file + meta hpp files that include other hpp files).

I'm wondering what the best / "most accepted" way to structure this is. My current setup is:

pkg/
| inst/
   | include/
      | cpp-lib-file.hpp
| src/
   | Makevars
   | my-rcpp-wrapper.cpp
   | cpp-lib-file.cpp

Where Makevars contains:
PKG_CXXFLAGS = -I../inst/include

This works for me, but I kind of hate having the library cpp files and my rcpp wrapper files at the same level. Ideally I could do:

pkg/
| inst/
   | include/
      | cpp-lib-file.hpp
| src/
   | Makevars
   | my-rcpp-wrapper.cpp
   | cpp-lib/
      | cpp-lib-file.cpp

I assume this is possible, and that I'm just not telling Makevars the right thing? I've tried a few adjustments but can't get it right. Anyone have ideas?

Edit) I've also stack-overflow-searched this to death and can't find a good answer, or I've just missed it.

1 Like

Generally I would suggest putting all of the library code in a subdirectory under src/.

pkg
  src
    Makevars
    R_pkg_specific_code.cc
      vender-code/
        vender-src.cc

See https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Compiling-in-sub_002ddirectories for how to setup your Makevars to do this (assuming the vendor library has a suitable makefile you can call).

Thanks! This was helpful. In particular it (indirectly) points to the Rcsdp pkg that does this which I can use as an example.

And for anyone else that comes to this post, the R-extensions section on Makevars is a must read to understand any of the relevant files in Rcsdp.

2 Likes