On 09.03.11 16:34, Krzysztof Cieniuch wrote:
The first problem with your makefile is that you cannot use automatic
variable ($@) in prerequisite list unless you turn on
second expansion (I don't use that feature so do not know much about it
but use in sample below :-))).
thanks your example it helped me to understand some basic rules.
I have now a very nice Makefile ready which rebuilds .c files if
required .h files are changed.
And also rebuild if the CFLAGS are changed so it does exactly what I need.
I attached my short short example so anyone how has the same problem can
use it as a template.
Bye,
Matthias
--
"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the universe trying to
produce bigger and better idiots. So far, the universe is winning." --
Rich Cook
# Makefile template which rebuilds if dependecies and/or compile options are
changed
# Copyright (c) 2011 Matthias Fechner <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# define what we would like to build
PROJECTNAME=test
PRJSRC=a.c hello.c
TRG=$(PROJECTNAME).o
# define some required variables we use
CC:=gcc
REMOVE:=rm -f
CFLAGS+=-O3 -I. $(WARN_FLAGS)
WARN_FLAGS:=-Wall -g
CMP:=cmp
# --- End of config section ---
# --- Build section ---
# Define the standard target to execute if no target is given
ifeq ($(.DEFAULT_GOAL),)
.DEFAULT_GOAL:=all
endif
# Variables with source files and dependecies
CFILES=$(filter %.c, $(PRJSRC))
OBJDEPS=$(CFILES:.c=.o)
-include $(OBJDEPS:.o=.d)
# check if cflags are different to the build before
.PHONY: FORCE
compiler_flags: FORCE
@echo '$(CFLAGS)' | $(CMP) -s - $@ || echo '$(CFLAGS)' > $@
# target definitions
all: $(TRG)
debug: CFLAGS+=-DTEST
debug: all
$(TRG): $(OBJDEPS)
$(CC) $(OBJDEPS) $(LDFLAGS) -o $(TRG)
%.o: %.c compiler_flags
@$(CC) -M $(CFLAGS) -o $(basename $(@)).d $<
$(CC) -c $(CFLAGS) -o $@ $<
.PHONY: clean
clean:
$(REMOVE) $(TRG)
$(REMOVE) $(OBJDEPS)
$(REMOVE) $(OBJDEPS:.o=.d)
$(REMOVE) compiler_flags
void printHello(void);
void bla(void);
#include <stdio.h>
#include <hello.h>
void printHello()
{
printf("Hello from function\n");
#ifdef TEST
printf("Additional test line\n");
#endif
}
#include <stdio.h>
#include <hello.h>
int main(void)
{
printf("Hello\n");
printHello();
return 0;
}
_______________________________________________
Help-make mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-make