[英]R CMD SHLIB Fortran 90 file which use NetCDF
I would like compile a fortran 90 file which use NetCDF.我想编译一个使用 NetCDF 的 fortran 90 文件。 I have installed NetCDF-Fortran and as shown here , to compile the file test_nc.f90:
我已经安装了NetCDF-Fortran并且如此处所示,以编译文件 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 的编译是
gfortran test_nc.f90 -o test_nc `nf-config --fflags --flibs`
where nf-config --fflags --flibs
is:其中
nf-config --fflags --flibs
是:
-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?我如何告诉 R CMD SHLIB 使用 Netcdf-fortran 库?
?SHLIB shows ?SHLIB 显示
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.在调用
R CMD SHLIB
,您从nf-config
提供的选项仅作为linker选项。 The compilation step is failing because setting the search path for the NetCDF Fortran module is required before the link process.编译步骤失败,因为在链接过程之前需要设置 NetCDF Fortran 模块的搜索路径。
To add the -I...
option from nf-config
you can use the environment variable PKG_FCFLAGS
:要从
nf-config
添加-I...
选项,您可以使用环境变量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.或者,您可以将
PKG_FCFLAGS
放入 Makevars 文件中。
(Note that, unlike C and C++, the include path option for module files is not for the pre-processing stage.) (请注意,与 C 和 C++ 不同,模块文件的包含路径选项不适用于预处理阶段。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.