Multi-directory Makefile

This is a discussion on Multi-directory Makefile within the C Programming forums, part of the General Programming Boards category; I'm looking to make a makefile which compiles 1 program, with a header file in a different directory. I've got: ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Multi-directory Makefile

    I'm looking to make a makefile which compiles 1 program, with a header file in a different directory.

    I've got:
    WordCount.c
    adt/Table.c
    adt/Table.h

    Does anyone know how to make this compile? The WordCount.c and the Table.h are linked.

  2. #2
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    use -include option for the command

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note sure which you are asking for. I think you are actually asking "How can I include a header file from a different directory than the current one?", in which case it's got very little to do with makefiles, and a lot to do with compiler flags:
    Code:
    ${CC} -Isomedir ...
    will tell the compiler to look for include files in "somedir".

    You will most likely also want to compile and link with "adt/table.c" - which leads to my followup of the posted answer:
    You CAN use "include" to include another makefile from another directory. This is good if you have a large makefile to produce the code in that directory. If you only have a few files to build in that other directory,, you can specify source files from another directory in one makefile:
    Code:
    somedir/blah.o: somedir/blah.c
    ...
    or
    blah.o: somedir/blah.c
    ...
    Of course, it may be that neither of these answers actually answer your question, in which case you need to rephrase the question.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by reelbigtim View Post
    I'm looking to make a makefile which compiles 1 program, with a header file in a different directory.

    I've got:
    WordCount.c
    adt/Table.c
    adt/Table.h

    Does anyone know how to make this compile? The WordCount.c and the Table.h are linked.
    In this case, the adt/ directory is acting like a "library" which WordCount.c is using. I'd make a sub-makefile for the adt "library" and place it in the adt/ directory. It will produce libadt.a. Then, the makefile for WordCount will refer to libadt.a with an appropriate rule which goes into adt/ and runs a sub-make if necessary.

    Code:
    WordCount.o: WordCount.c adt/Table.h
        $(CC) $(CFLAGS) -Iadt -c WordCount.c
    
    WordCount: WordCount.o adt/libadt.a
       $(CC) -o $@ WordCount.o -Ladt -ladt
    
    adt/libadt.a:
        cd adt && $(MAKE) libadt.a
    Where adt/ has its own makefile:

    Code:
    libadt.a: Table.o
        ar ruvs $@ $^
    Of course, these are not complete makefiles, but should show you what I mean.
    Last edited by brewbuck; 10-29-2007 at 10:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  3. help: makefile: directory rules
    By paulur in forum C Programming
    Replies: 5
    Last Post: 03-19-2006, 10:43 PM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 08:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21