12

I am trying to compile some C code (called rand_beta) in terminal which contains the lines to include R.h and Rmath.h header files using gcc -o rand_beta rand_beta.c so I can then call the code from within R. However, I get the error messages:

rand_beta.c:1:15: error: R.h: No such file or directory
rand_beta.c:2:19: error: Rmath.h: No such file or directory

It seems that these header files which should come installed with R are not on my system.

Can someone guide me as to how I can get my computer to find the R header files? Do I need to download them from somewhere?

zx8754
  • 52,746
  • 12
  • 114
  • 209
user3018658
  • 121
  • 1
  • 1
  • 3

4 Answers4

14

The other answers try to guess where your R installation directory is. But there is a more robust solution. Use the R.home command in R to find it wherever it is:

> R.home('include')
/usr/lib64/R/include

That is the folder containing R.h and Rmath.h on my system. Your folder may be in a different place.

drhagen
  • 8,331
  • 8
  • 53
  • 82
  • This should be the accepted answer, since it is not system dependent and can also be used for `lib` R directory with `R.home("lib")`. – Odin Aug 07 '19 at 14:59
9

You first need to locate those headers. In my system, they are located in /usr/lib64/R/include/R.h, part of the R-devel package I installed with yum.

Then use the -I option of gcc to tell gcc where to find them.

gcc -I/usr/lib64/R/include  -o rand_beta rand_beta.c

Then, you will also most probably need to export LD_LIBRARY_PATH to run your compiled program:

LD_LIBRARY_PATH=/usr/lib64/R/lib ./rand_beta
nico
  • 50,859
  • 17
  • 87
  • 112
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • 2
    Thanks everyone, actually I guess I just needed to use R CMD SHLIB rand_beta.c instead, as this creates the .so file which I need for R. – user3018658 Nov 21 '13 at 18:14
1

Another way is to specify some environment variables to directly use the include path:

export CPATH=/usr/lib64/R/include/
export C_INCLUDE_PATH=/usr/lib64/R/include/
export CPLUS_INCLUDE_PATH=/usr/lib64/R/include/
export GCC_INCLUDE_DIR=/usr/lib64/R/include/

This should then run fine:

gcc -o rand_beta rand_beta.c
mountrix
  • 1,126
  • 15
  • 32
0
sudo yum install R-devel

as error suggests helped me