ninja.stop
ninja.stop

Reputation: 430

Create object files in specific directory through Makefile

While this works perfectly :

object.o:   $(SRCDIR)object.c
    $(CC) -c $(CFLAGS) $(INCLUDEDIR) $<
    mv object.o $(OBJDIR)

The goal is to create .o file inside a directory automatically while creating it. ie. not like first create and then move.

I did this :

$(OBJDIR)/object.o: $(SRCDIR)object.c
    $(CC) -c $(CFLAGS) $(INCLUDEDIR) $<

This one outputs : make: *** No rule to make target 'object.o', needed by 'all'. Stop.

Any direction, how to achieve that?

Upvotes: 0

Views: 148

Answers (2)

blackghost
blackghost

Reputation: 1825

I think you're looking for something like this:

$(OBJS) = $(patsubst $(SRCS),$(SRCDIR)/%.c,$(OBJDIR)/%.o)

$(OBJS) : $(SRCDIR)/%.o : $(OBJDIR)/%.c
    @echo "$< => $@"
    @$(CC) -c -o $@ $(CFLAGS) $(INCLUDEDIR) $<

This is a static pattern rule which says to build your objects using a specific pattern. Notice that I assume you moved the trailing / out of $(SRCDIR) (as it's cleaner that way).

Upvotes: 1

ninja.stop
ninja.stop

Reputation: 430

Solved it.

object.o:   $(SRCDIR)object.c
    $(CC) -c -o $(OBJDIR)/object.o $(CFLAGS) $(INCLUDEDIR) $^

Upvotes: 0

Related Questions