I use Rcpp in my R package.
I have a src/Makevars file, where the only modification in the variables is:
PKG_CPPFLAGS=-I. -I$(FUZZYCOCO_SRC) -UNDEBUG
PKG_LIBS= mylib.a
When I compile my package using devtools, I noticed this command:
g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I. -Ifuzzycoco/src -I'/usr/lib/R/site-library/Rcpp/include' -fpic -g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/r-base-wwoKA3/r-base-4.5.1=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/r-base-wwoKA3/r-base-4.5.1=/usr/src/r-base-4.5.1-1.2404.0 -Wdate-time -D_FORTIFY_SOURCE=3 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
Note that there is a -O2 near the beginning, and a -O0 near the end. I believe that this trailing -O0 overrrides the -O2 so that the code is not optimized.
I also checked the /usr/lib/R/etc/Makeconf file, and the O0 string does not appear.
Where does this -O0 come from, and how to ensure that my C++ code is compiled with -O2 in a portable way (i.e. under Windows, MacOS etc...)?
This comes from pkgbuild::compile_dll() which has an argument debug which defaults to TRUE. When TRUE, it adds those debug flags.
If using pkgbuild::compile_dll(debug = FALSE), then the compilation command is:
g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I. -Ifuzzycoco/src -UNDEBUG -I'/usr/lib/R/site-library/Rcpp/include' -fpic -g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/r-base-wwoKA3/r-base-4.5.1=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fdebug-prefix-map=/build/r-base-wwoKA3/r-base-4.5.1=/usr/src/r-base-4.5.1-1.2404.0 -Wdate-time -D_FORTIFY_SOURCE=3 -Wall -pedantic -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
Note. pkgbuild::compile_dll() is called by devtools::load_all(), and there is apparently no way to pass the debug = FALSE argument.
So I suggest to explicitly compile before calling devtools::load_all().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With