Store object files in separate directory using make - makefile

I'm running a makefile using mingw on windows. I've seen a lot of SO links about this topic, but they all seem to be for C or c++. I'm not sure if the same rules apply, and since I'm using windows, syntax seems to be a bit different too. Here are some of the other references:
How to place object files in separate subdirectory (most promising, I think)
Using a make file to compile files in separate directories
Flat object file directory structure output with GNU Make
What I currently have is (verbatim)
VPATH =\
user \
static \
computations \
solvers\\steadyState \
solvers\\transient \
solvers\\transient\\momentum \
solvers\\transient\\induction
FC = gfortran
TOP_DIR = .
MOD_DIR = $(TOP_DIR)\\mod
OBJ_DIR = $(TOP_DIR)\\obj
FCFLAGS = -g
FCFLAGS += -J$(MOD_DIR) -fopenmp -fimplicit-none -Wuninitialized
TARGET = parametricStudy
SRCS_F =\
user\\constants.f90 \
static\\myExceptions.f90 \
static\\myDebug.f90 \
static\\scalarField.f90 \
static\\vectorField.f90 \
static\\myIO.f90 \
user\\simParams.f90 \
static\\solverSettings.f90 \
static\\myTime.f90 \
computations\\myError.f90 \
static\\coordinates.f90 \
user\\griddata.f90 \
static\\myAllocate.f90 \
static\\BCs.f90 \
user\\rundata.f90 \
computations\\myDel.f90 \
computations\\vectorOps.f90 \
static\\myExport.f90 \
computations\\applyBCs.f90 \
solvers\\steadyState\\mySOR.f90 \
solvers\\steadyState\\myPoisson.f90 \
solvers\\transient\\induction\\initializeBBCs.f90 \
solvers\\transient\\induction\\initializeBfield.f90 \
solvers\\transient\\induction\\initializeSigmaMu.f90 \
solvers\\transient\\momentum\\initializeUBCs.f90 \
solvers\\transient\\momentum\\initializeUfield.f90 \
solvers\\transient\\inductionSolver.f90 \
solvers\\transient\\momentumSolver.f90 \
solvers\\transient\\MHDSolver.f90 \
user\\MOONS.f90 \
parametricStudy.f90
OBJS_F = $(patsubst %.f90,$(OBJ_DIR)\\%.o,$(notdir $(SRCS_F)))
all: $(TARGET)
$(TARGET): $(OBJS_F)
$(FC) -o $# $(FCFLAGS) $(OBJS_F)
$(OBJ_DIR)\\%.o: %.f90
$(FC) $(FCFLAGS) -c -o $# $<
clean:
del $(OBJ_DIR)\\*.o $(MOD_DIR)\\*.mod parametricStudy.exe
list:; #echo " "
#echo " "
#echo "Source files:"
#echo $(SRCS_F)
#echo " "
#echo "Object files:"
#echo $(OBJS_F)
#echo " "
#echo "Compiler : $(FC)"
#echo "Include directory : $(INC_DIR)"
#echo "Root directory : $(ROOT_DIR)"
#echo "Bin directory : $(BIN_DIR)"
#echo "Modules directory : $(MOD_DIR)"
#echo "Modules directory : $(MOD_DIR)"
#echo "Object directory : $(OBJ_DIR)"
#echo " "
Using this, I can execute the following (again, verbatim)
C:\Users\Charlie\Desktop\development\FORTRAN_LIB>gmake
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\const
ants.o user/constants.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myExc
eptions.o static/myExceptions.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myDeb
ug.o static/myDebug.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\scala
rField.o static/scalarField.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\vecto
rField.o static/vectorField.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myIO.
o static/myIO.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\simPa
rams.o user/simParams.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\solve
rSettings.o static/solverSettings.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myTim
e.o static/myTime.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myErr
or.o computations/myError.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\coord
inates.o static/coordinates.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\gridd
ata.o user/griddata.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myAll
ocate.o static/myAllocate.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\BCs.o
static/BCs.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\runda
ta.o user/rundata.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myDel
.o computations/myDel.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\vecto
rOps.o computations/vectorOps.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myExp
ort.o static/myExport.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\apply
BCs.o computations/applyBCs.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\mySOR
.o solvers\\steadyState/mySOR.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\myPoi
sson.o solvers\\steadyState/myPoisson.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\initi
alizeBBCs.o solvers\\transient\\induction/initializeBBCs.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\initi
alizeBfield.o solvers\\transient\\induction/initializeBfield.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\initi
alizeSigmaMu.o solvers\\transient\\induction/initializeSigmaMu.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\initi
alizeUBCs.o solvers\\transient\\momentum/initializeUBCs.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\initi
alizeUfield.o solvers\\transient\\momentum/initializeUfield.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\induc
tionSolver.o solvers\\transient\\induction/inductionSolver.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\momen
tumSolver.o solvers\\transient\\momentum/momentumSolver.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\MHDSo
lver.o solvers\\transient/MHDSolver.f90
gfortran -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized -c -o .\\obj\MOONS
.o user/MOONS.f90
gmake: *** No rule to make target `.\\obj\parametricStudy.o', needed by `paramet
ricStudy'. Stop.
C:\Users\Charlie\Desktop\development\FORTRAN_LIB>gfortran -g -J.\\mod -fopenmp -
c -o .\\obj\parametricStudy.o parametricStudy.f90
C:\Users\Charlie\Desktop\development\FORTRAN_LIB>gmake
gfortran -o parametricStudy -g -J.\\mod -fopenmp -fimplicit-none -Wuninitialized
.\\obj\constants.o .\\obj\myExceptions.o .\\obj\myDebug.o .\\obj\scalarField.o
.\\obj\vectorField.o .\\obj\myIO.o .\\obj\simParams.o .\\obj\solverSettings.o .\
\obj\myTime.o .\\obj\myError.o .\\obj\coordinates.o .\\obj\griddata.o .\\obj\myA
llocate.o .\\obj\BCs.o .\\obj\rundata.o .\\obj\myDel.o .\\obj\vectorOps.o .\\obj
\myExport.o .\\obj\applyBCs.o .\\obj\mySOR.o .\\obj\myPoisson.o .\\obj\initializ
eBBCs.o .\\obj\initializeBfield.o .\\obj\initializeSigmaMu.o .\\obj\initializeUB
Cs.o .\\obj\initializeUfield.o .\\obj\inductionSolver.o .\\obj\momentumSolver.o
.\\obj\MHDSolver.o .\\obj\MOONS.o .\\obj\parametricStudy.o
Note that after I receive the error, I can explicitly compile the parametricStudy.f90 after the error with:
gfortran -g -J.\\mod -fopenmp -c -o .\\obj\parametricStudy.o parametricStudy.f90
and then type
gmake
Again, which results in no errors. I'm very puzzled.
Here is a screenshot of my directory:
Maybe it has something to do with TARGET? It seems like the path of the last file is somehow wrong. Any help is greatly appreciated!

I think the following style of makefile solves the problems. I tested it with a specimen C
project, not fortran, but that is immaterial to the make problem and solution.
# VPATH: Tell `make` to look for in `user` for prerequisites it can't find here
VPATH = user
# If e.g. you also want `make` to look for for prerequisites in `../include`, then:
# VPATH = user:../include
FC = gfortran
TOP_DIR = .
MOD_DIR = $(TOP_DIR)\\mod
OBJ_DIR = $(TOP_DIR)\\obj
FCFLAGS = -g
FCFLAGS += -J$(MOD_DIR) -fopenmp -fimplicit-none -Wuninitialized
TARGET = parametricStudy
SRCS_F =\
user\\constants.f90 \
...
parametricStudy.f90
OBJS_T1 = $(patsubst %.f90,%.o,$(SRCS_F))
OBJS_T2 = $(notdir $(OBJS_T1))
# The object files are all to be obj\<name>.o
OBJS_F = $(patsubst %.o,$(OBJ_DIR)\\%.o,$(OBJS_T2))
all: $(TARGET)
$(TARGET): $(OBJS_F)
$(FC) -o $# $(FCFLAGS) $(OBJS_F)
# How to make an obj\*.o from the matching *.f90. `make` considers the VPATH
$(OBJ_DIR)\\%.o: %.f90
$(FC) $(FCFLAGS) -c -o $# $<
clean:
del $(OBJ_DIR)\\*.o $(MOD_DIR)\\*.mod parametricStudy.exe
Using a relative path TOP_DIR = . rather than an absolute ROOT_DIR is good advice already offered
by #Wintermute.
To enable the required pattern rule:
$(OBJ_DIR)\\%.o: %.f90
to kick in, you must make it appear to make that any prerequisite *.f90 is right here, as per the pattern,
not in some other directory like, e.g. user\constants.f90 That is what the VPATH
achieves.
Continued for later developments
Not yet having seen a listing of the directory where the makefile resides I
can only venture a hypothesis, but the hypothesis suggested by what I do see is:
The directory does not actually contain a file called parametricStudy.f90,
but a file called parametricStudy.F90, and if it is renamed to parametricStudy.f90,
then the makefile will find and compile it.
Is that right?
How this explains the facts: The pattern rule:
$(OBJ_DIR)\\%.o: %.f90
is failing to match any parametricStudy.f90, so there is no such file. You say however that:
gfortran -g -J.\\mod -fopenmp -c -o .\\obj\parametricStudy.o parametricStudy.f90
successfully compiles.
You are building on Windows, so the toolchain subscribes to Windows' file-handling protocols.
Filenames are case-insentive: parametricStudy.f90 will identify parametricStudy.F90, if it exists,
and .F90 will be interpreted by gfortran (on Windows or anywhere else) as denoting a
Fortran 90 source file. Thus the successful commandline compile.
But the pattern rule is indifferent to file-handling protocols. It is just a pattern
matching rule, and the invariant .f90 is not matched by .F90.

The problem you're facing is that with
ROOT_DIR = "C:\Users\Charlie\"
OBJ_DIR = $(ROOT_DIR)\obj
the rule
$(OBJ_DIR)/%.o: %.f90
expands to
"C:\Users\Charlie\"obj/%.o: %.f90
which is parsed as the static pattern rule
"C: \Users\Charlie\"obj/%.o: %.f90
That is to say, with target "C, target pattern \Users\Charlie\"obj/%.o and prerequisite pattern %.f90. make complains that "C does not match the pattern \Users\Charlie\"obj/%.o.
There is some hacky code in GNU make (at least in MinGW; I think Cygwin behaves differently because it expects you to work with its unix-ish directory structure) to recognize absolute Windows paths, but it does not handle quoting. As long as your OBJ_DIR does not contain spaces, using
ROOT_DIR = C:\Users\Charlie\
should make the Windows path recognition kick in.
However...it is rather unusual to see absolute paths in handcrafted Makefiles. Are you sure you want to do it this way? A more common approach would be to work with relative paths so that the Makefile does not have to be changed if the source code is copied to a different directory. Assuming the Makefile is in the root directory, that would be
ROOT_DIR = .
or just nixing the ROOT_DIR variable altogether and saying
MOD_DIR = mod
OBJ_DIR = obj
Oh, and to answer the next question that's going to crop up: In order to make make use the
$(OBJ_DIR)/%.o: %.f90
rule, you'll need to make $(TARGET) have prerequisites that match the $(OBJ_DIR)/%.o pattern. That could be
OBJS_F = $(patsubst %.f90,$(OBJ_DIR)/%.o,$(notdir $(SRCS_F)))

Related

GNU make is adding an extra step not in my Makefile that causes all sorts of linker errors. What's going on?

Given the following makefile for GNU make:
# TODOs so I don't forget:
# - make debugging an option
# - make 64 below an actual option
# - figure out why make test seems to rebuild the DLL
# - __declspec(dllimport)
ifeq ($(MAKECMDGOALS),64)
CC = x86_64-w64-mingw32-gcc
RC = x86_64-w64-mingw32-windres
mflag = -m64
else
CC = i686-w64-mingw32-gcc
RC = i686-w64-mingw32-windres
mflag = -m32
endif
OBJDIR = .objs
OUTDIR = out
BASENAME = wintable
DLLFILE = $(OUTDIR)/$(BASENAME).dll
LIBFILE = $(OUTDIR)/$(BASENAME).lib
TESTEXEFILE = $(OUTDIR)/$(BASENAME).exe
CFILES = \
alloc.c \
api.c \
checkboxdraw.c \
checkboxevents.c \
children.c \
coord.c \
debug.c \
draw.c \
events.c \
header.c \
hscroll.c \
main.c \
metrics.c \
modelhelpers.c \
nullmodel.c \
resize.c \
scroll.c \
select.c \
update.c \
util.c \
visibility.c \
vscroll.c
HFILES = \
table.h \
tablepriv.h
TESTCFILES = \
test.c
OFILES = $(CFILES:%.c=$(OBJDIR)/%.o)
TESTOFILES = $(TESTCFILES:%.c=$(OBJDIR)/%.o)
xCFLAGS = \
--std=c99 \
-Wall \
-Wextra \
-Wno-unused-parameter \
$(mflag) \
$(CFLAGS)
xLDFLAGS = \
-static-libgcc \
-luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lole32 -loleaut32 -loleacc -luuid -lmsimg32 \
$(mflag) \
$(LDFLAGS)
default:
$(MAKE) clean
$(MAKE) it
$(MAKE) test
it: $(DLLFILE)
$(DLLFILE): $(OFILES)
$(CC) -g -o $(DLLFILE) -shared -Wl,--out-implib,$(LIBFILE) $(OFILES) $(xLDFLAGS)
test: $(TESTEXEFILE)
$(TESTEXEFILE): $(DLLFILE) $(TESTOFILES)
$(CC) -g -o $(TESTEXEFILE) $(TESTOFILES) $(LIBFILE) $(xLDFLAGS)
$(OBJDIR)/%.o: %.c $(HFILES) dirs
$(CC) -g -o $# -c $< $(xCFLAGS)
dirs:
mkdir -p $(OBJDIR) $(OUTDIR)
clean:
rm -rf $(OBJDIR) $(OUTDIR)
make -n test produces
mkdir -p .objs out
i686-w64-mingw32-gcc -g -o .objs/alloc.o -c alloc.c --std=c99 -Wall -Wextra -Wno-unused-parameter -m32
i686-w64-mingw32-gcc -g -o .objs/api.o -c api.c --std=c99 -Wall -Wextra -Wno-unused-parameter -m32
(and so on for all the other C files)
i686-w64-mingw32-gcc -g -o out/wintable.dll -shared -Wl,--out-implib,out/wintable.lib .objs/alloc.o .objs/api.o .objs/checkboxdraw.o .objs/checkboxevents.o .objs/children.o .objs/coord.o .objs/debug.o .objs/draw.o .objs/events.o .objs/header.o .objs/hscroll.o .objs/main.o .objs/metrics.o .objs/modelhelpers.o .objs/nullmodel.o .objs/resize.o .objs/scroll.o .objs/select.o .objs/update.o .objs/util.o .objs/visibility.o .objs/vscroll.o -static-libgcc -luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lole32 -loleaut32 -loleacc -luuid -lmsimg32 -m32
i686-w64-mingw32-gcc -g -o .objs/test.o -c test.c --std=c99 -Wall -Wextra -Wno-unused-parameter -m32
i686-w64-mingw32-gcc -g -o out/wintable.exe .objs/test.o out/wintable.lib -static-libgcc -luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lole32 -loleaut32 -loleacc -luuid -lmsimg32 -m32
i686-w64-mingw32-gcc test.c out/wintable.exe -o test
Notice how the last step that GNU make decides to do tries to recompile my test.c with the correct output executable file built before that into a new test executable, which (predictably) fails spectacularly:
out/wintable.exe: In function `WinMainCRTStartup':
/build/buildd/mingw-w64-3.1.0/build/i686-w64-mingw32-i686-w64-mingw32-crt/../../mingw-w64-crt/crt/crtexe.c:171: multiple definition of `WinMainCRTStartup'
/usr/lib/gcc/i686-w64-mingw32/4.9-win32/../../../../i686-w64-mingw32/lib/../lib/crt2.o:/build/buildd/mingw-w64-3.1.0/build/i686-w64-mingw32-i686-w64-mingw32-crt/../../mingw-w64-crt/crt/crtexe.c:171: first defined here
out/wintable.exe: In function `mainCRTStartup':
/build/buildd/mingw-w64-3.1.0/build/i686-w64-mingw32-i686-w64-mingw32-crt/../../mingw-w64-crt/crt/crtexe.c:199: multiple definition of `mainCRTStartup'
/usr/lib/gcc/i686-w64-mingw32/4.9-win32/../../../../i686-w64-mingw32/lib/../lib/crt2.o:/build/buildd/mingw-w64-3.1.0/build/i686-w64-mingw32-i686-w64-mingw32-crt/../../mingw-w64-crt/crt/crtexe.c:199: first defined here
out/wintable.exe:cygming-crtbegin.c:(.text+0x500): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/i686-w64-mingw32/4.9-win32/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
out/wintable.exe:cygming-crtbegin.c:(.text+0x550): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/i686-w64-mingw32/4.9-win32/crtbegin.o:cygming-crtbegin.c:(.text+0x50): first defined here
out/wintable.exe: In function `mainwinCreate':
/home/pietro/src/github.com/andlabs/wintable/test.c:20: multiple definition of `mainwinCreate'
/tmp/cc50VrIz.o:test.c:(.text+0x0): first defined here
out/wintable.exe: In function `mainwinDestroy':
/home/pietro/src/github.com/andlabs/wintable/test.c:66: multiple definition of `mainwinDestroy'
/tmp/cc50VrIz.o:test.c:(.text+0x2ba): first defined here
(and so on for virtually every symbol in my test.c)
/usr/lib/gcc/i686-w64-mingw32/4.9-win32/crtbegin.o:cygming-crtbegin.c:(.text+0x22): undefined reference to `_Jv_RegisterClasses'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'test' failed
make: *** [test] Error 1
Where is this last step coming from? It's nowhere in my makefile, as far as I can tell. (Notice the lack of compiler flags.) Googling the problem was also ineffective here. This only started to happen today, with all the same rules as before (the only change is the addition of modelhelpers.c and nullmodel.c to CFILES), so I'm not sure what's going on.
Does it have to do with the rule being test? If so, why did it work before?
This is GNU make 4.0 on Ubuntu GNOME 14.10.
Thanks.
Make has various built-in implicit rules, and one of them is the pattern rule % : %.c, which tells make how to create an executable (in UNIX systems, executables don't have any extension like .exe etc., they're just words like test, mkdir, etc.) if it has a source file with the same extension.
Your makefile has:
test: $(TESTEXEFILE)
and you provide no recipe for building the target test, so make looks at its built-in rules and finds % : %.c, and then make looks and sees that you DO have a test.c file, so make applies that default recipe to build the target test, which you asked it to do.
If you don't want this to happen you should tell make that test is not a real target: that you're only using it as a handle to build other things, using the .PHONY special target:
.PHONY: test
From the manual:
The implicit rule search is skipped for .PHONY targets.

gfortran make circular dependency dropped

I'm running a makefile using GNU Make 4.1 on windows. I've seen a lot of SO links about this topic, but they all seem to be for C or c++. I'm not sure if the same rules apply, and since I'm using windows, syntax seems to be a bit different too.
Here's my make file:
FC = gfortran
FCFLAGS = -O0 -Og -Wall -pedantic -fbacktrace -fcheck=all
# FCFLAGS = -O2
MODDIR = "bin"
FCFLAGS += -J$(MODDIR) -fopenmp -fimplicit-none -Wuninitialized
SRCS_C =\
gridFun.f90 \
test.f90
OBJS_C = $(SRCS_C:.c=.o)
TARGET = test
all: $(TARGET)
$(TARGET): $(OBJS_C)
$(FC) -o $# $(FCFLAGS) $(OBJS_C)
$(OBJS_C): $(SRCS_C)
$(FC) $(FCFLAGS) -c $(SRCS_C)
cleanMod:
del *.mod
cleanObj:
del *.o
I run my make file with
gmake
and I've noticed that
mingw32-make
seems to produce the same result. The error I'm getting is:
gmake: Circular gridFun.f90 <- gridFun.f90 dependency dropped.
gmake: Circular test.f90 <- gridFun.f90 dependency dropped.
gmake: Circular test.f90 <- test.f90 dependency dropped.
gfortran -O0 -Og -Wall -pedantic -fbacktrace -fcheck=all -J"bin" -fopenmp -fimpl
icit-none -Wuninitialized -c gridFun.f90 test.f90
gfortran -o test -O0 -Og -Wall -pedantic -fbacktrace -fcheck=all -J"bin" -fopenm
p -fimplicit-none -Wuninitialized gridFun.f90 test.f90
Any help about how to fix this, and maybe an explanation would be greatly appreciated!
You don't have c sources so the _C suffix on variables is inaccurate (harmless but confusing).
The real issue is with this OBJS_C = $(SRCS_C:.c=.o) substitution ref.
That's expecting to change .c files into .o files but you don't have any .c files.
Change that to OBJS_C = $(SRCS_C:.f90=.o) and it should work for you.

generic makefile to create multiple executable using different flags

I would like to create a generic Makefile that builds several executables using different compiler flags for each executable without using shell commands. The executable file name should be composed from the source file and a unique post fixed name. It should also produce an assembly or preprocessor file per source file if needed.
For the target BIN_BDG_FILES, the "$<" (exercise-1.1.0.c ) is always the first item from the list (exercise-1.1.0.c exercise-1.1.1.c exercise-1.2.0.c exercise-1.2.1.c) as expected. I tried without success to modify the SRC_FILES using the filter-out function. My intent was to remove the first item from the list for each Target, so that the first item corresponds to the correct target. I am not sure this is the correct approach. Your comments are welcome.
i.e.
This is my attempt at using built in make constructs.
$(BIN_DBG_FILES): $(SRC_FILES)
$(CC) $(DBG_CFLAGS) $(IFLAGS) $< -o $#
echo SRC_FILES := $(filter-out $<, $(SRC_FILES))
Makefile
SHELL = bash
SRC_FILES = $(wildcard *.c)
BIN_FILES = $(patsubst %.c,%,$(SRC_FILES))
BIN_DBG_FILES = $(patsubst %.c,%-dbg,$(SRC_FILES))
SRC_PRE = $(patsubst %.c,%-pre,$(SRC_FILES))
CC = gcc
WARNINGS := -Wall
CFLAGS = -O2 -std=c99 $(WARNINGS)
DBG_CFLAGS = -g -O -std=c99 $(WARNINGS)
PRE_FLAG = -E
IFLAGS = -I.
all: $(BIN_FILES) $(BIN_DBG_FILES) MK-BASH
$(BIN_DBG_FILES): $(SRC_FILES)
$(CC) $(DBG_CFLAGS) $(IFLAGS) $< -o $#
MK-BASH::
for src in $(SRC_FILES); do \
echo $(CC) $(DBG_CFLAGS) $(IFLAGS) $$src -o $${src%.c}-dbg; \
$(CC) $(DBG_CFLAGS) $(IFLAGS) $$src -o $${src%.c}-dbg; \
$(CC) $(DBG_CFLAGS) $(IFLAGS) $$src -o $${src%.c}-dbg; \
$(CC) $(PRE_FLAG) $$src > $${src%.c}-pre; \
done
clean:
rm -f $(BIN_FILES) *-dbg *-pre
This is the output from executing make command.
This is the output from the target BIN_FILES.
gcc -O2 -std=c99 -Wall exercise-1.1.0.c -o exercise-1.1.0
gcc -O2 -std=c99 -Wall exercise-1.1.1.c -o exercise-1.1.1
gcc -O2 -std=c99 -Wall exercise-1.2.0.c -o exercise-1.2.0
gcc -O2 -std=c99 -Wall exercise-1.2.1.c -o exercise-1.2.1
This is the output from target BIN_DBG_FILES which uses the first source file on the list to build all targets. It should use the appropriate file (exercise-1.1.1.c) to build each target file (exercise-1.1.1-dbg).
gcc -g -O -std=c99 -Wall -I. **exercise-1.1.0.c** -o exercise-1.1.0-dbg
gcc -g -O -std=c99 -Wall -I. **exercise-1.1.0.c** -o exercise-1.1.1-dbg
gcc -g -O -std=c99 -Wall -I. **exercise-1.1.0.c** -o exercise-1.2.0-dbg
gcc -g -O -std=c99 -Wall -I. **exercise-1.1.0.c** -o exercise-1.2.1-dbg
This is the output from the target MK-BASH using shell commands.
for src in exercise-1.1.0.c exercise-1.1.1.c exercise-1.2.0.c exercise-1.2.1.c; do \
echo gcc -g -O -std=c99 -Wall -I. $src -o ${src%.c}-dbg; \
gcc -g -O -std=c99 -Wall -I. $src -o ${src%.c}-dbg; \
gcc -g -O -std=c99 -Wall -I. $src -o ${src%.c}-dbg; \
gcc -E $src > ${src%.c}-pre; \
done
output:
gcc -g -O -std=c99 -Wall -I. exercise-1.1.0.c -o exercise-1.1.0-dbg
gcc -g -O -std=c99 -Wall -I. exercise-1.1.1.c -o exercise-1.1.1-dbg
gcc -g -O -std=c99 -Wall -I. exercise-1.2.0.c -o exercise-1.2.0-dbg
gcc -g -O -std=c99 -Wall -I. exercise-1.2.1.c -o exercise-1.2.1-dbg
Use a pattern rule:
DBG: $(BIN_DBG_FILES)
%-dbg: %.c
#echo $(CC) $(DBG_CFLAGS) $(IFLAGS) $< -o $#

Make wildcard dependency No rule to make target needed by

I am trying to build my targets with wildcards. Here is my Makefile:
BINARY = main
LDSCRIPT = stm32f4-discovery.ld
PREFIX ?= arm-none-eabi
CC = $(PREFIX)-gcc
LD = $(PREFIX)-gcc
OBJCOPY = $(PREFIX)-objcopy
CFLAGS += -Os -g \
-Wall -Wextra -Wimplicit-function-declaration \
-Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes \
-Wundef -Wshadow \
-I$(TOOLCHAIN_DIR)/include \
-fno-common -mcpu=cortex-m4 -mthumb \
-mfloat-abi=hard -mfpu=fpv4-sp-d16 -MD -DSTM32F4
LDSCRIPT ?= $(BINARY).ld
LDFLAGS += --static -lc -lnosys -L$(TOOLCHAIN_DIR)/lib \
-L$(TOOLCHAIN_DIR)/lib/stm32/f4 \
-T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \
-mthumb -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
OBJS += $(BINARY).o
all: images
images: $(BINARY).images
%.images: %.bin
##printf "*** $* images generated ***\n"
%.bin: %.elf
##printf " OBJCOPY $(*).bin\n"
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f4.a
##printf " LD $(subst $(shell pwd)/,,$(#))\n"
$(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32f4 $(LDFLAGS)
%.o: %.c Makefile
##printf " CC $(subst $(shell pwd)/,,$(#))\n"
$(Q)$(CC) $(CFLAGS) -o $# -c $<
clean:
$(Q)rm -f *.o
$(Q)rm -f *.d
$(Q)rm -f *.elf
$(Q)rm -f *.bin
-include $(OBJS:.o=.d)
When I make, I get the error:
make: *** No rule to make target `main.bin', needed by `main.images'. Stop.
I am trying to make a single image, so I can change each % to $(BINARY), and this works, but I would like to figure out why this isn't working.
You are likely missing a dependency somewhere down the chain.
Note: Some people forget that the "Makefile" itself, when placed on the dependency line - is also checked for existance. Double check that you haven't renamed it.
Like #MadScientist recommends, "make -d" will be your friend here. Use that to locate the rule/file that should have been found but was not.
I simplified your example (on Linux) and got the same type of error with:
Makefile:
all: main.out
%.out: %.elf
g++ $^ -o $#
%.elf: %.obj
mv $^ $#
%.obj: %.cpp Makefile
g++ -c $^ -o $#
Create main.cpp, see it work, then rename main.cpp to something else that it can't find, run again, see it fail.
Now rename the Makefile itself, see the same failure.

Creating a simple Makefile to build a shared library

I am trying to create a very basic hand crafted Makefile to create a shared library to illustrate a point.
This is what I have so far:
SHELL = /bin/sh
CC = gcc
FLAGS = -std=gnu99 -Iinclude
CFLAGS = -fPIC -pedantic -Wall -Wextra -march=native -ggdb3
DEBUGFLAGS = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program
TARGET = example.so
SOURCES = $(shell echo src/*.c)
HEADERS = $(shell echo include/*.h)
OBJECTS = $(SOURCES:.c=.o)
PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(FLAGS) $(CFLAGS) $(DEBUGFLAGS) -o $(TARGET) $(OBJECTS)
When I run make, it attempts to build an application - and ld fails because it can't resolve main().
Problem seems to be with CFLAGS - I have specified -fPIC but that is not working - what am I doing wrong?
Edit
I added the -shared flag as suggested, when I run make, I got this error:
gcc -std=gnu99 -Iinclude -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3 -O0 -D _DEBUG -o example.so src/example.o
/usr/bin/ld: src/example.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
src/example.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [example.so] Error 1
Which seems to be suggesting to revert back to -fPIC only.
BTW, my new CFLAGS setting is:
CFLAGS = -fPIC -shared -pedantic -Wall -Wextra -march=native -ggdb3
I am running gcc v4.4.3 on Ubuntu 10.0.4.
The solution was to modify the XXFLAGS as follows:
FLAGS = # -std=gnu99 -Iinclude
CFLAGS = -fPIC -g #-pedantic -Wall -Wextra -ggdb3
LDFLAGS = -shared
Compile with -shared:
gcc -o libfoo.so module1.o module2.o -shared
(This also works on MingW under Windows to produce DLLs.)
Example for C++ files. I've also included a clean target.
.PHONY : clean
CPPFLAGS= -fPIC -g
LDFLAGS= -shared
SOURCES = $(shell echo *.cpp)
HEADERS = $(shell echo *.h)
OBJECTS=$(SOURCES:.cpp=.o)
FIKSENGINE_LIBDIR=../../../../lib
FIKSENGINE_INCDIR=../../../../include
TARGET=$(FIKSENGINE_LIBDIR)/tinyxml.so
all: $(TARGET)
clean:
rm -f $(OBJECTS) $(TARGET)
$(TARGET) : $(OBJECTS)
$(CC) $(CPPFLAGS) $(OBJECTS) -o $# $(LDFLAGS)
Since you try to build so file, you probably need -shared.
this is my goto makefile rule for so files:
%.so: %.o ; $(LINK.c) $(LDFLAGS) -shared $^ -o $#
can be used like so
CFLAGS+=-fPIC
libmyfoo.so: # create from libmyfoo.o
# or
libmyfoo.so: myfoo.o # create from myfoo.o

Resources