warbaque
warbaque

Reputation: 633

How can I put .o files to different folder and how should I improve this overall?

I've never really wrote any makefiles before and I have little knowledge of its syntax. I'd like to put .o files into separate folder, obj/ for example. But I'm a bit lost how this should be done since there seem to be lot's of different ways to write makefile.

This is what I have currently and I would like to improve it.

PROGRAM=Project

CC=g++

CFLAGS=-c -g -std=c++0x -Wall -Wextra -pedantic -I $(SFML)/include -I src

LDFLAGS=-lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -L $(SFML)/lib -Wl,-rpath=$(SFML)/lib -Wl,-rpath-link=$(SFML)/lib

SOURCES=$(wildcard src/*.cpp)
OBJECTS=$(SOURCES:.cpp=.o)

EXECUTABLE=bin/project

all: build $(SOURCES) $(EXECUTABLE)

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

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

build:
    @mkdir -p bin

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

I've tried some different approaches but haven't yet figured out how to put .o files in their own folder.

Upvotes: 0

Views: 91

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

Replace your OBJECTS line with something like:

OBJECTS=$(patsubst src/%.cpp,obj/%.o,$(SOURCES))

Remove the .ccp.o rule and replace it with something like:

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

You can probably also remove $(SOURCES) from the prerequisite list of the all target unless you expect make to try to create those files somehow.

Upvotes: 1

Related Questions