Makefile:发出警告 gcc 版本低于 4.8.0

Makefile: Emit warning gcc version is lower than 4.8.0

Makefile参数到gcc

某个 Makefile` in an Open-Source project 包含以下调用:

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

-Og 参数被引入 gcc 4.8。我的 OSX 包含 gcc 4.2.1,它失败了,并显示了一条相当混乱的错误消息。

问题

是否有优雅和标准(即适用于任何 POSIX 环境)的方法来检查 gcc 版本并在低于 4.8 时发出警告?

问题是 gcc --version 有不同的输出格式:

$ 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.

我的问题

有没有一种优雅的、与 Makefile 兼容的标准方法来检查 gcc 的版本,并在版本低于 4 时发出警告。8.x?

Checking the gcc version in a Makefile? 之后,我得到了以下代码:

# 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

备注:

  • 使用gcc -dumpversion来避免gcc --version
  • 中的上述混乱
  • 使用shell expr比较版本

感谢sycko and madscientist的宝贵意见!