2

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
v010dya
  • 5,296
  • 7
  • 28
  • 48
  • You could generate a `VPATH` with `$(shell find ...)` but this really seems like a way to go deeper into madness. Why do you want to not specify the location of each file? – tripleee Aug 04 '20 at 07:32

1 Answers1

1

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'))))
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • Here's a `wildcard`-based solution that can deal with unlimited depth: https://stackoverflow.com/a/18258352/2752075 – HolyBlackCat Aug 04 '20 at 08:24
  • Thank you., it perfectly works for my usecase, where subdirs may be added in future. Hence wanted an alternative for -I option in Makefile. INC_LIST = $(addprefix -I, $(sort $(dir $(shell find . -name '*.h')))) – g8grundeller Aug 04 '20 at 10:33