Reputation:
I need to create a makefile for 4 files: q4.c, q3.c, q2.c, q1.c What would the format of the makefile for these 4 look like? I know the necessary flags for all, preprocess, compile, assemble, and clean, but what would the format of the file look like for 4 programs?
Side notes: all programs are in the same directory.
Thank you for your help- Dave L
Upvotes: 2
Views: 2412
Reputation:
Makefile is a very comprehensive and flexible way of describing build process with its actions, dependencies etc. There are a lot of possible ways of writing it and thus there will never be a simple single answer to your question. For the sake of example, here is how your Makefile could look like for your simple case:
BIN = q1 q2 q3 q4
all: $(BIN)
clean:
$(RM) $(BIN)
It could also look like this if you add and remove source files frequently and each file should be compiled into a standalone executable:
SRC = $(wildcard *.c)
BIN = $(patsubst %.c,%,$(SRC))
all: $(BIN)
clean:
$(RM) $(BIN)
And it can get more and more complex depending on the size and requirements of your project.
I'd recommend you to start from something simple like above example and read Make documentation to get a feeling of what it can do.
Also, Make is quite old, complicated technology. It takes a lot of time and practise to master and gives you a very little in response. There are alternatives. I personally recommend CMake - it is a lot easier, a bit higher level system that can generate build scripts for you. It supports Makefiles, Xcode and a lot more. It's definitely worth taking a look.
Upvotes: 4
Reputation: 11162
If you're using gnu make, and the flags are the same for each:
CFLAGS=-ffoo -O2 -fguess-at-hard-math
all: q1 q2 q3 q4
Otherwise, you could specify each explicitly
all: q1 q2 q3 q4
q1: q1.c
gcc -o q1 q1.c
q2: q2.c
gcc -o q2 -fq2-flags q2.c
...
Upvotes: 2