简体   繁体   English

f2py 和 gfortran 的编译错误:对“main”的未定义引用

[英]compilation error with f2py and gfortran: undefined reference to `main'

I recently started using f2py and python2.7 for working on some codes related to an ocean model (ROMS) in Fortran.我最近开始使用 f2py 和 python2.7 来处理与 Fortran 中的海洋模型 (ROMS) 相关的一些代码。 My workstation specifics are Ubuntu 16.04 with python2.7, anaconda2 and gfortran.我的工作站细节是 Ubuntu 16.04,带有 python2.7、anaconda2 和 gfortran。 I have netcdf and hdf5 libraries installed as well.我也安装了 netcdf 和 hdf5 库。

The instruction in the makefile is the following (in one line): makefile 中的指令如下(在一行中):

$(F2PY) --fcompiler=gfortran --f90flags=$(F2PY_F90_FLAG) $(F2PY_LIB) $(F2PY_INCLUDE) $(OBJF2PY) -m -c scrip pyscrip.f90 $(F2PY) --fcompiler=gfortran --f90flags=$(F2PY_F90_FLAG) $(F2PY_LIB) $(F2PY_INCLUDE) $(OBJF2PY) -m -c 脚本 pyscrip.f90

where:在哪里:

  • F2PY links to the f2py compiler in the /bin directory. F2PY链接到/bin目录中的 f2py 编译器。
  • OBJF2PY is the set of object files to be compiled using f2py (all related source files are there in the directory). OBJF2PY是使用 f2py 编译的一组目标文件(所有相关的源文件都在目录中)。
  • F2PY_INCLUDE is the include path (contains netcdf.inc and netcdf.mod) F2PY_INCLUDE是包含路径(包含 netcdf.inc 和 netcdf.mod)
  • -L${LIBDIR} -lnetcdf -lnetcdff -lhdf5 -lhdf5_hl - is the library path, linked to the netcdf and hdf5 library files. -L${LIBDIR} -lnetcdf -lnetcdff -lhdf5 -lhdf5_hl - 是库路径,链接到 netcdf 和 hdf5 库文件。
  • F2PY_F90_FLAG = '-g -fdefault-real-8 -ffixed-form -O2 -fPIC -m64 -shared ' - the compiler flags linked to f2py. F2PY_F90_FLAG = '-g -fdefault-real-8 -ffixed-form -O2 -fPIC -m64 -shared ' - 链接到 f2py 的编译器标志。

During compilation, I am getting an error as given:在编译过程中,我收到一个错误:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start: (.text+0x20): undefined reference to 'main' collect2: error: ld returned 1 exit status

I could not understand why this error is showing up.我不明白为什么会出现这个错误。 Is the instruction in the correct order?指令的顺序是否正确? Does it have something to do with the compiler flags?它与编译器标志有关吗? Is there any other include file that the code needs (other than netcdf.inc), or any other library (other than netcdf and hdf5 libs)?是否有代码需要的任何其他包含文件(netcdf.inc 除外)或任何其他库(netcdf 和 hdf5 库除外)?

The answer is given on GitHub and is to add the -shared option to your LDFLAGS environment variable.答案在GitHub 上给出,是将-shared选项添加到LDFLAGS环境变量中。 Try LDFLAGS="-shared $LDFLAGS" before running f2py .在运行f2py之前尝试LDFLAGS="-shared $LDFLAGS"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM