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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With