[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Automatic dependency generation - make won't create intermediate dirs fo

From: gmake2005
Subject: Automatic dependency generation - make won't create intermediate dirs for me
Date: Sat, 3 Dec 2005 14:44:48 -0500 (EST)


My Makefile, below, successfully uses the automatic dependency generation by using the g++ '-MM' option, then including the generated .d files back into the Makefile.

I wanted to unclutter my src dir by putting .d files into a subdir ".dep/", and .o files into ".obj/", and I got that to work.

Then I wanted make to automatically create the ".dep/" and ".obj/" subdirs for me if they don't exist, and that doesn't work. It seems like make wants to build my .d files first, though I thought I told it to make $(DEPDIR) first.

Also, if I start with a clean directory, just containing main.cpp and Makefile, 'make clean' fails; it's like it won't clean stuff until the .d files exist, and it can't create them because ".dep/" doesn't exist.

I think I'm overlooking something basic (I'm new to make), but I haven't found it yet... I'm sorry this message is long, but thanks for any help.

# --------------------------------------------------------
#  List of source files
# --------------------------------------------------------
SRCS =  \
        main.cpp \


# --------------------------------------------------------
#  The rest of this file shouldn't have to change much:
# --------------------------------------------------------
TARGET = make-problem

CC      = gcc
CXX = g++
LD      = g++

RM = /bin/rm -f

# automatically generate objs from srcs
OBJDIR = .obj/
DEPDIR = .dep/

OBJS = $(addprefix $(OBJDIR),$(SRCS:.cpp=.o))
DEPS = $(addprefix $(DEPDIR),$(SRCS:.cpp=.d))


# top-level rule, to compile everything.

.PHONY: clean

        mkdir $(OBJDIR)

        mkdir $(DEPDIR)

# rule to link the program
        $(LD) $(LDFLAGS) $(OBJS) -o $(TARGET) $(LIBS)

# tell make how to deal with obj files in the OBJDIR
        $(CXX) $(CFLAGS) -o $@ -c $<

# automatically generate dependencies based on all .cpp files:
$(DEPDIR)%.d: %.cpp
        $(CXX) -MM $(CFLAGS) $< \
                | sed 's/$*.o/$(OBJDIR:/=\/)$*.o/g' \
                | sed 's/$*.o/& $(subst /,\/,$@)/g' > $@

# rule for cleaning re-compilable files.
        $(RM) $(TARGET)
        $(RM) -r -f $(OBJDIR) $(DEPDIR)

# include the listed cpp files' dependencies:
include $(addprefix $(DEPDIR),$(SRCS:.cpp=.d))

#include <string>
#include <cstdio>

int main(int argc, char** argv)
        std::string x = "hello world...";
        printf( "%s\n", x.c_str() );
        return 0;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]