R CMD SHLIB Fortran 90 file which use NetCDF - r

I would like compile a fortran 90 file which use NetCDF. I have installed NetCDF-Fortran and as shown here, to compile the file test_nc.f90:
program test_nc
use netcdf
implicit none
integer :: ncid, nc_err
nc_err = nf90_open('test.nc', nf90_nowrite, ncid)
nc_err = nf90_close(ncid)
end program test_nc
The compilation with gfortran is
gfortran test_nc.f90 -o test_nc `nf-config --fflags --flibs`
where nf-config --fflags --flibs is:
-I/usr/include
-L/usr/lib -lnetcdff -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -lnetcdf -lnetcdf -ldl -lz -lcurl -lm
Replacing program by subroutine is
subroutine test_nc
use netcdf
implicit none
integer :: ncid, nc_err
nc_err = nf90_open('test.nc', nf90_nowrite, ncid)
nc_err = nf90_close(ncid)
end subroutine test_nc
However, When I run
R CMD SHLIB test_nc.f90 `nf-config --fflags --flibs`
results in:
gfortran -fno-optimize-sibling-calls -fpic -g -O2 -fdebug-prefix-map=/build/r-base-k1TtL4/r-base-3.6.1=. -fstack-protector-strong -c test_nc.f90 -o test_nc.o
test_nc.f90:2:8:
2 | use netcdf
| 1
Fatal Error: Cannot open module file ‘netcdf.mod’ for reading at (1): No such file or directory
compilation terminated.
Also, when I try:
R CMD SHLIB test_nc.f90 -I/usr/include -L/usr/lib -lnetcdff -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -lnetcdf -lnetcdf -ldl -lz -lcurl -lm
gfortran -fno-optimize-sibling-calls -fpic -g -O2 -fdebug-prefix-map=/build/r-base-k1TtL4/r-base-3.6.1=. -fstack-protector-strong -c test_nc.f90 -o test_nc.o
results in:
test_nc.f90:2:8:
2 | use netcdf
| 1
Fatal Error: Cannot open module file ‘netcdf.mod’ for reading at (1): No such file or directory
compilation terminated.
make: *** [/usr/lib/R/etc/Makeconf:195: test_nc.o] Error 1
How can I tell R CMD SHLIB to use the Netcdf-fortran libraries?
?SHLIB shows
R CMD SHLIB -o mylib.so a.f b.f -L/opt/acml3.5.0/gnu64/lib -lacml
So I guess it is possible to do this

In the call to R CMD SHLIB the options you have provided from nf-config are taken only as linker options. The compilation step is failing because setting the search path for the NetCDF Fortran module is required before the link process.
To add the -I... option from nf-config you can use the environment variable PKG_FCFLAGS:
env PKG_FCFLAGS="`nf-config --fflags`" R CMD SHLIB test_nc.f90 `nf-config --flibs`
Alternatively, you can put PKG_FCFLAGS in your Makevars file.
(Note that, unlike C and C++, the include path option for module files is not for the pre-processing stage.)

Related

Compiling C++ with R using sourceCPP [duplicate]

I can’t figure out how to give my R package’s shared library’s debug symbols source line information. What am I missing?
I create the following src/Makevars file:
PKG_CXXFLAGS=-O0 -ggdb
PKG_LIBS=-O0 -ggdb
I compile the package using R CMD INSTALL --no-multiarch --with-keep.source:
* installing to library ‘~/.local/lib/R/3.6’
* installing *source* package ‘reticulate’ ...
** using staged installation
g++ -std=gnu++11 -I"/usr/include/R/" -DNDEBUG -I"$HOME/.local/lib/R/3.6/Rcpp/include" -D_FORTIFY_SOURCE=2 -O0 -ggdb -fpic -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -c RcppExports.cpp -o RcppExports.o
** libs
g++ -std=gnu++11 -shared -L/usr/lib64/R/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o reticulate.so RcppExports.o event_loop.o libpython.o output.o python.o readline.o -O0 -ggdb -L/usr/lib64/R/lib -lR
installing to ~/.local/lib/R/3.6/00LOCK-reticulate/00new/reticulate/libs
I debug like this:
R -d gdb --slave -e 'reticulate::py_eval("print")()'
GNU gdb (GDB) 8.3
[...]
(No debugging symbols found in /usr/lib64/R/bin/exec/R)
(gdb) break py_get_formals
Function "py_get_formals" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (py_get_formals) pending.
(gdb) run
Starting program: /usr/lib/R/bin/exec/R --slave -e reticulate::py_eval\(\"print\"\)\(\)
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[...]
Thread 1 "R" hit Breakpoint 1, 0x00007fffeb6b79a0 in py_get_formals(PyObjectRef, bool) () from /home/angerer/.local/lib/R/3.6/reticulate/libs/reticulate.so
(gdb) step
Single stepping until exit from function _Z14py_get_formals11PyObjectRefb,
which has no line number information.
[...]
Why does my function not have line numbers even though I specified -ggdb in both compilation? I see that only RcppExports.cpp is mentioned in the command line, is that the problem? If so, how can I change this?
Changing the Makevars doesn’t prompt recompilation.
I needed to rm -f src/*.o src/*.so before the object files get recompiled.
This is specifically for Windows. The simplest way to do it is to set the R_MAKEVARS_USER environment to point to the Makevars.win file. That seems to work. However, debug break points have stopped working!!!!

R package 'RcppArmadillo' error during installation

I'm trying to install RcppArmadillo in my R environment.
I have created a Docker container starting from a Ubuntu:20.04 image and installing miniconda to manage the environment and install R.
When I install RcppArmadillo I have this error:
checking whether the C++ compiler works... no
configure: error: in `/tmp/Rtmp1znFrE/R.INSTALL3901267e6e22/RcppArmadillo':
configure: error: C++ compiler cannot create executables
I have checked the config.log file to search the error, and I found this:
gcc version 9.4.0 (conda-forge 9.4.0-15)
configure:1931: $? = 0
configure:1920: aarch64-conda-linux-gnu-c++ -std=gnu++14 -V >&5
aarch64-conda-linux-gnu-c++: error: unrecognized command line option '-V'
aarch64-conda-linux-gnu-c++: fatal error: no input files
compilation terminated.
configure:1931: $? = 1
configure:1920: aarch64-conda-linux-gnu-c++ -std=gnu++14 -qversion >&5
aarch64-conda-linux-gnu-c++: error: unrecognized command line option '-qversion'; did you mean '--version'?
aarch64-conda-linux-gnu-c++: fatal error: no input files
compilation terminated.
configure:1931: $? = 1
configure:1951: checking whether the C++ compiler works
configure:1973: aarch64-conda-linux-gnu-c++ -std=gnu++14 -fvisibility-inlines-hidden -fmessage-length=0 -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O3 -pipe -isystem /root/miniconda3/include -fdebug-prefix-map=/drone/src/build_artifacts/r-base-split_1630154255557/work=/usr/local/src/conda/r-base-4.1.1 -fdebug-prefix-map=/root/miniconda3=/usr/local/src/conda-prefix conftest.cpp >&5
/root/miniconda3/bin/../lib/gcc/aarch64-conda-linux-gnu/9.4.0/../../../../aarch64-conda-linux-gnu/bin/ld: /root/miniconda3/bin/../lib/gcc/aarch64-conda-linux-gnu/9.4.0/libstdc++.so: undefined reference to `__cxa_thread_atexit_impl#GLIBC_2.18'
collect2: error: ld returned 1 exit status
I don't understand if it's a problem of GCC (I have tried to uninstall and install again through apt-get but nothing changed), but seems really strange to me.
If I echo $PATH I get as result /root/miniconda3/bin:/usr/bin:/bin, but If I find / -name gcc I get:
/root/miniconda3/lib/R/library/BH/include/boost/mpl/aux_/preprocessed/gcc
/root/miniconda3/lib/gcc
/root/miniconda3/libexec/gcc
/root/miniconda3/libexec/gcc/aarch64-conda-linux-gnu/9.4.0/gcc
/root/miniconda3/share/licenses/gcc
/root/miniconda3/pkgs/gcc_impl_linux-aarch64-9.4.0-h603953f_15/libexec/gcc
/root/miniconda3/pkgs/gcc_impl_linux-aarch64-9.4.0-h603953f_15/libexec/gcc/aarch64-conda-linux-gnu/9.4.0/gcc
/root/miniconda3/pkgs/gcc_impl_linux-aarch64-9.4.0-h603953f_15/share/licenses/gcc
/root/miniconda3/pkgs/gcc_impl_linux-aarch64-9.4.0-h603953f_15/lib/gcc
/root/miniconda3/pkgs/libgcc-devel_linux-aarch64-9.4.0-h85f5abf_15/lib/gcc
/root/miniconda3/pkgs/libstdcxx-devel_linux-aarch64-9.4.0-h85f5abf_15/lib/gcc
/root/miniconda3/pkgs/gfortran_impl_linux-aarch64-9.4.0-h54cdb55_15/libexec/gcc
/root/miniconda3/pkgs/gfortran_impl_linux-aarch64-9.4.0-h54cdb55_15/lib/gcc
/root/miniconda3/pkgs/gxx_impl_linux-aarch64-9.4.0-h603953f_15/libexec/gcc
/usr/share/gcc
/usr/share/doc/gcc
/usr/share/doc/gcc-9-base/gcc
/usr/lib/gcc
/usr/bin/gcc
so maybe there are some mistakes in all these GCC instances.
Thanks for your help

Error in R CMD SHLIB compiling Fortran code

I'm trying to compile a Fortran subroutine in the remote machine, when I run:
R CMD SHLIB -fPIC vintp2p_afterburner_wind.f
I get the following error:
gcc -shared -L/sw/installed/R/3.6.2-fosscuda-2019b/lib64/R/lib -L/sw/installed/pocl/1.4-GCC-8.3.0/lib64 -L/sw/installed/ImageMagick/7.0.9-5-GCCcore-8.3.0/lib -L/sw/installed/GSL/2.6-GCC-8.3.0/lib -L/sw/installed/UDUNITS/2.2.26-GCCcore-8.3.0/lib -L/sw/installed/HDF5/1.10.5-gompic-2019b/lib -L/sw/installed/ICU/64.2-GCCcore-8.3.0/lib -L/sw/installed/libsndfile/1.0.28-GCCcore-8.3.0/lib -L/sw/installed/FFTW/3.3.8-gompic-2019b/lib -L/sw/installed/NLopt/2.6.1-GCCcore-8.3.0/lib64 -L/sw/installed/GMP/6.1.2-GCCcore-8.3.0/lib -L/sw/installed/libxml2/2.9.9-GCCcore-8.3.0/lib -L/sw/installed/cURL/7.66.0-GCCcore-8.3.0/lib -L/sw/installed/Tk/8.6.9-GCCcore-8.3.0/lib -L/sw/installed/Java/11.0.2/lib -L/sw/installed/LibTIFF/4.0.10-GCCcore-8.3.0/lib -L/sw/installed/libjpeg-turbo/2.0.3-GCCcore-8.3.0/lib64 -L/sw/installed/libpng/1.6.37-GCCcore-8.3.0/lib -L/sw/installed/PCRE/8.43-GCCcore-8.3.0/lib -L/sw/installed/SQLite/3.29.0-GCCcore-8.3.0/lib -L/sw/installed/zlib/1.2.11-GCCcore-8.3.0/lib -L/sw/installed/XZ/5.2.4-GCCcore-8.3.0/lib -L/sw/installed/bzip2/1.0.8-GCCcore-8.3.0/lib -L/sw/installed/ncurses/6.1-GCCcore-8.3.0/lib -L/sw/installed/libreadline/8.0-GCCcore-8.3.0/lib -L/sw/installed/cairo/1.16.0-GCCcore-8.3.0/lib -L/sw/installed/libGLU/9.0.1-GCCcore-8.3.0/lib -L/sw/installed/Mesa/19.1.7-GCCcore-8.3.0/lib -L/sw/installed/X11/20190717-GCCcore-8.3.0/lib -L/sw/installed/ScaLAPACK/2.0.2-gompic-2019b/lib -L/sw/installed/OpenBLAS/0.3.7-GCC-8.3.0/lib -L/sw/installed/GCCcore/8.3.0/lib64 -L/sw/installed/GCCcore/8.3.0/lib -L/sw/installed/CUDA/10.1.243/lib64 -o vintp2p_afterburner_wind.so vintp2p_afterburner_wind.o -fPIC -lgfortran -L/sw/installed/R/3.6.2-fosscuda-2019b/lib64/R/lib -lR
/sw/installed/binutils/2.28-GCCcore-6.4.0/bin/ld.gold: error: vintp2p_afterburner_wind.o: requires dynamic R_X86_64_PC32 reloc against 'log' which may overflow at runtime; recompile with -fPIC
collect2: error: ld returned 1 exit status
make: *** [vintp2p_afterburner_wind.so] Error 1
The subroutine has a log() function. I'm totally new to this error and I don't know where to start with!
Maybe someone will find it useful: compiling is done by :
gfortran -fPIC -shared -ffree-form vintp2p_afterburner_wind.f -o vintp2p_afterburner_wind.so

Compile a Rcpp package with line information in the debugging symbols

I can’t figure out how to give my R package’s shared library’s debug symbols source line information. What am I missing?
I create the following src/Makevars file:
PKG_CXXFLAGS=-O0 -ggdb
PKG_LIBS=-O0 -ggdb
I compile the package using R CMD INSTALL --no-multiarch --with-keep.source:
* installing to library ‘~/.local/lib/R/3.6’
* installing *source* package ‘reticulate’ ...
** using staged installation
g++ -std=gnu++11 -I"/usr/include/R/" -DNDEBUG -I"$HOME/.local/lib/R/3.6/Rcpp/include" -D_FORTIFY_SOURCE=2 -O0 -ggdb -fpic -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -c RcppExports.cpp -o RcppExports.o
** libs
g++ -std=gnu++11 -shared -L/usr/lib64/R/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o reticulate.so RcppExports.o event_loop.o libpython.o output.o python.o readline.o -O0 -ggdb -L/usr/lib64/R/lib -lR
installing to ~/.local/lib/R/3.6/00LOCK-reticulate/00new/reticulate/libs
I debug like this:
R -d gdb --slave -e 'reticulate::py_eval("print")()'
GNU gdb (GDB) 8.3
[...]
(No debugging symbols found in /usr/lib64/R/bin/exec/R)
(gdb) break py_get_formals
Function "py_get_formals" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (py_get_formals) pending.
(gdb) run
Starting program: /usr/lib/R/bin/exec/R --slave -e reticulate::py_eval\(\"print\"\)\(\)
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[...]
Thread 1 "R" hit Breakpoint 1, 0x00007fffeb6b79a0 in py_get_formals(PyObjectRef, bool) () from /home/angerer/.local/lib/R/3.6/reticulate/libs/reticulate.so
(gdb) step
Single stepping until exit from function _Z14py_get_formals11PyObjectRefb,
which has no line number information.
[...]
Why does my function not have line numbers even though I specified -ggdb in both compilation? I see that only RcppExports.cpp is mentioned in the command line, is that the problem? If so, how can I change this?
Changing the Makevars doesn’t prompt recompilation.
I needed to rm -f src/*.o src/*.so before the object files get recompiled.
This is specifically for Windows. The simplest way to do it is to set the R_MAKEVARS_USER environment to point to the Makevars.win file. That seems to work. However, debug break points have stopped working!!!!

No .so file after compiling with R SHLIB

So I wanted to compile my fortran subroutine to be able to use it in R. However I do not get an .so file only a .dll file. Is there maybe a problem with the paths? Or is this supposed to happen.
C:\BCT_Work>R CMD SHLIB draw_JU.f95
c:/Rtools/mingw_64/bin/gfortran -O2 -mtune=generic -c draw_JU.f95 -o draw_JU.o
c:/Rtools/mingw_64/bin/gfortran -shared -s -static-libgcc -o draw_JU.dll tmp.def draw_JU.o -LC:/PROGRA~1/R/R-35~1.1/bin/x64 -lR

Resources