0

I'm trying to move the *.o-files into a separate folder (/objs). But I'm getting the following error:

"No rule to make target '../objs/main.o', needed by 'all'. Stop."

How do I have to define these rules? My folder structure looks as follows:

model_1/
    main.cpp
    Model1.cpp
    Model1.h
    model_1.mk   # Makefile
pub_sub/
    ...
objs/

My Makefile:

CC=gcc
CXX=g++
RM=rm -f
PROG = myProgram
INCLUDES = -I../pub_sub
CXXFLAGS := -std=c++1y -g -Wall ${INCLUDES}
LDFLAGS = -L/usr/local/lib

SRCS= main.cpp Model1.cpp ../pub_sub/Subscriber.cpp ../pub_sub/Publisher.cpp

OBJS= $(addprefix ../objs/, $(notdir $(subst .cpp,.o,$(SRCS))))

all: $(OBJS)
     $(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(LDFLAGS)

.PHONY:
clean:
    $(RM) $(OBJS)

distclean: clean
    $(RM) $(PROG)
Akinna
  • 61
  • 10
  • 1
    You didn't specify the rule to create object file – Danh Jan 05 '17 at 10:04
  • 2
    GNU make has a builtin rule that tells it how to make a file `%.o` from a matching file `%.cpp`. It has no builtin rule that tells it how to make `../obj/%.o` from `%.cpp`, or `../obj/%.o` from `../pub_sub/%.cpp`. If you want it to do such things you will have to write the rules and recipes that tell it how. – Mike Kinghan Jan 05 '17 at 10:06
  • I'd say the root of your problem is the makefile placement. If your makefile would be in the folder where you plan to generate the objects instead of the source folder, then as MikeKinghan mentioned, automatic rules could apply. – grek40 Jan 05 '17 at 12:24
  • Thank you, that makes sense. Does someone know a good website, where I can find a tutorial, because I have not unterstood yet how to write these rules. – Akinna Jan 05 '17 at 14:43
  • I started writing a tutorial some time ago, but it is still very much incomplete: http://mg.readthedocs.io/make_tutorial/make_tutorial.html. Maybe some part of it helps you anyway ... – Matthias Jan 06 '17 at 09:39

0 Answers0