0

I've just started looking at make files but was unsure of what the default behaviour (running the command make with no parameters) of a make file is when targets after the 1st are not dependancies of the 1st .

myprogram: main.o foo.o
    gcc -o myprogram main.o foo.o

main.o: main.c foo.h
    gcc -c main.c

foo.o: foo.c foo.h
    gcc -c foo.c

main2.o: main2.c foo2.h
    gcc -c main2.c

foo2.o: foo2.c foo2.h
    gcc -c foo2.c

So from the above if I were to run make and the files main2.c and foo2.c were updated would the commands gcc -c main2.c and gcc -c foo2.c both run?

kdhp
  • 2,096
  • 14
  • 15
Anon957
  • 539
  • 2
  • 6
  • 24
  • Your `Makefile` is ugly. Perhaps adapt [this example](http://stackoverflow.com/a/20146082/841108) to your needs – Basile Starynkevitch Nov 06 '14 at 05:57
  • Wouldn't *trying it* be easier than posting the question? – Beta Nov 06 '14 at 06:03
  • 1
    only the first target in a make file is processed, when no specific target is given on the command line. To get all the targets to be processed, when no specific target is given, then BEFORE any other target, insert the lines: SRC = (wildcard *.c) then OBJ = ($(SRC): .c:.o) then INC = ($(SRC): .c:.h) then the first target (normally named 'all') all: myprogram $(OBJ) makefile – user3629249 Nov 06 '14 at 06:49
  • all these kinds of process blocks: foo2.o: foo2.c foo2.h gcc -c foo2.c can be reduced to a single process block: .o:.c (newline+tab)gcc $(CFLAGS) $< -o $@ and a process block: myprogram: $(OBJ) (newline+tab) gcc $(LDFLAGS) $(OBJ) -o $@ – user3629249 Nov 06 '14 at 06:57

2 Answers2

3

In your example no, main2.o and foo2.o would not be updated. This is because they are not needed to make myprogram, which is the default target because it is the first target in the file, and no default target is specified with .DEFAULT.

make with no target makes the targets defined by the special target .DEFAULT, if no targets are specified then it makes the first target found in the specified, or default, makefile.

kdhp
  • 2,096
  • 14
  • 15
0

A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed when you make an explicit request. There are two reasons to use a phony target: to avoid a conflict with a file of the same name, and to improve performance.

.PHONY=all

//line below might not be necessary to your program but here are some flags

OPTS=-g -std=gnu99 -pedantic -Wall -Werror

all:    $(OBJS)

myprogram:    main.o foo.o
        gcc -o myprogram $(OPTS) main.o foo.o

main.o:      main.c foo.h
        gcc -c $(OPTS) main.c

foo.o:      foo.c foo.h
        gcc -c $(OPTS) foo.c

main2.o:        main2.c foo2.h
        gcc -c $(OPTS) main2.c

foo2.o: foo2.c foo2.h
        gcc -c $(OPTS) foo2.c

clean:  
        rm -f *.o $(OBJS)
noobatrilla
  • 65
  • 1
  • 9