0

I am trying to compile some Fortran code using intel oneapi compiler (ifort -v 2021.2.0) and linking it with another Fortran library (MultiZ) previously coded. I am using the following makefile:

# User parameters

LIBPATHS  = /RouteToLibrary/
LIBFLAGS  = -lmultiz

# Options. 1: use gfortran. 2: use ifort.
OP = 2

# Fortran compilers

# GFC: GNU Fortran Compiler
# IFC: Intel Fortran Compiler
GFC = gfortran
IFC = ifort

# Common flags

# -g:     Produce symbolic debug information.
# -c:     Compile to object (.o) only, do not link.
# -pg:    Profile with gprof

DEBUG   = -g
FCFLAGS = -c $(DEBUG) -pg
FLFLAGS =

# GNU Fortran Flags

GFCFLAGS = -fdefault-real-8 -fdefault-double-8 -Wall -pedantic -std=f2008 
GFLFLAGS = 

# Intel Fortran Flags

# IFCFLAGS = -autodouble -warn
IFCFLAGS = -warn
IFLFLAGS = 

# Lists and variables

ifeq ($(OP),1)
  FC      := $(GFC)
  FCFLAGS := $(FCFLAGS) $(GFCFLAGS)
  FLFLAGS := $(FLFLAGS) $(GFLFLAGS)
else
  FC      := $(IFC)
  FCFLAGS := $(FCFLAGS) $(IFCFLAGS)
  FLFLAGS := $(FLFLAGS) $(IFLFLAGS)
endif

# List of example files, objects and executables.
FILES := $(wildcard *.f90)
OBJS  := $(patsubst %.f90, %.o,   $(FILES))
EXEC  := $(patsubst %.f90, %.exe, $(FILES))

# List of filenames without extension.
NOEX  := $(subst .f90,,$(FILES))

LPATHS := $(foreach PATH, $(LIBPATHS), -L$(realpath $(PATH)))
IPATHS := $(foreach PATH, $(LIBPATHS), -I$(realpath $(PATH)))

define MAKE_EXE
  $1.exe : $1.o ; $(FC) $(FLFLAGS) $(LPATHS) $1.o $(LIBFLAGS) -o $1.exe
endef

# Rules.

all : $(EXEC)

$(foreach f, $(NOEX), $(eval $(call MAKE_EXE, $f)))

%.o : %.f90
    $(FC) $(FCFLAGS) $(IPATHS) $< -o $@

clean :
    rm -f $(EXEC) $(OBJS)

However, I am getting the following error message:

/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
MyMainScript.o: In function `MyFunction_':

And after some warning messages, the following error message is displayed:

collect2: error: ld returned 1 exit status
make: *** [MyMainScript] Error 1
wovano
  • 4,543
  • 5
  • 22
  • 49
  • My compilation line is: make MyMainScript – Juan David Navarro Nov 22 '21 at 17:28
  • 2
    Do you believe that your Fortran source has a [main program defined](https://stackoverflow.com/a/66861849/3157076)? Assuming everything is fine with your build process (looking at `make -n` for example), we'd probably need to see something about the source to be able to answer. (It also seems that there's more to the error message about `MyFunction_`?) – francescalus Nov 22 '21 at 17:50
  • @francescalus Thank you very much for your reply, This is happening even for the simplest source: program MyMainScript implicit none print*, 'this program does nothing' CALL MyFunction(1) end program MyMainScript SUBROUTINE MyFunction(number) INTEGER, INTENT(IN) :: number print*, number END SUBROUTINE MyFunction And, that all the error message that I am getting... – Juan David Navarro Nov 22 '21 at 18:10
  • Your makefile complicates things for this simple case (and without the file structure isn't reproducible), so can you show the commands which are actually being executed when you run make (with `make --trace` if supported)? – francescalus Nov 22 '21 at 18:39
  • Did you already search for an answer on Stack Overflow? This is quite a common error message, at least in C/C++, but this also [has been asked](https://stackoverflow.com/questions/27753150/undefined-reference-to-main-when-compiling-a-module) for Fortran before. – wovano Nov 27 '21 at 10:35

0 Answers0