9

Can you set R's C and C++ flags at compilation time when installing from R CMD INSTALL (essentially, in this particular case I want to turn off compiler optimization, but ideally there's a general solution)?

I know you can affect some options using --configure-args="...", and I rather optimistically tried --configure-args="diable-optimization", to no avail. Similarly, I could also edit $RHOME/etc/Makeconf but again this is not really the kind of solution I'm looking for (and not possible where I don't have the relevant write permission).

I define my flags through an autoconf script and with a Makevars file in the package/src directory, if this makes any difference.

Alex
  • 2,000
  • 4
  • 23
  • 41

3 Answers3

8

Dirk - very helpful discussion (as always) and definitly pointed me in the right direction. For my specific issue, it turned out in addition to the Makevars file I had to pass arguments through to configure. I have no idea why this is the case (and reading around doesn't seem to be the norm, so maybe I've done something wrong somewhere), but if anyone else has the same problem, using a ~/.R/Makevars combined with the following arguments for configure/INSTALL worked for me.

R CMD INSTALL --configure-args="CFLAGS=-g CXXFLAGS=-g" package.tar.gz
Just a student
  • 10,560
  • 2
  • 41
  • 69
Alex
  • 2,000
  • 4
  • 23
  • 41
6

Yes, I use a file ~/.R/Makevars for that. Also handy to set CC and CXX to different compilers when, say, switching gcc versions, or switching to llvm, or ...

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    That's ideal - but having created the `~/.R/Makevars` file, how to you get R to use these variables during compilation? – Alex Jun 06 '12 at 22:01
  • 2
    It's automatic when you use `R CMD ...`. – Dirk Eddelbuettel Jun 06 '12 at 22:02
  • See, I was afraid you'd say that. Should I need anything special in my configure.ac script to make this work? – Alex Jun 06 '12 at 22:28
  • 1
    It is orthogonal to configure. Makevars is *system-wide*, configure.* is per-package. Two different things which can play together. But this is the wrong forum for this discussion anyway. Read some more 'Writing R Extensions', and follow R-devel. – Dirk Eddelbuettel Jun 06 '12 at 23:45
  • I've tried installing data.table but it ognores the content of Makevars. – skan Feb 22 '17 at 12:03
  • @skan: No it won't. But maybe you set the wrong variable? `data.table` is mostly C so you need to set `CC`, `CFLAGS`, etc pp – Dirk Eddelbuettel Feb 22 '17 at 12:22
  • I've set CXXFLAGS+= -mtune=native -march=native -Wno-unused-variable -Wno-unused-function in the Makevarks file at C:\Users\skan\Documents\.R I've also tried using the name Makevars.win and also tried C:\Users\skan\Documents\R. Sys.getenv("HOME") shows C:\Users\skan\Documents – skan Feb 22 '17 at 13:49
  • As I told you in the previous message, data.table is written in C. But CXXFLAGS works for C++ code. So you missed your target. Set CFLAGS, not CXXFLAGS. – Dirk Eddelbuettel Feb 22 '17 at 13:53
  • If using Windows, it's helpful to know that `path.expand("~")` will tell you the full path to `~`, which is likely `C:/Users/USERNAME/Documents` – Martin Smith Jan 21 '19 at 10:15
2

I can confirm that the Makevars file is very useful (specially if you need to use "-L/my/libs" or "-I/my/includes", or others build flags).

For the build, if you want to set an option for the site/machine, you can also change variables in the Makeconf file (/path/R/install/[lib64/R/]etc/Makeconf).

However, if like me, you still have some problems to manage and use libraries later, you can also set libraries with the ldpaths file [1]. This file contains the R_LD_LIBRARY_PATH used by R. This variable is the equivalent of the well known LD_LIBRARY_PATH on unix [2].

I just added some content (just before the comment on MacOS / Darwin) to this file (/path/R/install/[lib64/R/]etc/ldpaths):

if test -n "${LD_LIBRARY_PATH}"; then
  R_LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${R_LD_LIBRARY_PATH}"
fi
## This is DYLD_FALLBACK_LIBRARY_PATH on Darwin (OS X) and

Then, you will be able to manage your libraries dynamically e.g. using "environment modules" or "lmod".

Note that you can change many other environment and R variables with all the file which are in that config/etc directory (Renviron, repositories, javaconf, Rprofile.site ...).

[1] https://support.rstudio.com/hc/en-us/community/posts/200645248-Setting-up-LD-LIBRARY-PATH-for-a-rsession

[2] http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

remyd1
  • 31
  • 4