r - R CMD SHLIB Fortran 90 file which use NetCDF
问题描述
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.)
推荐阅读
- c# - 如何在 C# 中的 selenium webdriver 中断言?
- php - 字符串解析运算符到对象
- sql-server - 在没有返回数据的情况下,每周的星期一投 0 计数,SQL
- javascript - 检查一个对象数组是否包含 Jest 中另一个对象数组的元素
- unit-testing - 如何使用 Mockito 在 Flutter 中模拟静态方法?
- c# - StringWriter 处理生成的 csv 上的奇怪字符
- java - 将列表中的对象变量匹配和设置到不同的对象列表
- excel - 是否可以从位于网络文件夹中的文件加载 VBA 代码?
- ruby-on-rails - 如何在 Aptana Studio 3 上创建 Rails 运行配置?
- reactjs - React.memo 是创建一个单例组件还是可以用来创建多个独立的实例