Like Tree4Likes
  • 2 Post By tabstop
  • 2 Post By bernt

Makefiles depending on other makefiles

This is a discussion on Makefiles depending on other makefiles within the Tech Board forums, part of the Community Boards category; I've been trying to get GNU make to work with a root dir Makefile and a subdir Makefile.. I've written ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    412

    Makefiles depending on other makefiles

    I've been trying to get GNU make to work with a root dir Makefile and a subdir Makefile..
    I've written my Makefiles based on The GNU C Programming Tutorial but I can't figure out how to solve this.
    If I run make from the subdir then it works fine for that subdir's target. But if I run make from the top level dir it doesn't detect that one of the dependences of the subdir's Makefile has changed.

    For example:
    In subdir:
    Code:
    ~/os/kernel $ make
    make: `kernel' is up to date.
    
    ~/os/kernel $ touch kernel.h
    
    ~/os/kernel $ make
    /usr/local/cross/bin/x86_64-elf-g++ -m32 -Wall -Wextra -Werror -nostdlib -nostar
    tfiles -nodefaultlibs -o kernel_main.o -c kernel_main.cpp
    /usr/local/cross/bin/x86_64-elf-g++ -m32 -Wall -Wextra -Werror -nostdlib -nostar
    tfiles -nodefaultlibs -o kernel_printf.o -c kernel_printf.cpp
    /usr/local/cross/bin/x86_64-elf-ld -melf_i386 -T kernel.ld  -o kernel kernel_loa
    der.o kernel_main.o kernel_printf.o
    In parent directory:
    Code:
    ~/os $ make
    make: `kernel/kernel.build' is up to date.
    
    ~/os $ touch kernel/kernel.h
    
    ~/os $ make
    make: `kernel/kernel.build' is up to date.
    Directory structure:
    Code:
    ./kernel/kernel <-- Target for subdir Makefile
    ./kernel/kernel.build <-- Dummy target for top level Makefile
    ./kernel/Makefile <-- subdir Makefile
    ./kernel/(.cpp and .h files)
    ./Makefile <-- top level Makefile
    ./Makefile:
    Code:
    MOUNTSCRIPT=scripts/mount_vdisk.diskpart
    UNMOUNTSCRIPT=scripts/unmount_vdisk.diskpart
    COPYSCRIPT=scripts/copy_to_vdisk.sh
    
    kernel/kernel.build : kernel/kernel
    	diskpart /s $(MOUNTSCRIPT)
    	sh $(COPYSCRIPT)
    	diskpart /s $(UNMOUNTSCRIPT)
    	touch kernel/kernel.build
    
    # what do I put here as dependency?
    kernel/kernel :
    	make -C kernel
    ./kernel/Makefile:
    Code:
    ASM=yasm
    CPP=/usr/local/cross/bin/x86_64-elf-g++
    LD=/usr/local/cross/bin/x86_64-elf-ld
    ASMFLAGS=-felf
    CCFLAGS=-Wall -Wextra -Werror -nostdlib -nostartfiles -nodefaultlibs
    LDFLAGS=-melf_i386 -T kernel.ld 
    OBJS=kernel_main.o kernel_printf.o
    
    kernel : kernel_loader.o $(OBJS)
    	$(LD) $(LDFLAGS) -o $@ kernel_loader.o $(OBJS)
    	
    $(OBJS) : kernel.h
    
    kernel_loader.o : kernel_loader.asm
    	$(ASM) $(ASMFLAGS) -o kernel_loader.o kernel_loader.asm
    
    %.o : %.cpp
    	$(CPP) -m32 $(CCFLAGS) -o $@ -c $<
    Do I have to duplicate all the dependencies from the kernel/Makefile in the top level one?
    This is running under cygwin by the way, not that I think it should matter.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm guessing you probably want to use include: Include - GNU `make'
    MK27 and _Mike like this.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Call the subdirectory's make every time, and let the subdir decide whether its dependencies are up-to-date on its own.
    _Mike and stahta01 like this.
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Thank you both.
    I ended up using the 'FORCE' way. Include also worked, but it made things a bit trickier to use with relative paths in the subdir makefile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefiles
    By TriKri in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2008, 06:57 AM
  2. Makefiles!
    By AmbliKai in forum C Programming
    Replies: 6
    Last Post: 11-29-2007, 01:36 AM
  3. Makefiles
    By DaveHope in forum C Programming
    Replies: 1
    Last Post: 10-06-2005, 12:42 AM
  4. makefiles
    By bto19 in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2003, 12:33 PM
  5. help with makefiles
    By metsman20 in forum C Programming
    Replies: 1
    Last Post: 04-20-2003, 09:50 PM

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