【问题标题】:Makefile: Emit warning gcc version is lower than 4.8.0Makefile:发出警告 gcc 版本低于 4.8.0
【发布时间】:2017-02-23 15:01:17
【问题描述】:

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 则发出警告?

【问题讨论】:

  • this question 对您有帮助吗?够优雅吗?
  • 请注意,更现代的 MacOS 版本根本不提供 GCC。相反,他们提供了一个gcc 程序,它实际上只是clang 的链接/包装器。这就是输出看起来如此不同的原因:即使您键入的命令是gcc,系统运行的编译器也完全不同。您将无法使用 GCC 版本号来比较 Clang/LLVM 版本。旧版本的 MacOS 附带旧版本的 GCC 的原因是,Apple 不会与新 GPLv3 下的软件有任何关系,因为他们坚持使用旧版本的 GNU 软件。

标签: gcc makefile version-detection


【解决方案1】:

按照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 比较版本

感谢 syckomadscientist 提供有用的 cmets!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多