7

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

Sergio
  • 714
  • 1
  • 8
  • 24

1 Answers1

5

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.)

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • object test_nc.so returns Segmentation fault (core dumped). I tried with including Makevars but gives undefined symbol: __netcdf_MOD_nf90_close. According to this https://github.com/Unidata/netcdf-fortran/issues/76 it is compiled with the correct flags. – Sergio Apr 28 '20 at 18:31
  • If using `Makevars` then you'll still need to set the linker options to point to the library. Did you set the linker options in `Makevars` or continue with ``nf-config --flibs`` on the command line? Or is it the undefined symbol when you try to load the library? – francescalus Apr 28 '20 at 19:55
  • And I assume the segmentation fault comes from when it did work and you tried to use the `test_nc.so` in your R session. How did you try to call it from R? – francescalus Apr 28 '20 at 19:59
  • On Makevars, If I write PKG_FCFLAGS="`nf-config --fflags --flibs`" I get "Cannot open module file ‘netcdf.mod’". When I take out "--flibs", I get "undefined symbol: __netcdf_MOD_nf90_close". As I`m trying to create an R package here, I call it with "R CMD INSTALL ." Should I need to create an confgure.ac? – Sergio Apr 29 '20 at 01:05
  • `PKG_FCFLAGS` should just be the `--fflags` option, for compiler flags. The linker flags from `--flibs` must be given elsewhere, such as in the `R CMD` command. Does the full `env ...` command I give work in your case? – francescalus Apr 29 '20 at 01:16
  • Here I am testing with netcdf-fortran and R https://github.com/ibarraespinosa/nf – Sergio Apr 29 '20 at 01:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212747/discussion-between-sergio-and-francescalus). – Sergio Apr 29 '20 at 01:40