Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use the option -std=c99 for installing R packages

Tags:

r

I have problem in installing plyr R package, and got the following error:

Installing package into '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2'
(as 'lib' is unspecified)
* installing *source* package 'plyr' ...
** package 'plyr' successfully unpacked and MD5 sums checked
** libs
g++ -I/share/apps/R/lib64/R/include -DNDEBUG  -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include"   -fpic  -g -O2  -c RcppExports.cpp -o RcppExports.o
gcc -I/share/apps/R/lib64/R/include -DNDEBUG  -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include"   -fpic  -g -O2  -c loop_apply.c -o loop_apply.o
loop_apply.c: In function 'loop_apply':
loop_apply.c:15:3: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
   for(int i = 0; i < n1; i++) {
   ^
loop_apply.c:15:3: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
make: *** [loop_apply.o] Error 1
ERROR: compilation failed for package 'plyr'
* removing '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/plyr'
Warning message:
In install.packages("R packages/plyr_1.8.3.tar.gz", repos = NULL) :
  installation of package 'R packages/plyr_1.8.3.tar.gz' had non-zero exit status

How can I use -std=c99 option when try to install R packages from the source by install.packages command?

Thanks

like image 637
Zaynab Avatar asked Feb 04 '16 10:02

Zaynab


1 Answers

Try C11 First

The compiler error tells you to choose either C99 or C11. So unless you're knowingly compiling legacy code, try C11 first. You can always then try the legacy C99 if that doesn't work.

Use withr::with_makevars

Editing individual package Makevars or coordinating global changes to your include/lib directories for every package that doesn't directly compile from source is not a happy strategy for the future. Don't do that! A great alternative to editing your R's make configuration is to use withr::with_makevars to manipulate the Makevars only for the installation command:

library(withr)

with_makevars(c(PKG_CFLAGS = "-std=c11"), 
              install.packages("plyr", repos = NULL, type = "source"), 
              assignment = "+=")

Plus, you likely already have withr installed since it's a devtools dependency.

like image 97
merv Avatar answered Nov 01 '22 16:11

merv