1

I realise this is a commong query, but I've spent hours looking at previous answers but am still at a loss. I don't have a huge amount of experience with makefiles and am finding it difficult to follow the examples.

I have the following makefile:

CC=g++

CFLAGS=-c -Wall
LDFLAGS= -lSDL2 -lSDL2_image -lSDL2_ttf
SOURCES=main.cpp hello.cpp factorial.cpp
OBJS=$(SOURCES:%.cpp=%.o)
EXECUTABLE=hello
FULLOBJS = $(addprefix obj/,$(OBJS))


all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJS)
    $(CC) $(LDFLAGS) $(OBJS) -o $@

.cpp.o:
    $(CC) -MD -I. $(CFLAGS) $< -o $@

clean:
    -rm -rf $(OBJS) $(EXECUTABLE) $(OBJS:%.o=%.d)


-include $(OBJS:%.o=%.d)

Which works as I'd like. However, it would be even better if I was able to neatly organise the .h .cpp .o and .b files into /header/ /source/ and /object/ relative subdirectories. Please could you explain to me how to do so?

Thanks very much

user7119460
  • 1,451
  • 10
  • 20
  • Do you want to create separate folders for .h, .cpp, .o and add the path to these folders? – CroCo Dec 20 '16 at 01:36
  • Yes if possible. I assume there is a better way than specifying the path strings for each file separately. And I also assume there is a way to place all .o and .d files created by the makefile into a specified directory. – user7119460 Dec 20 '16 at 02:04

1 Answers1

0

For placing the .cpp files, VPATH is your friend, e.g.:

VPATH = ./src ./blah

... will cause the Makefile to look for your .cpp files in ./src and ./blah (instead of only in the directory where your Makefile is)

For .h files, you'll want to pass the compiler a flag telling it where to look for them, e.g.:

CXXFLAGS += -I./headers -I./blah

Ways to place .o files into a sub-folder can be found here.

Community
  • 1
  • 1
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234