1

In my Makefile, how can I specify to use g++-9 or later?

I do not want to specify g++-9 as that will cause hassle for people who have later versions of g++, but my build server defaults to g++-7.

My current solution is to use

CXX ?= g++

in the Makefile, and then on the build server, put export CXX="g++-9" in ~/.profile.

Zaz
  • 46,476
  • 14
  • 84
  • 101
  • You need a build generator (such as cmake) to deal with this meta information about your commands' version. The best you can do inside the Makefile is to have shell commands that check the version of gcc and act in response to that. If you can control the code you are compiling you can check the version of g++ from inside the code. – alfC Jul 23 '23 at 20:46
  • possibly a duplicate: https://stackoverflow.com/questions/5188267/checking-the-gcc-version-in-a-makefile – alfC Jul 23 '23 at 20:49

1 Answers1

0

A little crude, but should work.

This needs bc to be installed to compare integers. In Make >=4.4 you can use $(intcmp ) instead.

ifneq ($(filter undefined default,$(origin CXX)),)
CXX := $(shell bash -c "compgen -c g++- | grep -P '^g\+\+-\d+(?=\.exe$$|$$)' | sort -n -t- -k2 | tail -1")
$(info Guessed CXX=$(CXX))
else
$(info CXX=$(CXX))
endif

# If using GCC...
ifneq ($(filter g++-%,$(CXX)),)
# Guess version.
GCC_VER := $(word 2,$(subst -, ,$(subst ., ,$(CXX))))
$(info GCC_VER=$(GCC_VER))
# Check version.
GCC_VER_OK := $(shell echo '$(GCC_VER) >= 9' | bc)
ifeq ($(GCC_VER_OK),0)
$(error Need GCC version >= 9, but got $(GCC_VER))
else ifneq ($(GCC_VER_OK),1)
$(error `bc` is not installed)
endif
endif
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207