Search code examples
rrcpp

R's Makevars: PKG_CXXFLAGS vs. PKG_CXX11FLAGS


When creating an R package with a compiled C++ extension, one can specify the C++ standard and flags to use to compile it in a file src/Makevars - for example:

CXX_STD = CXX11
PKG_CXXFLAGS = -fmyflag

Although the R build system has variables used in compilation such as CXX (the C++ compiler) and PKG_CXXFLAGS (flags to pass to the C++ compiler), it also has standard-specific flags such as CXX11/CXX14 and PKG_CXX11FLAGS/PKG_CXX14FLAGS.

The R Extensions Manual does not say much about those other flags, but it does mention that the current default standard is C++11 and thus CXX == CXX11, and that different compilers might be used for different C++ standards. Some of the notes also mention that PKG_CXXFLAGS might in some situations only apply to the default CXX compiler.

If I am creating a package in which I define an src/Makevars and I specifically require it to compile as C++11:

CXX_STD = CXX11

Should I specify the flags through PKG_CXXFLAGS, or should I use instead PKG_CXX11FLAGS, or both? Is is portable/good-practice to use standard-specific flags?

Right now I assume it shouldn't make any difference but the default standard might change in the future and I would want the package to keep working without changes. I see packages such as RcppArmadillo have just PKG_CXXFLAGS+CXX_STD, but if I try adding flags to my user Makevars, oftentimes packages would only pick them if they are under PKG_CXX11FLAGS.


Solution

  • This is just wrong:

    CXX_STD = CXX11
    PKG_CXXFLAGS = -fmyflag
    

    as you have a contradiction between the two lines, so you need

    CXX_STD = CXX11
    PKG_CXX11FLAGS = -fmyflag
    

    Personally, I never quite understood why we have four of these given that any one package can only ever compile to one standard but .... shrug-emoji-here .... I don't own the R build system either and just play along. Which works if you play by the rules.