Moep Meep
Moep Meep

Reputation: 111

Makefile, executable and object files in different directories

I'm trying to get the object files and the executable file in different directories. In the root folder there is a obj and exe folder for this, but i have no idea how to get make to run it.

I have tried stuff like:

$(EXEDIR)/sfml-app: $(OBJ) 

and

$(OBJDIR)/%.o: %.cpp 

but it gives me errors. Can anybody explain me how I can get this to run?

Upvotes: 2

Views: 9840

Answers (1)

MadScientist
MadScientist

Reputation: 101041

If you want your output to go to another directory, you have to tell make (and the compiler) about it. They won't just guess because you have a variable named OBJDIR! You have to actually make use of it.

Make sure your target names have the directory prefix so make knows where you expect the object files to end up:

OBJ = $(patsubst %.cpp, $(OBJDIR)/%.o, $(SRC))

and make sure you tell the compiler where you want the object files to end up by using the -o flag:

$(OBJDIR)/%.o: %.cpp
        $(CXX) $(CXXFLAGS) -c $< -o $@

Similarly, if you want the final output to to into EXEDIR you have to use it both in the makefile and send that value to the linker, again via -o:

all: $(EXEDIR)/sfml-app

$(EXEDIR)/sfml-app: $(OBJ)
        $(CXX) -o $@ $(OBJ) $(LIBS)

Upvotes: 4

Related Questions