Makefile causes `gfortran: error: No such file or directory` - makefile

I am trying to compile this code with gfortran.
Makefile:
# makefile for BIRRP
FC = gfortran
FCFLAGS = -g -fbounds-check
FLFLAGS = -g -fbacktrace
SRC = birrp.f coherence.f dataft.f diagnostic.f fft.f filter.f math.f rarfilt.f response.f rtpss.f utils.f weight.f zlinpack.f
# "make" builds all
PROGRAM = birrp
all: $(PROGRAM)
$(PROGRAM): $(SRC)
$(FC) $(FCFLAGS) $# $<
%.o: %.f
$(FC) $(FLFLAGS) -o $# $^
clean:
rm -f *.o
It doesn't work
gfortran -g -fbounds-check birrp birrp.f
gfortran: error: birrp: No such file or directory
Makefile:13: recipe for target 'birrp' failed
make: *** [birrp] Error 1
I am using the gfortran compiler. I have copied the Makefile from my other program.
After adding -o:
FC = gfortran
FCFLAGS = -g -c -fbounds-check
FLFLAGS = -g -fbacktrace
SRC = birrp.f coherence.f dataft.f diagnostic.f fft.f filter.f math.f rarfilt.f response.f rtpss.f utils.f weight.f zlinpack.f
# "make" builds all
PROGRAM = birrp
all: $(PROGRAM)
$(PROGRAM): $(SRC)
$(FC) $(FCFLAGS) -o $# $<
%.o: %.f
$(FC) $(FLFLAGS) -o $# $^
clean:
rm -f *.o
Now it just compiles
gfortran -g -c -fbounds-check -o birrp birrp.f
Why?

gfortran interprets birrp as a source-file, but you want it as the outfile.
man gfortran
says the outfile is specified by the -o parameter, so your target becomes this:
$(PROGRAM): $(SRC)
$(FC) $(FCFLAGS) -o $# $<
But this doesn't solve all your problems. According to the GNU Make manual $< specifies the first dependency and not all of them. You always want to have the whole list of dependencies, which would be $^.
So your target becomes this:
$(PROGRAM): $(SRC)
$(FC) $(FCFLAGS) -o $# $^

Related

Makefile change suffix rule based on target

Currently, I have two sets of flags in my Makefile. I comment one out if I want my program to be optimized or if I want more debugging information (see below).
FC = gfortran
# I switch between having one of the following two lines commented
# FFLAGS = -Og -Wall -Wextra -fcheck=all -fbacktrace -fbounds-check
FFLAGS = -O3
MOD='Modules/'
SRC = Modules/pyplot_module.f90
SRC += Modules/logs.f90
SRC += program.f90
OBJ = ${SRC:.f90=.o}
%.o: %.f90
$(FC) -c -J${MOD} $< $(FFLAGS) -o $#
program: $(OBJ)
$(FC) -J${MOD} $(FFLAGS) -o $# $(OBJ)
remake: remove program
clean:
rm -f **/*.o **/*.mod
remove: clean
rm -f program
Is there a way to have two different suffix rules and have a specific one used based on which target I call. I desire something like I have show below. I suspect this is an XY problem, but I don't know how else to approach this.
FC = gfortran
# Notice changes here
DEBUGFFLAGS = -Og -Wall -Wextra -fcheck=all -fbacktrace -fbounds-check
FFLAGS = -O3
MOD='Modules/'
SRC = Modules/pyplot_module.f90
SRC += Modules/logs.f90
SRC += program.f90
OBJ = ${SRC:.f90=.o}
%.o: %.f90
$(FC) -c -J${MOD} $< $(FFLAGS) -o $#
%.o: %.f90 # I want this suffix rule to be used only when program-debug target is made
$(FC) -c -J${MOD} $< $(DEBUGFFLAGS) -o $#
program-optimized: $(OBJ)
$(FC) -J${MOD} $(FFLAGS) -o $# $(OBJ)
program-debug: $(OBJ)
# But I also want the SRC files to be compiled with these flags aswell
$(FC) -J${MOD} $(DEBUGFFLAGS) -o $# $(OBJ)
remake: remove program
clean:
rm -f **/*.o **/*.mod
remove: clean
rm -f program
You can use target-specific variables to do this, like this:
DEBUGFFLAGS = -Og -Wall -Wextra -fcheck=all -fbacktrace -fbounds-check
FFLAGS = -O3
EXTRAFLAGS =
%.o: %.f90
$(FC) -c -J${MOD} $< $(FFLAGS) $(EXTRAFLAGS) -o $#
program-optimized program-debug: $(OBJ)
$(FC) -J${MOD} $(FFLAGS) $(EXTRAFLAGS) -o $# $(OBJ)
program-debug: EXTRAFLAGS = $(DEBUGFLAGS) # this is the magic
However, this is a bad idea.
The problem is make won't rebuild your object files when you change the flags, it only rebuilds when the files are edited. So, whenever you run make you could have some mix of object files compiled with optimized options and debug options, and you'll never know which are which unless you completely clean all object files in between builds.
Much better is to create a separate subdirectory to hold the object files built for debug versus optimized, then they don't conflict with each other.

Why does my Makefile with pattern rules not create debugging symbols for main?

I am using this auto-generated Makefile with pattern rules, that I oviously do not understand yet. I want to create debuggins symbols and then debug main, but it doesn't work. There is a -g flag. Adding $(LDFLAGS) statement above after $(ODIR) does not print one as expcted.
IDIR =./include
CC=g++
CFLAGS = -I$(IDIR)
LDFLAGS = -g
ODIR=./
LIBS=
_OBJ = main.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c
$(CC) -c -o $# $< $(CFLAGS)
main: $(OBJ)
$(CC) $(LDFLAGS) -o $# $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o
This is the terminal output
g++ -c -o Source.o Source.cpp
g++ -g -o Source Source.o -I./include
Your sources are C++ (.cpp) but your Makefile contains explicit instructions for building C files. Make is therefore falling back to its built in implicit rules.
Also note that by convention those rules use $(CXX) to refer to the C++ compiler, with $(CXXFLAGS) replacing $(CFLAGS), and the -I flag belongs in $(CPPFLAGS):
IDIR =./include
CPPFLAGS = -I$(IDIR)
CXXFLAGS = -g
ODIR=.
LIBS=
_OBJ = main.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cpp
$(CXX) -c -o $# $(CPPFLAGS) $(CXXFLAGS) $<
main: $(OBJ)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# $^ $(LIBS)
.PHONY: clean
clean:
rm -f $(OBJ)
If you were to do away with the ODIR handling and use the conventional variable names you could do without the explicit .o: .cpp rule altogether.

Explaining syntax of makefile

can anyone help me in interpreting the following makefile:
# compiler
FC = gfortran
# compile flags
FCFLAGS = -c -ffixed-line-length-72 -std=gnu -Wline-truncation -Wunused-variable
# link flags
FLFLAGS =
# module files
MODULES = Calculator.f
# object files
OBJS = Calculator.o
OBJS += Main.o Common.o Io.o
# program name
PROGRAM = calFap
all: $(PROGRAM)
$(PROGRAM): $(OBJS)
$(FC) $(FLFLAGS) -o $# $^
%.o: %.f
$(FC) $(FCFLAGS) -o $# $^
mod: $(MODULES)
$(FC) $(FCFLAGS) $# $^
clean:
rm -f *.o *.mod
What does this mean:
# module files
MODULES = Calculator.f
Is this just a kind of placeholder? So MODULES is "linked" to Calculator.f ? The file Calculator.f already exists.
# object files
OBJS = Calculator.o
OBJS += Main.o Common.o Io.o
So here I create a variable and I put in Calculator.o Main.o Common.o Io.o . But in my folder they do not exist up to now?
all: $(PROGRAM)
So the internet tells me, that "all" as a target is used in order to compile the whole program? But as far as I understand, makefiles are there for only compiling those parts of a programm which have changed?
$(PROGRAM): $(OBJS)
$(FC) $(FLFLAGS) -o $# $^
If there is a change in one of the OBJS files then something should be compiled. But I do not get the part with "-o $# $^" . Maybe all objects which have changed?
%.o: %.f
$(FC) $(FCFLAGS) -o $# $^
If there is a change in one of the .f files ...? I don't get this line.
For your help I would be really happy.
Thanks Helmut

How to make specific binary from specific object file?

Here is my makefile, i have object files in obj/ directory, and i need to compile them into binaries in bin/ folder, but somehow it doesn't work as i wanted it to work, any ideas?
SOURCES= $(wildcard *.c)
OBJECTS:= $(patsubst %.c, %.o, $(SOURCES))
OBJECTS:= $(addprefix obj/,$(OBJECTS))
NAMES:= $(patsubst %.c, %, $(SOURCES))
NAMES:= $(addprefix bin/,$(NAMES))
CC=gcc
CFLAGS= -Wall -c -o
DIRS = bin obj
all: $(DIRS) $(NAMES)
$(NAMES): $(OBJECTS)
$(CC) -o $# $<
obj/%.o: %.c
$(CC) $(CFLAGS) $# $<
$(DIRS):
mkdir -p $#
clean:
rm -rf $(DIRS)
Actual output:
mkdir -p bin
mkdir -p obj
gcc -Wall -c -o obj/task1.o task1.c
gcc -Wall -c -o obj/task2.o task2.c
gcc -Wall -c -o obj/task3.o task3.c
gcc -o bin/task1 obj/task1.o
gcc -o bin/task2 obj/task1.o
gcc -o bin/task3 obj/task1.o
Expected output:
mkdir -p bin
mkdir -p obj
gcc -Wall -c -o obj/task1.o task1.c
gcc -Wall -c -o obj/task2.o task2.c
gcc -Wall -c -o obj/task3.o task3.c
gcc -o bin/task1 obj/task1.o
gcc -o bin/task2 obj/task2.o
gcc -o bin/task3 obj/task3.o
In this rule:
$(NAMES): $(OBJECTS)
$(CC) -o $# $<
each executable depends on all objects. And since $< grabs only the first prerequisite, all you see is obj/task1.o.
Do it this way:
bin/%: obj/%.o
$(CC) -o $# $<
or this way:
$(NAMES): bin/% : obj/%.o
$(CC) -o $# $<

variable error in makefile

I have Makefile in Ubuntu12.10 as below.
when I run it by make command, it shows as
g++ -c -o myprog1.o myprog1.cpp
It seems the variables CFLAGS and CINCLUDE do not work.
Can any one help me to check it?
Thanks,
RM = rm -f
CC = gcc
AR = ar rc
CFLAGS= -Wall -g -O2 -std=c++11 -fPIC
CINCLUDE= -I. -I../include
OBJECTS= myprog1.o myprog2.o
STATIC_LIB = libctest.a
$(STATIC_LIB): $(OBJECTS)
$(AR) $(STATIC_LIB) $(OBJECTS)
%.o : %.c
$(CC) -c $(CFLAGS) $(CINCLUDE) $< -o $#
clean:
$(RM) $(OBJECTS)
$(RM) $(STATIC_LIB)
The problem is that you have a make rule for %.o : %.c but not for %.o : %.cpp. When you run the make, the implicit make rule for '%.cpp' runs, but the implicit rule doesn't contain references to CFLAGS or CINCLUDE.
Since your source is in c++, you probably want to add another rule for c++ compilation and set up the CXX variables appropriately.
CXXFLAGS = $(CFLAGS)
%.o : %.cpp
$(CXX) -c $(CXXFLAGS) $(CINCLUDE) $< -o $#

Resources