Makefile for building multiple programs

I have the following part of a makefile and want to simplify it
using rules rather than having to code the same two blocks
when I need ti build another program.

An having difficulty doing it

all: 1dvel2 1dvel 2dvel


#--------------------------------------------------------------------

1dvel2 : 1dvel2.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $(BINDIR)/1dvel2 $(OBJDIR)/1dvel2.o

1dvel2.o : $(MISC_MAINDIR)/1dvel2.f
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

#--------------------------------------------------------------------

1dvel : 1dvel.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $(BINDIR)/1dvel $(OBJDIR)/1dvel.o

1dvel.o : $(MISC_MAINDIR)/1dvel.f
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

#--------------------------------------------------------------------

2dvel : 2dvel.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $(BINDIR)/2dvel $(OBJDIR)/2dvel.o

2dvel.o : $(MISC_MAINDIR)/2dvel.f
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

---------- Post updated at 11:42 AM ---------- Previous update was at 06:52 AM ----------

I am now doing like this. Now the problem in putting the separate executable
targets together

TARGETS = $(BINDIR)/1dvel2 $(BINDIR)/1dvel $(BINDIR)/2dvel
all: $(TARGETS)

$(BINDIR)/1dvel2 : $(OBJDIR)/1dvel2.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $@ $<

$(BINDIR)/1dvel : $(OBJDIR)/1dvel.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $@ $<

$(BINDIR)/2dvel : $(OBJDIR)/2dvel.o $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -o $@ $<

#--------------------------------------------------------------------

$(OBJDIR)/%.o : $(MISC_MAINDIR)/%.f
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

Why do you want the following line?

TARGETS = $(BINDIR)/1dvel2 $(BINDIR)/1dvel $(BINDIR)/2dvel
all: $(TARGETS)

Do you wanna create a new executable with these three binaries?

Why are you moving the object files? If you do, make will always think the object files are missing and force a rebuild.

I think you should place the makefile and source files in the same directory. Let make buidl the programs in that directory. Add a seperate clean target that removes temporary files.

He has heard and ignored this advice before; as with most other problems, he insists on his own unique(tm) solutions.

Make and source files can be in different folders.

I don't understand how the three targets are combined into one.

The problem is that I do not want to combine the targets into one. Now I have another makefile like this. A problem I have is that the makefile
is not printing anything when building the targets.

# Fortran linker
FLINKER = gfortran

# Fortran compiler
FCOMPILER = gfortran

MAINDIR = ../../../libs/main
MISC_MAINDIR = $(MAINDIR)/misc
OBJDIR = ../release/obj
BINDIR = ../release/bin

MISC_INCLUDE = $(MISC_MAINDIR)/ray.par

LDFLAGS :=
CFLAGS_INC :=
CFLAGS := -g -Wall $(CFLAGS_INC)

# SOURCE FILES
SRCS := $(wildcard $(MISC_MAINDIR)/*.f)
SRCNAMES :=  $(notdir $(SRCS))

# OBJECT FILES
OBJS = $(subst .f,.o,$(subst $(MISC_MAINDIR),$(OBJDIR),$(SRCS)))
OBJNAMES :=  $(patsubst %.f,%,$(notdir $(OBJS)))

# LIST OF MISC EXECUTABLES
TARGETS = $(patsubst %.f,%,$(subst $(MISC_MAINDIR),$(BINDIR),$(SRCS)))
TARGETNAMES :=  $(patsubst %.f,%,$(notdir $(TARGETS)))

all : $(OBJS) $(TARGETS)

$(TARGETS) : 
    for T in $(TARGETNAMES) ; do \
      $(FCOMPILER) -o $(BINDIR)/$$T $(OBJDIR)/$$T.o ; \
    done

$(OBJDIR)/%.o : $(MISC_MAINDIR)/%.f $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

Ok Ok. So what am I supposed to do as I do not like all object files, makefiles and executables in the same directory. Or is it that that things are done this way?

---------- Post updated at 01:32 PM ---------- Previous update was at 01:26 PM ----------

I think there is a way how to make the compiler to know where the .o files are.

---------- Post updated at 03:12 PM ---------- Previous update was at 01:32 PM ----------

This is the new make file.

I noticed that when I do

make all

the object files are removed. Cannot figure out how make issues a remove at the end

# Fortran linker
FLINKER = gfortran

# Fortran compiler
FCOMPILER = gfortran

RM_OPT = --preserve-root --verbose

MAINDIR = ../../../libs/main
MISC_MAINDIR = $(MAINDIR)/misc
OBJDIR = ../release/obj
BINDIR = ../release/bin

MISC_INCLUDE = $(MISC_MAINDIR)/ray.par

LDFLAGS :=
CFLAGS_INC :=
CFLAGS := -g -Wall $(CFLAGS_INC)

# SOURCE FILES
SRCS := $(wildcard $(MISC_MAINDIR)/*.f)
SRCNAMES :=  $(notdir $(SRCS))

# OBJECT FILES
OBJS = $(subst .f,.o,$(subst $(MISC_MAINDIR),$(OBJDIR),$(SRCS)))
OBJNAMES :=  $(patsubst %.f,%,$(notdir $(OBJS)))

# LIST OF MISC EXECUTABLES
TARGETS = $(patsubst %.f,%,$(subst $(MISC_MAINDIR),$(BINDIR),$(SRCS)))
TARGETNAMES :=  $(patsubst %.f,%,$(notdir $(TARGETS)))

.PHONY : default
default : help

all : $(TARGETS)

$(BINDIR)/% : $(OBJDIR)/%.o
    $(FCOMPILER) -o $@ $^

# COMPILE MISC MAIN PROGRAMS
$(OBJDIR)/%.o : $(MISC_MAINDIR)/%.f $(MISC_MAINDIR)/ray.par
    $(FCOMPILER) -c $<
    mv *.o $(OBJDIR)

# DELETE OBJECT AND EXECUTABLE FILES

.PHONY: clean cleanall clean_objects clean_targets

clean : cleanall
cleanall : clean_objects clean_targets

clean_objects :
    -$(RM) $(RM_OPT) $(OBJS)

clean_targets :
    -$(RM) $(RM_OPT) $(TARGETS)

.PHONY : help
help :
    @echo ""
    @echo "Operating System Detected: $(OPSYS) "
    @echo " "
    @echo "USAGE: "
    @echo ""
    @echo "  make -f misc.mk        To get this listing"
    @echo "  make -f misc.mk help   To get this listing"
    @echo "  make -f misc.mk all    Builds the application xzslice"
    @echo "  make -f misc.mk list   List the details of building nraypk"
    @echo "  make -f misc.mk clean  Remove object and executable files"
    @echo ""
    @echo "OBJDIR = $(OBJDIR)"
    @echo ""
    @echo "OBJS = $(OBJS)"
    @echo ""
    @echo "TARGETNAMES = $(TARGETNAMES)"
    @echo ""

MSG1_SRCNAMES = $(firstword $(SRCNAMES))
MSG2_SRCNAMES = $(wordlist 2, $(words $(SRCNAMES)), $(SRCNAMES))

MSG1_OBJNAMES = $(firstword $(OBJNAMES))
MSG2_OBJNAMES = $(wordlist 2, $(words $(OBJNAMES)), $(OBJNAMES))

.PHONY : list
list :
    @echo ""
    @echo "SETUP"
    @echo "====="
    @echo ""
    @echo "  OPSYS:      $(OPSYS)"
    @echo "  EXEC:       $(EXEC_NRAYPK) $(EXEC_XRAYPK)"
    @echo "  TARGET:     $(TARGET_NRAYPK) $(TARGET_XRAYPK)"
    @echo "  FCOMPILER:  $(FCOMPILER)"
    @echo "  FLINKER:    $(FLINKER)"
    @echo "  CCOMPILER:  $(CCOMPILER)"
    @echo "  OPMZ:       $(OPMZ_OPTN)"
    @echo "  CURDIR:     $(CURDIR)"
    @echo ""
    @echo ""
    @echo "DIRECTORIES"
    @echo "==========="
    @echo ""
    @echo "  OBJDIR = $(OBJDIR)"
    @echo "  BINDIR = $(BINDIR)"
    @echo ""
    @echo "TARGETS"
    @echo "======="
    @echo ""
    @echo "  TARGETS = $(TARGETS)"
    @echo ""
    @echo "  TARGETNAMES = $(TARGETNAMES)"
    @echo ""
    @echo "INCLUDE FILES"
    @echo "============="
    @echo ""
    @echo "  MISC_INCLUDE = $(MISC_INCLUDE)"
    @echo ""
    @echo "SOURCE FILES"
    @echo "============"
    @echo ""
    @echo "  SRCNAMES = $(MSG1_SRCNAMES)"
    @$(foreach s, $(MSG2_SRCNAMES), echo "            $(s)";)
    @echo ""
    @echo "OBJECT FILES"
    @echo "============"
    @echo ""
    @echo "  OBJS = $(OBJS)"
    @echo ""
    @echo "  OBJNAMES = $(MSG1_OBJNAMES)"
    @$(foreach s, $(MSG2_OBJNAMES), echo "            $(s)";)
    @echo ""

edit:

bakunin

They are there because you put them there. This was a nicely organized project, until you jammed them all into one folder and makefile. I still don't understand why you did that after being shown ways to control them without dumping them in one big pile.