How to install/locate R.h and Rmath.h header files? - r

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?

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.

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

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

Related

Can't find files which exist (hdf5,h, mpi.h, fftw3.h, libGLU.so.1) - needed for Athena++ and Visit

Relatively new to Linux and trying to work with a program (Athena++ - https://github.com/PrincetonUniversity/athena-public-version/wiki - , a magnetohydrodynamics code) for my studies.
I've followed the tutorial there and can run simpler simulations, but for anything which requires mpi or hdf5, I'm running into an issue where it says
fatal error: hdf5.h: No such file or directory
[sferrel6#phys44222 athena]$ make
g++ -O3 -std=c++11 -c src/main.cpp -o obj/main.o
In file included from src/main.cpp:43:0:
src/outputs/outputs.hpp:22:18: fatal error: hdf5.h: No such file or directory
#include <hdf5.h>
^
compilation terminated.
make: *** [obj/main.o] Error 1
Similarly I get the same issue for mpi.h, fftw3.h and (when trying to use Visit) libGlu.so.1
These files certainly exist, though I can't figure out how to get my program to see them.
$ find -iname "hdf5.h"
~/local/anaconda2/pkgs/hdf5-1.10.4-nompi_h3c11f04_1106/include/hdf5.h
~/local/anaconda2/pkgs/hdf5-1.10.4-hb1b8bf9_0/include/hdf5.h
~/local/anaconda2/include/hdf5.h
$ find -iname "mpi.h"
~/local/anaconda2/pkgs/openmpi-4.0.1-hc99cbb1_2/include/mpi.h
~/local/anaconda2/include/mpi.h
find -iname "libGLU.so.1"
~/local/anaconda2/lib/libGLU.so.1
~/local/anaconda2/pkgs/libglu-9.0.0-hf484d3e_1/lib/libGLU.so.1
I've tried updating my path in .bash_profile (I'm on RHEL7) to include these paths, but to no avail - still the same error. I've also tried to use export CPATH='~/local/anaconda2/include' but that has not helped.
Please note that I'm working on a university machine, which means I can't use sudo (why everything is through anaconda).
Also, it means my directory structure is a bit unorthodox.
~ is nethome/myname and has very limited space so all of my work is at /localdata/myname which is reached via symbolic link at ~/local
I've also tried copying those specific files into my Athena directory (~/local/athena, where I'm running my athena Makefile from) in the hopes that it would then be able to find them and I could better diagnose the issue. No luck there either.
Was able to solve this problem by testing compiler search path with cpp -v which determined that my search paths weren't looking where I was able to find those various .h files. I updated this by using
export CPATH='/nethome/myname/local/anaconda2/include:/nethome/myname/local/anaconda2/lib'
I'm sure there's a better way to do this (since I've seen cautions against using CPATH in my various googling), but if you happen upon this question, it's at least the method that I found to work for me.

How to add a directory to the search path of GCC (crti.o not found)

I am installing GSI V3.5 https://dtcenter.org/com-GSI/users/. I have no experience in gcc so I cannot use a correct terminolgy for this.
After compiling the program I get an error
” ...........
"/raid60/raid2/opt/local/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld:
cannot find Scrt1.o: No such file or directory
/raid60/raid2/opt/local/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld:
cannot find crti.o: No such file or directory
/raid60/raid2/opt/local/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld:
cannot find -lm
/raid60/raid2/opt/local/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld:
cannot find -lgomp "
It seems that there is problem with gcc which cannot find the
directory including Scrt1.0, crti.o and others.I found these files in
this directory
/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib. Because I do not have adminstration previlage, as suggested, I could
not install packages like libc6-dev. Follwing
https://askubuntu.com/questions/251978/cannot-find-crti-o-no-such-file-or-directory
And
https://askubuntu.com/questions/251978/cannot-find-crti-o-no-such-file-or-directory,
I added the directory above to ~/.cshrc.
setenv LD_LIBRARY_PATH
/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib
But still gcc does not recognize this path.
I also tried to feed this path into configure using LDFLGAS
> ./configure
> LDFLAGS="-L/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib"
But no success. At the end I added LDFLAGS into configure
script. But it also did not work. Any help is highly apreaciated.
Of course, the expected answer would be making gcc identify the path
of crti.o and other missing files.
UPDATE:
As Florian Weimer suggested, I made the file crt.specs. I also updated related line in configure. These files are in dropbox link . But I still get the same error.
UPDATE 2.
These are some lines before error occurs:
../write_obsdiags.o ../wrwrfmassa.o ../wrwrfnmma.o
-L/opt/local/wrf_kpp/gsi35/comGSIv3.5_EnKFv1.1/lib -lsp_i4r8 -lsigio_i4r4 -lsfcio_i4r4 -lnemsio -lbacio -lw3nco_i4r8 -lbufr_i4r8 -lw3emc_i4r8 -lcrtm /opt/local/wrf_kpp/WRFV3//external/io_int/libwrfio_int.a
/opt/local/wrf_kpp/WRFV3//external/io_netcdf/libwrfio_nf.a
/opt/local/wrf_kpp/WRFV3//frame/pack_utils.o
/opt/local/wrf_kpp/WRFV3//frame/module_machine.o
/opt/local/wrf_kpp/WRFV3//frame/module_driver_constants.o
-L/home/srvx11/lehre/users/a1276905/.conda/envs/libgsi/lib -lnetcdff -lnetcdf -L/home/srvx11/lehre/users/a1276905/.conda/envs/libgsi/lib -L/home/srvx11/lehre/users/a1276905/.conda/envs/libgsi/lib -llapack -lblas -fopenmp /raid60/raid2/opt/local/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld:
cannot find Scrt1.o: No such file or directory
Update 3
I have used option -B to add directory including crti.o to the search path of gcc.
when I invoke
gcc -print-search-dirs
I see my desired path "/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib" among known paths
libraries:
=/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib/x86_64-unknown-linux-gnu/5.3.0/:/home/opt/anaconda/x86_64-conda_cos6-linux-gnu/sysroot/usr/lib/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../x86_64-unknown-linux-gnu/lib/x86_64-unknown-linux-gnu/5.3.0/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../x86_64-unknown-linux-gnu/lib/../lib64/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../x86_64-unknown-linux-gnu/5.3.0/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../lib64/:/lib/x86_64-unknown-linux-gnu/5.3.0/:/lib/../lib64/:/usr/lib/x86_64-unknown-linux-gnu/5.3.0/:/usr/lib/../lib64/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../../x86_64-unknown-linux-gnu/lib/:/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../:/lib/:/usr/lib/
But I am not through.
The linker search path is only used for libraries. It is not used to locate any object files, including the startup object files.
If the toolchain was built with sysroot support, you can use --sysroot to change where GCC looks for most of its files. This may require adding symbolic links or copying files to create a working sysroot environment and can be somewhat cumbersome.
If you want to change just the location of the startup files, you can do so with a custom GCC specs file. What you need to use is unfortunately somewhat dependent on the GCC version and how GCC was built. This means that as a starting point, you need to use the specs built into GCC, which you can obtain using this command:
gcc -dumpspecs
There are two sections in this file, *startfile: and *endfile:, that are of interest. Copy those two lines and the line following each into a new file, say crt.specs. Edit them and replace Scrt1.o, crt1.o, crti.o (in *startfiles:) and crtn.o (in *endfiles:) with the absolute path to the files you want to use (presumable files you have extracted from libc6-dev using dpkg -x).
Then you can use the new specs by invoking GCC using:
gcc --specs=crt.specs
It will definitely save you a lot of trouble if you can get the system administrator to install the libc6-dev package. It is the only safe way to ensure that these files are kept up-to-date along with the libc6 package.

Compile With Static Library Using GNAT

To learn the processes behind compilation of Ada programs (without resorting to using gpr), I've decided to compile one of my projects manually. To compile a program, I run the following command on each .adb file therein (not manually, however):
$ gcc -c src/<file>.adb -o obj/<file>.o
That compiles all the files and puts them in the obj directory.
Or rather, it would. There is a slight problem with this. I have an archive (static library) that I've generated from another project, called libapples.a, containing package Apples. The Apples package is used by the files you see me compiling above.
Since libapples.a doesn't have source files anymore (given its archive format), it's not viable (not even possible) to provide the sources of that library to the command above using -I switches; on the other hand, if I don't have the sources included, the command above comes back to me saying:
<file>.adb:<y>:<x>: file "apples.ads" not found
gnatmake: "src/<file>.adb" compilation error
I've attempted to include the library in the compilation process by using flags -L and l (which I believe is how you'd do it in C; feel free to correct me if I'm wrong). In another attempt I placed the archive right in the source directory.
How does one go about including a library in the compilation process?
apples.ads is somewhat like a header in C and you definitely need it for your file to compile. You should use -I to point gcc to the directory where apples.ads is located in.
Be aware that compiling an Ada source not only yields an object file, but also an Ada Library Information (.ali) file. You'll need that for the next step. You should not use the -o flag because it may lead to your .o and .ali files having different names.
On the other hand, since you only want to produce an object file, you should use -c to tell gcc to only compile. In other languages like C, gcc compiles and links by default; in Ada, the process is more complex.
After compilation, you need to call gnatbind on the .ali file of your main program. This generates a package that will take care of proper initialization of everything. To do that, gnatbind needs the .ali file of the Apples package! You can specify the .ali search directory with -aO.
Finally, gnatlink called on the main .ali file will link everything together. It figures out itself which object files and libraries it needs. It will append the necessary options to the linker, including the reference to libapples.a. It also compiles the package generated by gnatlink.
I don't know exactly what gnatlink will do under the hood; you could probably compile the package generated by gnatbind manually and then call the linker if you can figure out the proper arguments.

Specifying Directories during Fortran Compilation

I am using GNU Fortran 95 on OpenSUSE Leap to compile my source code, and I am trying to figure out how to tell gfortran to change the default directory to search for my modules.
Let me explain my setup. I've written several module files that contain general subroutines I use. I've placed and compiled these files in a folder entitled ModuleRepo. My working scripts are placed in a separate folder. For example, I tried the following command:
gfortran -o script script.f95 -I/pathToFolder/ModuleRepo module1.o module2.o
However, this is not working. It is returning the following error:
gfortran: error: module1.o: No such file or directory.
The *.o files do exist in ModuleRepo, so I'm unsure what's going on. Everything works fine if I copy all my module files to the directory. But, I'd like a way to avoid copying the same files over and over.
gfortran -o script script.f95 -I/pathToFolder/ModuleRepo /pathToFolder/ModuleRepo/module1.o /pathToFolder/ModuleRepo/module2.o

Basic compile issue with QT4

I've been trying to get a dead simple listing from a university textbook to compile with the newest QT SDK for Windows I downloaded last night. After struggling through the regular nonsense (no make.bat, need to manually add environment variables and so on) I am finally at the point where I can build. But only one of the two libraries seem to work.
The .pro file I use is dead simple:
SUBDIRS += utils \
dataobjects
TEMPLATE = subdirs
In each of these two subfolders I have the source for a library. Running QMAKE generates a makefile and running Make runs through all the preliminaries and then fails on the g++ call:
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc --out-implib,libdataobjects.a -shared -mthreads -Wl -Wl,--out-implib,c:\Users\Cobus\workspace\lib\libdataobjects.a -o ..\..\lib\dataobjects.dll object_script.dataobjects.Debug -L"c:\Users\Cobus\Portab~1\Qt\2010.02.1\qt\lib" -LC:\Users\Cobus\workspace\lib -lutils -lQtXmld4 -lQtGuid4 -lQtCored4
c:/users/cobus/portab~1/qt/2010.02.1/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -lutils
The problem seems to be right near the end of the command line, where -lutils is added, indicating that there is a library by the name of utils. While I would have expected to see that, you'll notice the library names after --out include lib in the name, so they become libutils and libdataobjects. I have tried to figure out why this is happening, to no avail.
Anyone have an idea what's going on?
You .pro file uses the SUBDIRS template which requires a .pro file matching the directory name for each subdir. You should have a utils\utils.pro and dataobjects\dataobject.pro file as well.
Is c:\Users\Cobus\workspace\lib\libdataobjects.a built correctly? If not, check the sub directory .pro files.
With what you posted, I would not expect anything to compile. You need targets in the sub-directory .pro files to build anything.
Their seems to be another pro file as the generated Makefile is pulling QtXml which it wont do by default.
Please check the subdirs for any .pro or .pri files .. one of them will have answer to your confusion

Resources