-
Question About Makefiles
How would I create a makefile with three .cc files? Would something like this be right?
Code:
Prog1: main.o Rational.o test.o
g++ -Wall -W -Werror main.o Rational.o test.o -o Prog1
main.o: main.cc Rational.h
g++ -Wall -W -Werror -c main.cc -o Rational.o
Rational.o: Rational.cc Rational.h
g++ -Wall -W -Werror -c Rational.cc -o Rational.o
test.o: test.cc Rational.h
g++ -Wall -W -Werror -c test.cc -o test.o
I appreciate any help and input.
-
Code:
Prog1: main.o Rational.o test.o
g++ -Wall -W -Werror main.o Rational.o test.o -o Prog1
main.o: main.cc Rational.h
g++ -Wall -W -Werror -c main.cc
Rational.o: Rational.cc Rational.h
g++ -Wall -W -Werror -c Rational.cc
test.o: test.cc Rational.h
g++ -Wall -W -Werror -c test.cc
-
You might like to use variables at the top of the file so you don't have to change everything in every line. Just makes neater code.
Example:
Code:
CFLAGS=-W -Wall -ggdb `sdl-config --cflags`
LIBS=-lm `sdl-config --libs`
OBJS=main.o particle.o background.o resources.o
CC=g++
default:
@echo 'Plese check the variables at the top of the Makefile, then run make program.'
clean:
rm -f program $(OBJS)
rm -f *~
program: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o program $(LIBS)
main.o: main.cpp gamedefs.h
$(CC) $(CFLAGS) -c main.cpp
particle.o: particle.cpp particle.h gamedefs.h
$(CC) $(CFLAGS) -c particle.cpp
background.o: background.cpp background.h gamedefs.h
$(CC) $(CFLAGS) -c background.cpp
resources.o: resources.cpp resources.h gamedefs.h
$(CC) $(CFLAGS) -c resources.cpp
That's hopefully helpful. Sorry if it isn't. I know that three modules isn't anything big, but later on, this would save a lot of time.