I need some help with my Makefile for a project. The source directory looks something like this.
|-- Makefile
|-- drivers
| |-- Makefile
| |-- tty
| |-- Makefile
| |-- console.c
| |-- keyboard.c
|-- kernel
| |-- Makefile
| |-- kmain.c
In the top Makefile, I have exported a variable OBJECTS that I want to populate with object files so I can build and link them together in the top Makefile.
I want to update OBJECTS in, say, drivers/tty/Makefile by doing something like this:
OBJECTS += $(CURDIR)console.o
OBJECTS += $(CURDIR)keyboard.o
But the change to OBJECTS does not bubble up to the top Makefile. I’ve been looking at the Makefiles in the Linux source tree, and they seem to be doing something similar. However, I can’t get it to work. Am I missing something here?
You appear to be using Make recursively, something like
This doesn’t work, because each Make has its own
OBJECTS; the child Make can’t modify variables in the parent Make. It’sexport, notimport/exportorshare(there’s no such thing asimport/exportorshare, I’m just trying to illustrate).You can get the effect you want by including the other makefiles instead of invoking them:
You’ll notice there is some unpleasant location-dependency there;
drivers/tty/Makefilehas “drivers/tty” spelled out inside it, which makes maintenance a pain. There are ways to fix that, once you have this basicincludetrick working.