1

I have an RcppEigen package that has a bunch of .h header files in it that I copy/pasted from my other library. These header files exist in two places, and right now there is only one difference between copies. In the pure c++ library, I include Eigen with #include <Eigen/Dense>. However, in the RcppEigen project, I need to use #include <RcppEigen.h>.

The problem with the naive solution--to hold on to two nearly-dentical copies of all these header files--is that I have a lot of code duplication.

To fix that I want to include the pure c++ library as a git submodule in the RcppEigen package. I would like to edit the c++ library to use conditional inclusion statements, so that they can be included in both pure c++ projects, as well as RcppEigen projects. I envision something like the following at the tops of the header files:

#ifdef DROPPINGTHISINRPACKAGE
    #include <RcppEigen.h>
#else
    #include <Eigen/Dense>
#endif

I'm confused what I should use for DROPPINGTHISINRPACKAGE, though. What is the customary way to do this in RcppEigen packages? What kinds of things are defined by the preprocessor in these kinds of packages that I can use?

Edit:

sourceCpp appears (to me) to be skipping over these flags being set in Makevars. Running Rcpp::sourceCpp(file = "~/pfexamplesinr/src/likelihoods.cpp") fails and complains that certain features are only available in c++17. It points to the spot in a header from the c++ library:

            #ifndef DROPPINGTHISINRPACKAGE
            if constexpr(debug) 
                std::cout << "time: " << m_now << ", transposed sample: " << m_particles[ii].transpose() << ", log unnorm weight: " << m_logUnNormWeights[ii] << "\n";
            #endif
Taylor
  • 1,797
  • 4
  • 26
  • 51
  • 1
    A fairly common trick is to define 'something' in `src/Makevars{,.win}` to indicate the 'from R' compilation -- your first case. – Dirk Eddelbuettel Feb 25 '23 at 01:09
  • 1
    So to be plain: add a line `PKG_CPPFLAGS=-DDROPPINGTHISINRPACKAGE` and you should be done. – Dirk Eddelbuettel Feb 25 '23 at 01:15
  • @DirkEddelbuettel does Makevars get skipped over when I compile with `Rcpp::sourceCpp()`? I didn't notice the flag pop up when I was looking at the build commands, and my attempt to hide c++17 code with `#ifndef DROPPINGTHISINRPACKAGE` failed – Taylor Feb 25 '23 at 03:47
  • Yes of course it does. `sourceCpp()` is for one-offs, _has its manual page telling you how to se a `#define` too_ and it is generally well-established that a reall 'project' (your term, from you subject line) should be a package. – Dirk Eddelbuettel Feb 25 '23 at 12:26

0 Answers0