Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non recursive makefile example

Tags:

makefile

I've visited many links including Peter Miller's PDF about non recursive make but I still can't find a simple example on how to implement it.

I've googled for hours but many of them seem to be over-complicated and hard to tell the flow of things.

Can somebody show an example of a couple of directories? I'd like to see how the makefile scales as more directories are added.

Maybe for something like:

/src/
|->main.cc

|->dirA/a.cc
|->dirA/module.mk

|->dirB/b.cc
|->dirB/module.mk

/include/
|->main.h
|->a.h
|->b.h

Then have main.o require a.o and b.o to be compiled into one executable.

Thanks for any tips and guidance in advance!

like image 373
Tek Avatar asked Jun 26 '26 23:06

Tek


1 Answers

You haven't said much about what you want this system to do. Here's a crude but effective solution:

/src/Makefile:

vpath %.h include

main: main.o
    $(CXX) $^ -o $@

%.o: %.cc
    $(CXX) -c -Iinclude $< -o $@     # not the best way, but maybe the clearest

include dirA/module.mk
include dirB/module.mk

/src/dirA/module.mk:

main: dirA/a.o

dirA/a.o: a.h

/src/dirB/module.mk:

main: dirB/b.o

dirB/b.o: b.h

There is more you can do, once you have this much working.

like image 178
Beta Avatar answered Jul 02 '26 11:07

Beta