James Raitsev
James Raitsev

Reputation: 96371

Makefile trouble, clarification needed

The following is the Makefile i use. All is well, except for .o should be created in obj/ directory and it's not.

What am i doing wrong please?

enter image description here

After making sure that

When make is ran, i see

g++ -pedantic -Wall -c src/a.cpp  -o /Users/me/Dropbox/dev/c++/hott/obj/src/a.o

when it should be

g++ -pedantic -Wall -c src/a.cpp  -o /Users/me/Dropbox/dev/c++/hott/obj/a.o

What am i doing wrong please?

UPDATE: Nothing seems to change when hardcoding path and not relying on pwd resolution

Upvotes: 0

Views: 103

Answers (2)

James Raitsev
James Raitsev

Reputation: 96371

All is ok when used like this. A small modification from what Jonathon suggested

enter image description here

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

If you use -o you have to specify the filename, not just the output path. Try:

$(CC) $(FLAGS) $(SOURCES) $(OBJ)/$@

This question may help, too:

Also, you may want to call FLAGS something like CFLAGS, meaning "the flags for compilation".


Edit

Note that you are not using make efficiently, because you are always recompiling all your .o files from your .cpp files. You should instead use a Pattern Rule, so that Make can have rules to only build what is necessary. (ie. "To build any .o file from a .cpp file, do this: ___") 

%.o : %.c
    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

You could edit this to include $(OBJ) before the $@.

Upvotes: 1

Related Questions