0

I've tried to create my own, small but universal Makefile for my university projects. What I managed to write is flowing code. The problem is, I have to run make all multiple times to compile all executables. The .o files and corresponding executables are created incrementally on every make call and not at once. How should it be done right?

# Variables
Project = Project_Name
Src     = src
Include = inc
Lib     = lib
Build   = build
Dist    = dist
Res     = resources
LibSem  = -L$(Lib) -lbib
CC      = gcc $(CFLAGS)

# Vpaths for implicite rules.
vpath %.c $(Src)
vpath %.h $(Include)
vpath %.o $(Build)
vpath %.d $(Build)
# Src is set for explicite named files.
CPPFLAGS += -I$(Src) -I$(Include)

# Define Source variable with the list of .c files
# File is updated by Vim by the call of "make update"
include build/Sources.inc

all: prog1 prog2
    cp -fr $(Res)/* $(Dist)

bib: libbib.o
    ar -cvrs $(Lib)/libbib.a $^

prog1: prog1.o sem
    $(CC) $(CFLAGS) $(CPPFLAGS) -o $(Dist)/$@ $< $(LibSem)

prog2: prog2.o sem
    $(CC) $(CFLAGS) $(CPPFLAGS) -o $(Dist)/$@ $< $(LibSem)

update:
    #----------------------------------------
    # Updating list of sources.
    #----------------------------------------
    echo -n "Sources = " > $(Build)/Sources.inc
    find src/ -type f -name '*.c' -printf "%f " >> $(Build)/Sources.inc

clean:
    #----------------------------------------
    # Cleaning files.
    #----------------------------------------
    rm -rf $(Build)/*
    rm -rf $(Dist)/*
    rm -rf $(Lib)/*
    touch $(Build)/Sources.inc

dist: clean dep all
    #----------------------------------------
    # Distribute archive.
    #----------------------------------------
    tar -cvzf $(Project).tar.gz $(Include) $(Res) $(Src) Makefile

# changed for output in Build directory
%.o: %.c
    $(COMPILE.c) -o $(Build)/$@ $<

# autogenerated dependencies files
-include $(subst .c,.d,$(Sources))

dep %.d: %.c
    #----------------------------------------
    # Generating auto-dependencies
    #----------------------------------------
    $(CC) -M $(CPPFLAGS) $< > $(Build)/$@.$$$$; \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $(Build)/$@.$$$$ > $(Build)/$@; \
    rm -f $(Build)/$@.$$$$
1

1 Answer 1

0

These rules are wrong:

prog1: prog1.o sem
        $(CC) $(CFLAGS) $(CPPFLAGS) -o $(Dist)/$@ $< $(LibSem)

Every "normal" rule in make must update the target you told make it would update. Here your rule tells make it will update the file prog1, but what your rule actually does is update the file dist/prog1. Those are completely different and your makefile does not work because you've lied to make.

In general it's not possible to use vpath to describe where to put generated targets. The vpath feature is only valid when used to locate source files.

You'll have to write your rule like this:

$(Dist)/prog1: $(Build)/prog1.o sem
        $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $< $(LibSem)

However that's just the tip of the iceberg. You'll need to make a lot more changes as a result of this to get things the way you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.