sohnda
sohnda

Reputation: 29

How can I include headers from a directory and all its subdirectories in my makefile?

My project organization is as follows:

In my makefile, to grab all *.h from the subdirectories I currently use:

#includes (.h / .hpp)
INC_DIRS := \
$(wildcard include/*/)

INCLUDE = $(foreach d, $(INC_DIRS), -I$d)

This does work, and I am able to compile fine. However I was wondering if there was a simpler way to include all subdirectories in a single include statement.

The current method I am using does a '-Iinclude/subdirectory' for each subdirectory and it looks really messy and confusing in the terminal.

Upvotes: 1

Views: 803

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

Given your include path approach, this is a good way to specify it in your makefile.

However, it would be better to not require all these subdirectories to be in the path, and instead use relative paths inside your code.

#include "somedir/thing.h"
#include "somedir2/otherthing.h"

and so forth.

This will also then be much easier to port to other build systems if needed.

Upvotes: 2

Related Questions