Makefile in different folders

I know this has been discussed a lot already, but I'm a little crazy and can't figure it out myself.

I'm trying to learn how to create makefiles and I'm having trouble defining a makefile for files in different folders.

This is what I would like to get after compilation:

/makefile
/test.exe
/src/factorials.cpp
/src/main.cpp
/src/hello.cpp
/obj/factorials.o
/obj/main.o
/obj/hello.o
/include/functions.h

      

What's wrong with this makefile?

C++ = g++
FILENAME = test
SOURCES_PATH = src/
SRC = $(SOURCES_PATH)factorial.cpp $(SOURCES_PATH)main.cpp $(SOURCES_PATH)hello.cpp
OBJ = factorial.o main.o hello.o

all: test.exe

test.exe: $(OBJ)
$(C++) $(OBJ) -o $(FILENAME) -Iinclude

%.o: 
$(C++) -c $(SOURCES_PATH)$*.cpp -Iinclude

clean:
rm -f test.exe

      

Everything goes right, but it gives me an error when trying to compile src/all.cpp

. Also, I don't know how to tell g ++ to put the .o files in a folder obj/

.

Thank you so much!

+3


source to share


2 answers


So, it seems that I was able to get the output using the following makefile

C++ = g++
FILENAME = test
OBJS = obj/factorial.o obj/hello.o obj/main.o
INC = -Iinclude/
vpath+= src

$(FILENAME): $(OBJS)
    $(C++) $(OBJS) -o $@ $(INC)

obj/%.o: %.cpp
    $(C++) -o $@ -c $< $(INC)

all: $(FILENAME)

clean:
    rm -rf objs/*.o *~ $(FILENAME).exe

      



Thank!:)

0


source


You should amend your rule .o

as follows

obj/%.o: $(SOURCES_PATH)/%.cpp
    $(CC) $(CXXFLAGS) $< -o $@

      



Alternatively $(vpath)

can be used to determine where the make

source (required) files are viewed for the target:

vpath += $(SOURCES_PATH)

obj/%.o: %.cpp
    $(CC) $(CXXFLAGS) $< -o $@

      

0


source







All Articles