Skip to content

What does `LinkingTo` do in an R package?

An answer to this question on Stack Overflow.

Question

I am building an R package whose description contains:

LinkingTo: Rcpp

The package has a fairly deep makefile structure. I know using R CMD build . creates and modifies variables such as CXX11FLAGS which must be passed down through subsequent layers of makefiles.

I suspect that LinkingTo also produces such a variable, which I must be aware of and pass along. I suspect this because, several layers down, I hit the error:

mycode.hpp:5:10: fatal error: Rcpp.h: No such file or directory
#include <Rcpp.h>

I'm not sure how to inform this file's makefile as to where Rcpp is. I suspect the hypothesized variable could be used, but I do not know that variable's name. Can anyone clarify?

Answer

I edited my Makefile to include the following dark magic:

VARS_OLD := $(.VARIABLES)
$(foreach v,                                        \
$(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)),   \
$(info $(v) = $($(v))))

This prints out all of the environmental variables passed to the make process by R when you run R CMD build ..

Digging in this revealed some very interesting variables:

ALL_CPPFLAGS = -I/usr/share/R/include -DNDEBUG  -I"/home/myuser/.R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" 
CLINK_CPPFLAGS = -I"/home/myuser/.R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include"
R_INCLUDE_DIR = /usr/share/R/include

Note that these variables contain the -I flag and, thus, must be communicated to any part of the build process that relies on those.

By passing these between makefiles I was able to achieve compilation.