Fortran Makefile with 2 executables - makefile

I'm trying to compile 2 F90 files and to execute each one separately using a Makefile. The language used is fortran. I am not used to this so I need your help.
The first file is called HECESE_avec_modif.f90 and the second one is called hecese_seq.f90.
The makefile is below :
# Define dependance for suffix .FFREE
# -----------------------------------
.SUFFIXES: .exe .out .o .f .f90
#
# Compilation options for ifort
# ---------------------------------
# ---------------------------------
#FLAGS_fred = -free -O2 -traceback -mcmodel=large\
# -check overflow -check bounds
#FLAGS_fred = -FR -free -ftz -O3 -fPIC
#F90_COMPILER = /opt/intel/bin/ifort
#---------------------------------------------------------
#
# Compilation options for G95
#
#FLAGS_fred = -C -O3 -ffree-form -ffree-line-length-huge -fzero
#FLAGS_fred = -O5 -ffree-form -ffree-line-length-huge -fzero
#F90_COMPILER = g95
#
# Compilation options for GFortran
FLAGS_fred = -ffree-form -ffree-line-length-none -O3 #-march=native
F90_COMPILER = gfortran -m64
F90_COMPILER7 = gfortran-7 -m64
#FLAGS_fred = -O3
#F90_COMPILER = gfortran
#FLAGS_CPPFOR = -L/usr/local/gfortran/lib/x86_64/
#
#
#FLAGS_fred = -free -O4
#
#
# ***************************************************
# ***************************************************
# ** **
# ** definition of file groups for F. Waymel **
# ** **
# ***************************************************
# ***************************************************
#
# 0.) group of all modules containing common variables
# for multiblocs initialisation
# ----------------------------------------------------
#
MAIN = HECESE_avec_modif.f90
MAIN2 = hecese_seq.f90
#
# Compilation of object files :
# -----------------------------
#
MAIN_OBJ =$(MAIN:.f90=.o)
MAIN_OBJ2 =$(MAIN2:.f90=.o)
# Creation of executable:
# ---------------------------------
#
#DEST1 = ~/bin
EXEC = hecesevfinale.out
EXEC2 = heceseseq.out
all : $(EXEC) $(EXEC2)
$(EXEC):$(MAIN_OBJ)
$(F90_COMPILER) -g $(FLAGS_fred) -o $(EXEC) $(MAIN_OBJ)
$(EXEC2):$(MAIN_OBJ2)
$(F90_COMPILER) -g $(FLAGS_fred) -o $(EXEC2) $(MAIN_OBJ2)
#
%.o : %.mod
.f90.o:
$(F90_COMPILER) -g $(FLAGS_fred) -c $<
# $(F90_COMPILER) -g $(FLAGS_fred) -c $<
%.o : %.mod
.f90.o:
$(F90_COMPILER) -g $(FLAGS_fred) -c $<
# $(F90_COMPILER) -g $(FLAGS_fred) -c $<
clean:
rm -f *.o *.mod *.out
#depend .depend:
# makedepf90 metas.f90 $(SOURCES90_fred1) $(SOURCES90_fred2) $(SOURCES90_mwl) $(SOURCES90_mwl2) > .depend
#include .depend
I think that I made a mistake related to the second file.
The message I get when I execute this makefile is :
gfortran -m64 -g -ffree-form -ffree-line-length-none -O3 -o hecesevfinale.out HECESE_avec_modif.o
make: *** No rule to make target 'hecese_seq.o', needed by 'heceseseq.out'. Stop.
I have another problem when I try to execute make clean.
I get :
makefile:82: warning: overriding recipe for target '.f90.o'
makefile:76: warning: ignoring old recipe for target '.f90.o'
Can you help me, please ?

Related

How to compile free-form and fixed-form Fortran codes with ifort in linux

I try to compile free-form Fortran code (param0.f90, param1.f90, and param2.f90), in which contains alot of parameters for tsunami modelling. There is only single main program in fixed-form (tunami.for) in this model package. I am struggling to compile and link the code by using the makefile. Here the makefile that I used.
#-----------BEGIN MAKEFILE-------------------------------------------------
DEF_FLAGS = -P -traditional
EXEC = TUNAMI
#==========================================================================
#--------------------------------------------------------------------------
# PRECISION DEFAULT PRECISION: SINGLE
# UNCOMMENT TO SELECT DOUBLE PRECISION
#--------------------------------------------------------------------------
# FLAG_1 = -DDOUBLE_PRECISION
# FLAG_2 = -DPARALLEL
# FLAG_3 = -DLANDSLIDE
# FLAG_4 = -DSALINITY
# FLAG_5 = -DTEMPERATURE
# FLAG_6 = -DBUBBLE
# FLAG_7 = -DSEDIMENT
# FLAG_8 = -DVEGETATION
FLAG_9 = -DINTEL
# FLAG_10 = -DBALANCE2D
# FLAG_12 = -DTWOLAYERSLIDE
# FLAG_13 = -DCORALREEF
#--------------------------------------------------------------------------
# mpi defs
#--------------------------------------------------------------------------
CPP = /usr/bin/cpp
CPPFLAGS = $(DEF_FLAGS)
# FC = ifort
FC = ifort
DEBFLGS =
OPT = #-g
CLIB =
#==========================================================================
FFLAGS = $(DEBFLGS) $(OPT)
MDEPFLAGS = --cpp --fext=f90 --file=-
RANLIB = ranlib
#--------------------------------------------------------------------------
# CAT Preprocessing Flags
#--------------------------------------------------------------------------
CPPARGS = $(CPPFLAGS) $(DEF_FLAGS) $(FLAG_1) $(FLAG_2) $(FLAG_3) \
$(FLAG_4) $(FLAG_5) $(FLAG_6) $(FLAG_7) $(FLAG_8) $(FLAG_9) \
$(FLAG_10) $(FLAG_11) $(FLAG_12) $(FLAG_13)
#--------------------------------------------------------------------------
# Libraries
#--------------------------------------------------------------------------
LIBS = -L/home/gugum/Downloads/hypre/src/hypre/lib -lHYPRE
INCS = -L/home/gugum/Downloads/hypre/src/hypre/include
#--------------------------------------------------------------------------
# Preprocessing and Compilation Directives
#--------------------------------------------------------------------------
.SUFFIXES: .o .f90 .F .F90
.c.o:
$(CC) -c $(CFLAGS) $(INCS) $*.c
.F.o:
$(CPP) $(CPPARGS) $(PETSC_FC_INCLUDES) $*.F > $*.f90
$(FC) -c $(FFLAGS) $(INCS) $*.f90
\rm $*.f90
.F90.o:
$(CPP) $(CPPFLAGS) $(CPPDEFS) $(INCLDIR) $< > $*.f
$(FC) -c $(FFLAGS) $(FIXEDFLAGS) $(INCS) $(INCLDIR) $*.f
\rm $*.f90
# \rm $*.f90
#--------------------------------------------------------------------------
# NHWAVE Source Code.
#--------------------------------------------------------------------------
MODS = param0.f90 param1.f90 param2.f90
MAIN = TUNAMI.for
SRCS = $(MODS) $(MAIN)
#OBJS = $(SRCS:.F=.o) param0.o param1.o param2.o
OBJS = $(patsubst %.for,%.o,$(patsubst %.f90,%.o,$(SRCS)))
#--------------------------------------------------------------------------
# Linking Directives
#--------------------------------------------------------------------------
$(EXEC): $(OBJS)
$(FC) $(FFLAGS) $(LDFLAGS) -o $(EXEC) $(OBJS) $(LIBS)
#--------------------------------------------------------------------------
# Cleaning targets.
#--------------------------------------------------------------------------
clean:
/bin/rm -f *.o *.mod
clobber: clean
/bin/rm -f *.f90 *.o tunami
However, it produces an error.
make: *** No rule to make target 'param0.o', needed by 'TUNAMI'. Stop.

Adding static library file to the makefile

I have following makefile executed by python script:
SOURCES_DIRECTORY = sources
# These directories should be created by build.py script
BUILD_DIRECTORY = builds
OBJECTS_DIRECTORY = objs
CORE = cortex-m4
LINKER_SCRIPT = linker_rom.ld
# C definitions
ifeq ($(RAM_FLASH), 1)
C_DEFS = -DRAM_FLASH
else
C_DEFS =
endif
INC_DIRS = sources
SRCS_DIRS = sources
# extension of C files
C_EXT = c
# wildcard for C source files (all files with C_EXT extension found in current
# folder and SRCS_DIRS folders will be compiled and linked)
C_SRCS = $(wildcard $(patsubst %, %/*.$(C_EXT), . $(SRCS_DIRS)))
# extension of ASM files
AS_EXT = s
# wildcard for ASM source files (all files with AS_EXT extension found in
# current folder and SRCS_DIRS folders will be compiled and linked)
AS_SRCS = $(wildcard $(patsubst %, %/*.$(AS_EXT), . $(SRCS_DIRS)))
# optimization flags ("-O0" - no optimization, "-O1" - optimize, "-O2" -
# optimize even more, "-Os" - optimize for size or "-O3" - optimize yet more)
OPTIMIZATION = -Og
# set to 1 to optimize size by removing unused code and data during link phase
REMOVE_UNUSED = 1
C_WARNINGS = -Wall -Wextra -Werror
# C language standard ("c89" / "iso9899:1990", "iso9899:199409",
# "c99" / "iso9899:1999", "gnu89" - default, "gnu99")
C_STD = c99
VPATH = $(SRCS_DIRS)
#============================================#
# Toolchain Configuration #
#============================================#
TOOLCHAIN = arm-none-eabi-
CXX = $(TOOLCHAIN)g++
CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)gcc -x assembler-with-cpp
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
RM = rm -f
.PHONY: all clean
#=============================================================================#
# various compilation flags
#=============================================================================#
# core flags
CORE_FLAGS = -mcpu=$(CORE) -mthumb -specs=nano.specs -specs=nosys.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard
# flags for C compiler
C_FLAGS = -std=$(C_STD) -g -ggdb3 -fverbose-asm -Wa,-ahlms=$(OBJECTS_DIRECTORY)/$(notdir $(<:.$(C_EXT)=.lst)) -DSTM32F429xx $(FLAGS)
# flags for assembler
AS_FLAGS = -g -ggdb3 -Wa,-amhls=$(OBJECTS_DIRECTORY)/$(notdir $(<:.$(AS_EXT)=.lst)) $(FLAGS)
# flags for linker
LD_FLAGS = -T$(LINKER_SCRIPT) -g -Wl,-Map=$(OBJECTS_DIRECTORY)/test.map,--cref,--no-warn-mismatch
# process option for removing unused code
ifeq ($(REMOVE_UNUSED), 1)
LD_FLAGS += -Wl,--gc-sections
OPTIMIZATION += -ffunction-sections -fdata-sections
endif
LIBS = libmylib.a
LIB_DIRS = ./sources
#=============================================================================#
# do some formatting
#=============================================================================#
C_OBJS = $(addprefix $(OBJECTS_DIRECTORY)/, $(notdir $(C_SRCS:.$(C_EXT)=.o)))
AS_OBJS = $(addprefix $(OBJECTS_DIRECTORY)/, $(notdir $(AS_SRCS:.$(AS_EXT)=.o)))
OBJS = $(AS_OBJS) $(C_OBJS) $(CXX_OBJS) $(USER_OBJS)
DEPS = $(OBJS:.o=.d)
INC_DIRS_F = -I. $(patsubst %, -I%, $(INC_DIRS))
LIB_DIRS_F = $(patsubst %, -L%, $(LIB_DIRS))
ELF = $(BUILD_DIRECTORY)/$(NAME).elf
HEX = $(BUILD_DIRECTORY)/$(NAME).hex
BIN = $(BUILD_DIRECTORY)/$(NAME).bin
LSS = $(BUILD_DIRECTORY)/$(NAME).lss
DMP = $(BUILD_DIRECTORY)/$(NAME).dmp
# format final flags for tools, request dependancies for C++, C and asm
C_FLAGS_F = $(CORE_FLAGS) $(OPTIMIZATION) $(C_WARNINGS) $(C_FLAGS) $(C_DEFS) -MD -MP -MF $(OBJECTS_DIRECTORY)/$(#F:.o=.d) $(INC_DIRS_F)
AS_FLAGS_F = $(CORE_FLAGS) $(AS_FLAGS) $(AS_DEFS) -MD -MP -MF $(OBJECTS_DIRECTORY)/$(#F:.o=.d) $(INC_DIRS_F)
LD_FLAGS_F = $(CORE_FLAGS) $(LD_FLAGS) $(LIB_DIRS_F)
#contents of output directory
GENERATED = $(wildcard $(patsubst %, $(BUILD_DIRECTORY)/*.%, bin d dmp elf hex lss lst map o))
#=============================================================================#
# make all
#=============================================================================#
all : $(ELF) $(LSS) $(DMP) $(HEX) $(BIN)
# make object files dependent on Makefile
$(OBJS) : $(MAKEFILE_NAME)
# make .elf file dependent on linker script
$(ELF) : $(LINKER_SCRIPT)
#-----------------------------------------------------------------------------#
# linking - objects -> elf
#-----------------------------------------------------------------------------#
$(ELF) : $(OBJS)
$(CC) $(LD_FLAGS_F) $(OBJS) $(LIBS) -o $#
#-----------------------------------------------------------------------------#
# compiling - C source -> objects
#-----------------------------------------------------------------------------#
objs/%.o : %.$(C_EXT)
$(CC) -c $(C_FLAGS_F) $< -o $#
#-----------------------------------------------------------------------------#
# assembling - ASM source -> objects
#-----------------------------------------------------------------------------#
objs/%.o : %.$(AS_EXT)
$(AS) -c $(AS_FLAGS_F) $< -o $#
#-----------------------------------------------------------------------------#
# memory images - elf -> hex, elf -> bin
#-----------------------------------------------------------------------------#
$(HEX) : $(ELF)
$(OBJCOPY) -O ihex $< $#
$(BIN) : $(ELF)
$(OBJCOPY) -O binary $< $#
#-----------------------------------------------------------------------------#
# memory dump - elf -> dmp
#-----------------------------------------------------------------------------#
$(DMP) : $(ELF)
$(OBJDUMP) -x --syms $< > $#
#-----------------------------------------------------------------------------#
# extended listing - elf -> lss
#-----------------------------------------------------------------------------#
$(LSS) : $(ELF)
$(OBJDUMP) -S $< > $#
# include dependancy files
-include $(DEPS)
This is the project tree
-> /sources/
-> /sources/libmylib.a
-> /makefile
I thought adding
LIBS = libmylib.a
LIB_DIRS = ./sources
will solve the problem. My lib is libmylib.a file. However console still outputs the errors with undefined references to functions. What should I change?
Path to the directories with sources, objects, headers and flags are passed dynamically by the python script as arguments.
It should be:
LIBS = -lmylib
The compiler will prepend lib and append .a to the libraries you specify, making it look for a library file named libmylib.a which is what you want.

make 'match 'is up to date

When I run the code of siftDemoV4 I get the following error:
make 'match 'is up to date
What's wrong with this? I know that there is something wrong with the Makefile:
# ------------------ Compilation options ------------------------
# Loads math library.
LIBS = -lm
# Flags for the C compiler:
# -Wall for strict gcc warnings (requires prototypes for all functions).
# -g to produce debug data for gdb
# -O for optimization
CFLAGS = -Wall -g
CC = gcc
# --------------------- Code modules ----------------------------
# Object files
OBJ = match.o util.o
# Definitions
DEFS = defs.h
# ------------------------ Rules --------------------------------
match: ${OBJ}
${CC} -o $# ${CFLAGS} ${OBJ} ${LIBS}
# Implicit rule used by Gnu Make: $(CC) -c $(CPPFLAGS) $(CFLAGS)
${OBJ}: ${DEFS}

Gfortran compiling error after replacing with f77

QI need to compile some files using gfortran. I went to the makefile and replaced all the "f77" with "gfortran". However I get this error when I do "make"
gfortran -c verbal.f
gfortran -c trgl6_octa.f
gfortran -c trgl6_icos.f
gfortran -c gauss_trgl.f
gfortran -c gauss_leg.f
gfortran -c sgf_3d_fs.f
gfortran -c sgf_3d_w.f
f77 -c -o sgf_3d_2p_w.o sgf_3d_2p_w.f
make: f77: Command not found
make: *** [sgf_3d_2p_w.o] Error 127
I do not understand where in the make file (pasted below) there is a hidden f77 that did not get replaced. I checked all the sources files for "f77" and there was none. I am very confused.
#
# Objects
# -------
#
OBJ0 = verbal.o
OBJ1 = trgl6_octa.o trgl6_icos.o gauss_trgl.o gauss_leg.o
OBJ2 = sgf_3d_fs.o sgf_3d_w.o sgf_3d_2p_w.o
OBJ2A = sgf_3d_3p.o sgf_3d_3p_ewald.o sgf_3d_3p_qqq.o
OBJ3 = prtcl_3d_mob.o
OBJ30 = elm_geom.o abc.o interp_p.o printel.o
OBJ33 = slp_trgl6.o slp_trgl6_sing.o slp_trgl3_sing.o
OBJ4 = gel.o gel_inv.o
OBJ = $(OBJ0) $(OBJ1) $(OBJ2) $(OBJ2A) $(OBJ3) $(OBJ30) $(OBJ33) $(OBJ4)
#
# link
# ----
#
prtcl_3d_mob: $(OBJ)
gfortran -c prtcl_3d_mob $(OBJ)
#
# compile
# ------
#
prtcl_3d_mob.o: prtcl_3d_mob.f
gfortran -c prtcl_3d_mob.f
trgl6_octa.o: trgl6_octa.f
gfortran -c trgl6_octa.f
trgl6_icos.o: trgl6_icos.f
gfortran -c trgl6_icos.f
verbal.o: verbal.f
gfortran -c verbal.f
sgf_3d_fs.o: sgf_3d_fs.f
gfortran -c sgf_3d_fs.f
sgf_3d_w.o: sgf_3d_w.f
gfortran -c sgf_3d_w.f
sgf_3d_3p.o: sgf_3d_3p.f
gfortran -c sgf_3d_3p.f
sgf_3d_3p_ewald.o: sgf_3d_3p_ewald.f
gfortran -c sgf_3d_3p_ewald.f
sgf_3d_3p_qqq.o: sgf_3d_3p_qqq.f
gfortran -c sgf_3d_3p_qqq.f
gel.o: gel.f
gfortran -c gel.f
gel_inv.o: gel_inv.f
gfortran -c gel_inv.f
prtcl_3d_geo.o: prtcl_3d_geo.f
gfortran -c prtcl_3d_geo.f
interp_p.o: interp_p.f
gfortran -c interp_p.f
abc.o: abc.f
gfortran -c abc.f
printel.o: printel.f
gfortran -c printel.f
elm_geom.o: elm_geom.f
gfortran -c elm_geom.f
slp_trgl6.o: slp_trgl6.f
gfortran -c slp_trgl6.f
slp_trgl6_sing.o: slp_trgl6_sing.f
gfortran -c slp_trgl6_sing.f
slp_trgl3_sing.o: slp_trgl3_sing.f
gfortran -c slp_trgl3_sing.f
gauss_leg.o: gauss_leg.f
gfortran -c gauss_leg.f
gauss_trgl.o: gauss_trgl.f
gfortran -c gauss_trgl.f
#
# clean
# -----
#
clean:
rm -f core
rm -f $(OBJ) prtcl_3d_mob
rm -f prtcl_3d_mob.net prtcl_3d_mob.out
rm -f matrix_inverse.out
rm -f particle_elements.out
#
# purge
# ---
#
purge:
rm -f core
rm -f $(OBJ) prtcl_3d_mob
rm -f prtcl_3d_mob.net prtcl_3d_mob.out
rm -f matrix_inverse.out
rm -f particle_elements.out
#
# clobber
# ---
#
clobber:
rm *
#
# all
# ---
#
all:
make prtcl_3d_mob
GNU make has a number of implicit rules https://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules
By default, it will compile .f files with the rule $(FC) $(FFLAGS) -c where by default FC is set to f77 (see link above for all of this).
As an explicit rule ("from .f to .o") is not found for a file, make invokes the default rule.
Solutions: either one of the two
Set FC to gfortran
add an explicit rule for sgf_3d_2p_w.o
In general, I write the rule myself as
%.o: %.f90
$(FC) $(FFLAGS) -c $<
in the Makefile. It makes it obvious that I need to define FC and FFLAGS.
Also, you can then remove all of the individual rules for compiling your files.

Get make to recompile only unchanged source files

I'm trying to get my Makefile to compile only changed source files. In the Makefile below, I would like the targets: %.o : ${SOURCE_DIR}/%.c and %.o : ${SOURCE_DIR}/%.s to only compile if the source file has changed, or the corresponding .o object file does not exist.
Not entirely sure what's wrong here, can someone please offer some advice?
# Project name
# ---------------------------------------------------------------------------------------------------------------------
PROJECT_NAME = stm32f4_template
# Source configuration
# ---------------------------------------------------------------------------------------------------------------------
OUT_DIR = ./Build
SOURCE_DIR = ./Src
SOURCES = main.c
SOURCES += startup.s
C_SOURCES = $(filter %.c, $(SOURCES))
ASM_SOURCES += $(filter %.s, $(SOURCES))
OBJECTS = $(C_SOURCES:.c=.o)
OBJECTS += $(ASM_SOURCES:.s=.o)
# Tools
# ---------------------------------------------------------------------------------------------------------------------
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld -v
CP = arm-none-eabi-objcopy
OD = arm-none-eabi-objdump
# Compilation, linker and other tool flags
# ---------------------------------------------------------------------------------------------------------------------
CFLAGS = -I./ -c -fno-common -O0 -g -mcpu=cortex-m4 -mthumb
LFLAGS = -nostartfiles -TLinker/memory.ld -TLinker/sections.ld
CPFLAGS = -Obinary
ODFLAGS = -S
# Target: all ---------------------------------------------------------------------------------------------------------
#
all: setup $(PROJECT_NAME).elf
#echo "Done! $?"
# Target: setup -------------------------------------------------------------------------------------------------------
#
setup:
#mkdir -p $(OUT_DIR)
# Target: $(PROJECT_NAME).elf
# ---------------------------------------------------------------------------------------------------------------------
$(PROJECT_NAME).elf: $(OBJECTS)
#echo "Linking $#"
$(LD) $(LFLAGS) -o ${OUT_DIR}/main.elf $(OUT_DIR)/main.o
#echo
# Target %.o (.c sources)
# ---------------------------------------------------------------------------------------------------------------------
%.o : ${SOURCE_DIR}/%.c # --> Execute only if source changed!!!
#echo "Compiling $<"
$(CC) $(CFLAGS) $< -o $(OUT_DIR)/$#
#echo
# Target %.o (.s sources)
# ---------------------------------------------------------------------------------------------------------------------
%.o : ${SOURCE_DIR}/%.s # --> Execute only if source changed!!!
#echo "Compiling $<"
$(CC) $(CFLAGS) $< -o $(OUT_DIR)/$#
#echo
# Target: clean
# ---------------------------------------------------------------------------------------------------------------------
clean:
#echo "Cleaning build output..."
#rm -rf $(OUT_DIR)
This rule:
%.o : ${SOURCE_DIR}/%.c # --> Execute only if source changed!!!
#echo "Compiling $<"
$(CC) $(CFLAGS) $< -o $(OUT_DIR)/$#
The problem is that the target of this rule is main.o, so Make uses it in an attempt to build main.o, because another target demands main.o, but what this rule actually builds is Build/main.o. Make keeps running this rule because it sees that main.o isn't there (and the rule for the elf file uses Build/main.o, which Make keeps rebuilding unawares).
I suggest you change it:
OBJECTS = $(patsubst %.c, $(OUT_DIR)/%.o, $(C_SOURCES))
$(OUT_DIR)/%.o : ${SOURCE_DIR}/%.c # --> this should work
#echo "Compiling $<"
$(CC) $(CFLAGS) $< -o $#
The same goes for the other %.o rule.

Resources