jeudi 27 juin 2019

GNUMakefile and gcc: resolving the order of conflicting "-std=c++" requirements

Many packages these days come with a configuration utility to help set compiler requirements. I can give more specific examples if required, but a Makefile that links against two libraries might have something like:

CPPFLAGS += $(shell PackageA-config --cflags)
CPPFLAGS += $(shell PackageB-config --cflags)

%.o : %.cc 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

Now let's say that PackageA requires at least c++11, and PackageB requires c++17, i.e. PackageA-config --cflags would yield -std=c++11 and PackageB-config --cflags would yield -std=c++17. gcc will use whichever flag came last, and since all the standards are backwards compatible, this is fine and dandy.

But what if I write my Makefile in the "wrong" order, so that PackageB comes first? Now PackageA's -std=c++11 flag is the one that actually gets used, and compilation fails.

Is there a way to tell gcc to take the highest flag given, rather than just use the last one? If not, is there a standard way to tackle this headache? Failing everything else, can some guru come up with a fix by manipulating the Makefile variable?

Aucun commentaire:

Enregistrer un commentaire