Tytanyx
Tytanyx

Reputation: 1

Creating a Makefile with Different Executable Files

I have two different programs that use a common header file. Both work perfectly on their own. What I want to do is be able to compile with a single makefile so that if I want to run one file, I have to type ./progone.out instead of ./a.out and for the other c file, I type ./progtwo.out. I have not done a lot of work with makefiles, so I want to know if there is a way to do it. I've been checking a bunch of tutorials and none of them seem to explain how to do this.

Upvotes: 0

Views: 40

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

# this will have the make to compile both executables when invoked as "make" or "make all"
.PHONY all
all: progone.out progtwo.out

# these defines how each programs will be built
progone.out: progone.c commonheader.h
    gcc -o progone.out progone.c

progtwo.out: progtwo.c commonheader.h
    gcc -o progtwo.out progtwo.c

You can also generalize the rules to build each programs like this if they are the same:

%.out: %.c commonheader.h
    gcc -o $@ $<

$@ will be the name of the target(%.out here) and $< will be the name of the first dependency (%.c here).

Upvotes: 1

Related Questions