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

1k views Asked by At

I recently started using f2py and python2.7 for working on some codes related to an ocean model (ROMS) in Fortran. My workstation specifics are Ubuntu 16.04 with python2.7, anaconda2 and gfortran. I have netcdf and hdf5 libraries installed as well.

The instruction in the makefile is the following (in one line):

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

where:

  • F2PY links to the f2py compiler in the /bin directory.
  • OBJF2PY is the set of object files to be compiled using f2py (all related source files are there in the directory).
  • F2PY_INCLUDE is the include path (contains netcdf.inc and netcdf.mod)
  • -L${LIBDIR} -lnetcdf -lnetcdff -lhdf5 -lhdf5_hl - is the library path, linked to the netcdf and hdf5 library files.
  • F2PY_F90_FLAG = '-g -fdefault-real-8 -ffixed-form -O2 -fPIC -m64 -shared' - the compiler flags linked to 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)?

1

There are 1 answers

0
3lectrologos On

The answer is given on GitHub and is to add the -shared option to your LDFLAGS environment variable. Try LDFLAGS="-shared $LDFLAGS" before running f2py.