3
votes

I am new using gcc in Ubuntu, so I am trying to create a Makefile. My project folder is called "project" inside of this folder I have three more folders: sources, headers, objects (empty).

My Makefile should be able to create the object files in the folder "objects". However, I want my executable and Makefile in the main folder: "project" then I can clean the executable and the object files. How can I compile the program including these folders? This is my Makefile as an example. The folder "project" is in my home folder.

 CFLAGS= -ansi -pedantic -Wall -c
all:    project

project: ./objects/main.o ./objects/factorial.o ./objects/gcd.o ./objects/greatestNumber.o ./objects/primeNumbers.o
    gcc -o project ./objects/main.o ./objects/factorial.o ./objects/gcd.o ./objects/greatestNumber.o ./objects/primeNumbers.o

main.o: ./sources/main.c
    gcc $(CFLAGS) ./sources/main.c

factorial.o: ./sources/factorial.c ./headers/factorial.h
    gcc $(CFLAGS) ./sources/factorial.c

gcd.o: ./sources/gcd.c ./headers/gcd.h
    gcc $(CFLAGS) ./sources/gcd.c

greatestNumber.o: ./sources/greatestNumber.c ./headers/greatestNumber.h
    gcc $(CFLAGS) ./sources/greatestNumber.c

primeNumbers.o: ./sources/primeNumbers.c ./headers/primeNumbers.h
    gcc $(CFLAGS) ./sources/primeNumbers.c

clean:
    rm -f ./objects/main.o ./objects/factorial.o ./objects/gcd.o ./objects/greatestNumber.o ./objects/primeNumbers.o project

Thanks

1
Are you sure main.o doesn't depend on any headers?aschepler
it depends on gcd.h, factorial.h, greatestNumber.h, primeNumbers.huser3654532

1 Answers

7
votes

You should give the path to the object targets just like you do for all the other filenames.

./objects/main.o: ./sources/main.c
    gcc $(CFLAGS) ./sources/main.c

... and so on.