This is an incredibly simple question, but I'm new to makefiles. I am trying to make a makefile that will compile two independent programs:
program1: gcc -o prog1 program1.c program2: gcc -o prog2 program2.c
All the examples online go into way more details than I need and are confusing! All I really want it to do is to run the two gcc
lines. What am I doing wrong?
If you use more than one ' -f ' or ' --file ' option, you can specify several makefiles. All the makefiles are effectively concatenated in the order specified.
The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.
Makefiles are not obsolete, in the same way that text files are not obsolete.
Do it like so
all: program1 program2 program1: program1.c gcc -o program1 program1.c program2: program2.c gcc -o program2 program2.c
You said you don't want advanced stuff, but you could also shorten it like this based on some default rules.
all: program1 program2 program1: program1.c program2: program2.c
Pattern rules let you compile multiple c files which require the same compilation commands using make
as follows:
objects = program1 program2 all: $(objects) $(objects): %: %.c $(CC) $(CFLAGS) -o $@ $<
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With