How to make a Fortran Makefiles with object files (.o) and a share library (.so)?

I am totally new to the world of Makefiles. I tried to follow some tutorials to make my own makefile. In the examples that I found there was no treatment for a case including object files (.o) and shared libraries (.so). So, I tried to inspire but it seems that there is something missing in my understanding of how to make a Fortran makefile.

I started with a simple example corresponding to the command that I generally use on the terminal:

gfortran -ffree-line-length-none -o binary A.F90 B.F90 C.F90 /Path/to/Directory/D.so

The command mentioned above works perfectly and lead to the creation of the executable called binary.

The corresponding Makefile that I wrote is:

FORTRAN_COMPILER=gfortran
#FORTRAN_FLAGS=-O3 -Wall -Wextra -std=f2008 -ffree-line-length-none
FORTRAN_FLAGS=-ffree-line-length-none
SRC=A.F90 B.F90 C.F90
OBJ1:${SRC:.F90=.o}
SharedLib_PATH = /Path/to/Directory
OBJ2 = $(SharedLib_PATH)/D.so
LIBS     = $(OBJ1) $(OBJ2) 


%.o: %.F90
    @echo 'converting .F90 files to .o'
    $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) -o $@ -c $<

binary: $(LIBS)
    @echo 'make an executable from objet files (.o) and the shared object (.so)'
    $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) -o $@ $(LIBS)

clean:  
    @echo 'clean'
    @rm -f *.mod *.o binary

By executing the Makefile, I have:

converting .F90 files to .o
gfortran -ffree-line-length-none -o A.o -c A.F90
converting .F90 files to .o
gfortran -ffree-line-length-none -o B.o -c B.F90
converting .F90 files to .o
gfortran -ffree-line-length-none -o C.o -c C.F90

And it does not go further and does not generate the executable binary.
Typing again on terminal "make" leads to: make: Nothing to be done for 'OBJ1'.
Could you please tell me where the problem comes from? and apologies if the problem is too obvious.

>Solution :

First, this is not right:

OBJ1:${SRC:.F90=.o}

This defines a rule with a target named OBJ1. I assume you wanted to define a variable named OBJ1. You should write:

OBJ1 = $(SRC:.F90=.o)

Second, it’s important to understand that by default, unless you give it a command line argument specifying otherwise, make will try to build the FIRST explicit target defined in the makefile. Due to the above typo the first explicit target is OBJ1 so that’s all it tries to build.

If you want make to build binary when you run it with no arguments, make sure that’s the first explicit target in your makefile.

Leave a Reply