How to install / find Rh and Rmath.h header files?

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

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

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

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

+6
source share
3 answers

First you need to find these headers. On my system, they are located in /usr/lib64/R/include/Rh , in the R-devel package that I installed with yum .

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

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

Then you will most likely need to export LD_LIBRARY_PATH to run your compiled program:

 LD_LIBRARY_PATH=/usr/lib64/R/lib ./rand_beta 
+5
source

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

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

This is the folder containing Rh and Rmath.h on my system. Your folder may be located elsewhere.

+4
source

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

 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/ 

Then this should work fine:

 gcc -o rand_beta rand_beta.c 
+1
source

Source: https://habr.com/ru/post/958585/


All Articles