help-make
[Top][All Lists]
Advanced

[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)

help-make:

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.


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

CFLAGS = LIBS =

# --------------------------------------------------------
#  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))

LDFLAGS =

# top-level rule, to compile everything.
all: $(OBJDIR) $(DEPDIR) $(TARGET)

.PHONY: clean

$(OBJDIR):
        mkdir $(OBJDIR)

$(DEPDIR):
        mkdir $(DEPDIR)

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

# tell make how to deal with obj files in the OBJDIR
$(OBJDIR)%.o:
        $(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.
clean:
        $(RM) $(TARGET)
        $(RM) -r -f $(OBJDIR) $(DEPDIR)


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


main.cpp
-------------------
#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]