markzzz
markzzz

Reputation: 47975

foreach duplicate include in makefile

I'm trying to include some headers using Makefile.

Here's the code I did:

# Add .h and .hpp to the preprocessor build
HEADERS += $(wildcard src/*.h)
HEADERS += $(wildcard src/*.hpp)
HEADERS += $(wildcard src/**/*.h)
HEADERS += $(wildcard src/**/*.hpp)
INCLUDES = $(foreach HEADERS, $(HEADERS), -I$(dir $(HEADERS)))

# FLAGS will be passed to both the C and C++ compiler
FLAGS +=
CFLAGS +=
CXXFLAGS += $(INCLUDES)

But once I build, that's what it execute:

g++  -Isrc/  -Isrc/  -Isrc/  -Isrc/misc/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/  -Isrc/widgets/ -Wsuggest-override -std=c++11  -DSLUG=NWK -fPIC -I../../include -I../../dep/include -O3 -DVERSION=0.6.4 -MMD -MP -g -march=nocona -ffast-math -fno-finite-math-only -Wall -Wextra -Wno-unused-parameter -DARCH_WIN -D_USE_MATH_DEFINES -c -o build/src/plugins/MyPlug.cpp.o src/plugins/MyPlug.cpp 

It seems that, for each .h/.hpp it found, it replicate the HEADER flag.

How would you fix it?

Upvotes: 0

Views: 278

Answers (1)

HardcoreHenry
HardcoreHenry

Reputation: 6387

A simple solution is to just pass your search through $(sort ...). In make, $(sort ...) removes any duplicates:

INCLUDES = $(sort $(foreach HEADERS, $(HEADERS), -I$(dir $(HEADERS))))

One note -- sort does change the order of the include directories, which in turn will change the order the directories will be searched, however, the method you are using to populate your lists does not seem to be order dependent, so you should be good.

Upvotes: 1

Related Questions