How to generate .o files in different directory using Makefile

  c++, makefile

I’m using a Makefile to compile and link my source files with sfml libraries.
This works perfectly. The only problem: it generates .o files (and .exe) in the same directory as the source files (.cpp). I want my Makefile to generate those into a different one (/obj for example). How could I do that ?

Here is the Makefile :

CXX      = g++
INCL_DIR = src/include
LIB_DIR  = src/lib
SRC      = $(wildcard *.cpp)
OBJ      = $(SRC:.cpp=.o)

all: compile link

compile:
    $(CXX) -I $(INCL_DIR) -c $(SRC)

link:
    $(CXX) $(OBJ) -o main -L$(LIB_DIR) -lsfml-graphics -lsfml-window -lsfml-system

Source: Windows Questions C++

LEAVE A COMMENT