2

The Makefile parameters to gcc

A certain Makefile` in an Open-Source project contains the following call:

CFLAGS = -I$(RM_INCLUDE_DIR) -Wall -g -fPIC -lc -lm -Og -std=gnu99

The -Og parameter was introduced to gcc 4.8. My OSX contained gcc 4.2.1, and it failed with a rather confusing error message.

The problem

Is there an elegant and standard (i.e., works on any POSIX environment) way to check the version of gcc and emit a warning if it's lower than 4.8?

The problem is that gcc --version has different output formats:

$ gcc --version
gcc (Homebrew gcc 5.3.0) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

My question

Is there an elegant, Makefile-compatible and standard way to check the version of gcc, and emit a warning if the version is lower than 4.8.x?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • 1
    does [this question](http://stackoverflow.com/questions/5188267/checking-the-gcc-version-in-a-makefile) help you? Is that elegant enough? – super schaap Feb 23 '17 at 15:04
  • 1
    Just a point of note, more modern versions of MacOS don't provide GCC at all. Instead they provide a `gcc` program which is actually just a link to/wrapper around `clang`. That's why the output looks so different: even though the command you typed is `gcc` the system ran a completely different compiler. You won't be able to use GCC version numbers to compare Clang/LLVM versions. The reason older versions of MacOS shipped with ancient versions of GCC is that Apple won't have anything to do with software under the new GPLv3, wo they're stuck with older versions of GNU software. – MadScientist Feb 23 '17 at 16:03

1 Answers1

2

Following Checking the gcc version in a Makefile?, I ended up with the following code:

# Warn if gcc version is lower than 4.8 (-Og was introduced in this version)
MIN_GCC_VERSION = "4.8"
GCC_VERSION := "`gcc -dumpversion`"
IS_GCC_ABOVE_MIN_VERSION := $(shell expr "$(GCC_VERSION)" ">=" "$(MIN_GCC_VERSION)")
ifeq "$(IS_GCC_ABOVE_MIN_VERSION)" "1"
    GCC_VERSION_STRING := "GCC version OK $(GCC_VERSION) >= $(MIN_GCC_VERSION)"
else
    GCC_VERSION_STRING := "ERROR: gcc version $(GCC_VERSION) is lower than $(MIN_GCC_VERSION), 'gcc -Og' might fail."
endif

Notes:

  • Using gcc -dumpversion to avoid the abovementioned clutter in gcc --version
  • Using shell expr to compare the versions

Thanks sycko and madscientist for your useful comments!

Community
  • 1
  • 1
Adam Matan
  • 128,757
  • 147
  • 397
  • 562