r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
199 stars 46 forks source link

How to set up `CPPFLAGS` with `cpp11::cpp_source` #246

Open vspinu opened 2 years ago

vspinu commented 2 years ago

Sorry if obvious, but I cannot figure out a way to provide compiler flags to cpp_soruce besides modifying the ~/.R/Makevars. A local Makevars file is not picked up.

jimhester commented 2 years ago

You should be able to set the environment variable R_MAKEVARS_USER to wherever you want it to be if you don't want to use the site wide makevars file. see https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Customizing-package-compilation

There isn't anything built into cpp11 to make this easier however.

vspinu commented 2 years ago

Thanks! I learned something new today.

Would it make sense for cpp11 to pick the Makevars in the working directory automatically? Similarly how the Makevars within packages works I would suggest.

paleolimbot commented 2 years ago

You can also temporarily set the PKG_CPPFLAGS environment variable (totally unverified hack on my end). For example, I use this to prototype C++ linking to my local development version of Arrow:

withr::with_envvar(
  list(
    PKG_CXXFLAGS = paste0("-I", Sys.getenv("ARROW_HOME"), "/include"),
    PKG_LIBS = paste0("-L", Sys.getenv("ARROW_HOME"), "/lib", " -larrow")
  ),
  cpp11::cpp_source("2021_11_05_copy_files.cpp")
)