dependencies for sources in sub dirs.

Tobias Grimm listaccount@e-tobi.net
Thu Nov 10 13:26:00 GMT 2005


John Love-Jensen wrote:
> mkdir src
> vi src/Foo.c
> (put in something simple:  int foo() { return 3; } )
> gcc -c src/Foo.c
>
> Does the Foo.o end up in your current working directory, or in src/Foo.o?
>   

In this case, Foo.o is created in ./ instead of src/. But in my 
makefile, I force output with "-o" to src/Foo.o. (See example below).

As a short-hand solution, I modify the dependencies with Sed to prefix 
the target with the full path of the first dependent source:

sed "s/.*: \([^ ]*\/\).*/\1\0/"

This works,  but I thought there might be a way to instruct gcc to 
create the dependencies right in the first place.

Here's the small example:

src/foo.cc:
    #include "bar.h"

    int main()
    {
        return 0;
    }

src/bar.h:
    // EMPTY

Makefile:
    SRCS  = src/foo.c
    OBJS := $(addsuffix .o,$(basename ${SRCS}))

    %.o: %.cc
            gcc $< -o $@

    .dependencies: Makefile
            @gcc -MM -MG $(SRCS) > $@

    -include .dependencies

    test:$(OBJS)
            gcc $(OBJS) -o $@

    clean:
            @-rm -f $(OBJS) .dependencies test

Tobias



More information about the Gcc-help mailing list