dependencies for sources in sub dirs.

Tobias Grimm
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:

    #include "bar.h"

    int main()
        return 0;

    // EMPTY

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

            gcc $< -o $@

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

    -include .dependencies

            gcc $(OBJS) -o $@

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


More information about the Gcc-help mailing list