Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile to compile multiple C programs?

Tags:

c

makefile

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?

like image 210
Sarah Avatar asked May 10 '11 12:05

Sarah


People also ask

Can you have multiple makefile?

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.

What is $@ in makefile?

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.

Are makefiles outdated?

Makefiles are not obsolete, in the same way that text files are not obsolete.


2 Answers

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 
like image 78
cnicutar Avatar answered Sep 17 '22 03:09

cnicutar


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 $@ $< 
like image 39
hmofrad Avatar answered Sep 19 '22 03:09

hmofrad