2

I want to create all directories that are needed for object creation. Here is my project structure:

Project/
    src/
        main.cc
        graphics/
            window.h
            window.cc
        math/
            vec3.h
            vec3.cc
    Makefile

And here is my Makefile:

CXX = g++
CXXFLAGS = -Wall -Wno-write-strings -std=c++11
LDLIBS = -lglfw3 -lGLEW -lGLU -lGL -lX11 -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lpthread

SRC_DIR = src/ src/graphics/ src/math/
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -L/usr/include

SOURCE = $(wildcard $(SRC_DIR)/*.cc)
OBJECTS = ${SOURCE:%.cc=$(OBJ_DIR)/%.o}
EXECUTABLE = application

all: init $(OBJECTS) $(EXECUTABLE)

${EXECUTABLE}: $(OBJECTS)
    $(CXX) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDLIBS)

$(OBJ_DIR)/%.o: %.cc
    $(CXX) $(INC_DIR) -c $< -o $@

init:
    @echo $(SRC_DIR)
    @echo $(OBJ_DIR)
    mkdir -p "$(addprefix $(OBJ_DIR)/,$(SRC_DIR))"

clean:
    rm -rf $(OBJ_DIR) $(EXECUTABLE)

I want to create bin, bin/src/, bin/src/graphics and bin/src/math directories.

At the target init I've done mkdir -p "$(OBJ_DIR)/$(SRC_DIR)" but that only creates bin/src src/graphics src/math instead of bin/src bin/src/graphics/ bin/src/math. How can I add bin prefix to those folders that I'm creating?

Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22
  • Im also learning how to write makefiles. Maybe you can have a look my Q and [this answer](http://stackoverflow.com/a/25086511/3087952). Here is [my current makefile](http://stackoverflow.com/questions/31329581/error-flood-when-compiling-simple-hello-world-program). – nonsensation Jul 11 '15 at 16:28

1 Answers1

2

Use foreach:

init:
    $(foreach d, $(SRC_DIR), mkdir -p $(addprefix $(OBJ_DIR)/,$(d));)
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • I did that, and the output for the make command shows those directories being created. But when I actually open those folders I see that the directory created is not `bin/src/math` for example but instead `bin/src/bin/src/graphics bin/src/math` The makefile apparently doesn't understand that that is a list and creating the directory using the whole list at once. – Mertcan Ekiz Jul 11 '15 at 16:20
  • I believe you skipped some recipes involving SRC_DIR. Except the `init` target, SRC_DIR is used by noone. Is it be design? – Eugeniu Rosca Jul 11 '15 at 16:26
  • I've edited the question with my current Makefile. I believe that `SRC_DIR=src src/graphics src/math` part is wrong but I don't know how to turn that into a list. Can you take a look at it? – Mertcan Ekiz Jul 11 '15 at 16:29
  • Thank you, that works for creating the correct directories. But now I get an error saying `/usr/bin/ld: cannot find src: File format not recognized /usr/bin/ld: cannot find src/graphics: File format not recognized` I think I'm passing in directories to the linker instead of objects. I would really appreciate if you can take another look into this. Thank you so much.--------Edit: I've added `@echo $(OBJECTS)` to my linking target and it prints: `src src/graphics bin/src/math/vec3.o bin/src/math/vec4.o` I don't want `src` and `src/graphics` at the beginning. How do I fix that? – Mertcan Ekiz Jul 11 '15 at 16:39
  • 1
    Specifically to answer your last question: `OBJECTS_NOSRC := $(filter %.o, $(OBJECTS))` – Eugeniu Rosca Jul 11 '15 at 16:57
  • Thanks so much for all your answers – Mertcan Ekiz Jul 11 '15 at 17:10