0

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

  • src directory contains a.cpp
  • target directory exists and is empty
  • obj directory exists and is empty

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

James Raitsev
  • 92,517
  • 154
  • 335
  • 470

2 Answers2

1

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 $@.

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

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

enter image description here

James Raitsev
  • 92,517
  • 154
  • 335
  • 470