g8grundeller
g8grundeller

Reputation: 33

Makefile with multiple subdirectories

Do we have an efficient way for including all subdirectories in Makefile, rather than including each directory with -I option?

For instance below is my directory structure,where source code and headers are distributed across different subdirs. I do not have any Makefiles in subdirectories, but only a single top Makefile.

|--> SRC 

     |--> scripts 
           |--- **Makefile**
     |--> lib
           |------*.c
           |------*.h
           |----> subdir
                   |------*.c
     |--> tests
           |---> block1
                  |------*.c                  
           |---> block2
                  |------*.c                
           |---> block3
                  |------*.c
                  |------*.h

Upvotes: 2

Views: 810

Answers (1)

dash-o
dash-o

Reputation: 14491

The request is to automated the generation of the '-I' option - one per each folder that contain '.h' header file. If the depth of the tree is known, possible to use the wildcard function. If the depth of the tree is not known (or very large), the 'find' command can be used.

The code below is relevant for the top level Makefile. The indicate that the Makefile exists in the top level folder, but shows the Makefile in the scripts sub-folder

# Assuming max 2 level of header files
INC_LIST := $(addprefix -I, $(sort $(dir $(wildcard */*.h */*/*.h))))

For the case of unlimited depth, using find

INC_LIST = $(addprefix -I, $(sort $(dir $(shell find . -name '*.h'))))

Upvotes: 1

Related Questions