Thread: Basic Makefile question

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Basic Makefile question

    Hi,

    well the question is how to compile objects in a subdirectory?

    Code:
    ./bo/%.o : %.c
    	$(CC) $(CFLAGS) <$
    the command abouve will compile my objects but leave them in the same directory. However I want them in a different directory. in ./bo directory. How to achieve that ?

    EDIT;

    I just realize that if i remove ./bo/ from the line it does not work.... why is that ?

    thnx
    Last edited by baxy; 08-25-2013 at 06:06 AM.

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    You need to include the relative path for both sources and targets:
    Code:
    bo/%.o: bo/%.c
    	$(CC) $(CFLAGS) $^ -c -o $@
    
    bo/%: bo/%.o
    	$(LD) $(LDFLAGS) $^ -o $@
    However, it is mode common to put (or copy or generate) a separate Makefile in each directory, and use
    Code:
    directory: directory/Makefile
    	cd directory && $(MAKE)
    (The directory/Makefile part is only needed or useful if you generate your Makefiles.)

    The GNU Make Manual is quite useful, even if some of its features are GNU make specific; most of the features are available in other make variants, too, and the documentation usually mentions if the feature is GNU specific or not.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Thnx i did not know that. Still struggling with c in general but slowly progressing. Truly it is a shock moving from Perl (do as you want policy) to a structured/strict language like c.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If you have multiple directories (sooner or later, you will), this can get pretty complicated.
    I do it in the following way:
    https://github.com/manasij7479/gl/bl...ework/Makefile

    You only need to add your folders to the SRCDIRS variable to have everything work nicely.

    Recursive make invocation would be more flexible of course, but more trouble to set up.
    EDIT: Didn't notice your point on having a build directory.
    In that case, having a variable listing the build locations should help.
    Last edited by manasij7479; 08-25-2013 at 07:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile question
    By vexed in forum Tech Board
    Replies: 8
    Last Post: 03-06-2011, 09:32 AM
  2. Makefile question
    By -EquinoX- in forum C Programming
    Replies: 12
    Last Post: 03-29-2008, 01:29 PM
  3. makefile question
    By hka26 in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2007, 12:01 AM
  4. makefile question
    By spank in forum Tech Board
    Replies: 3
    Last Post: 07-23-2007, 10:10 AM
  5. Makefile question
    By Yasir_Malik in forum Tech Board
    Replies: 19
    Last Post: 04-03-2004, 10:19 PM