首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Autoconf中确定编译器名称或供应商?

如何在Autoconf中确定编译器名称或供应商?
EN

Stack Overflow用户
提问于 2017-11-04 18:09:50
回答 2查看 1K关注 0票数 1

我正在用SunCC开发Solaris。Autoconf的AC_COMPILE_IFELSEAC_LINK_IFELSE是错误的编译器特性。Autoconf报告功能是可用的,即使编译器拒绝使用诸如illegal option之类的消息。

代码语言:javascript
运行
复制
$ echo 'int main(int argc, char* argv[]) {}' > test.C
$ /opt/solarisstudio12.4/bin/CC test.C
$ /opt/solarisstudio12.4/bin/CC -msse4.2 -msha test.C
CC: Warning: Option -msse4.2 passed to ld, if ld is invoked, ignored otherwise
CC: Warning: Option -msha passed to ld, if ld is invoked, ignored otherwise
ld: fatal: option '-h a' is incompatible with building a dynamic executable
$ /opt/solarisstudio12.4/bin/CC -xarch=sha test.C
CC: Warning: illegal use of -xarch option, illegal value ignored: sha

我想尝试解决错误检测问题,但我需要知道编译器才能做到这一点。Autoconf有一些提供CPU、供应商和操作系统的规范化名称的宏,但它们似乎不包括编译器或其供应商。

如何在Autoconf中检测或确定编译器名称或供应商?

添加以下内容并不是很有帮助,因为它不能识别编译器。

代码语言:javascript
运行
复制
AC_MSG_NOTICE(["Build: $build"])
AC_MSG_NOTICE(["Compiler: $compiler"])

然后:

代码语言:javascript
运行
复制
CXX=/opt/solarisstudio12.4/bin/CC ./configure
...

configure: "Build: i386-pc-solaris2.11"
configure: "Compiler: /opt/solarisstudio12.4/bin/CC"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-04 19:59:46

我不认为有什么标准的方法可以做到这一点。

我们根据predef.sourceforge.net手动检查编译器宏的存在,可能还有更多的源代码,如cc --version、cc的命令名、操作系统名称、.)。

也就是说,您编译一个程序,并检查定义。如果它不存在/程序#错误退出->而不是SunCC。

它看起来很混乱,但是下面是一个直接来自分-P分-P的例子也许你可以从中得到一些启示:

代码语言:javascript
运行
复制
AC_DEFUN([AX_COMPILER_VENDOR],
[AC_CACHE_CHECK([for _AC_LANG compiler vendor], ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor,
  dnl Please add if possible support to ax_compiler_version.m4
  [# note: don't check for gcc first since some other compilers define __GNUC__
  vendors="intel:     __ICC,__ECC,__INTEL_COMPILER
           ibm:       __xlc__,__xlC__,__IBMC__,__IBMCPP__
           pathscale: __PATHCC__,__PATHSCALE__
           clang:     __clang__
           cray:      _CRAYC
           fujitsu:   __FUJITSU
           gnu:       __GNUC__
           sun:       __SUNPRO_C,__SUNPRO_CC
           hp:        __HP_cc,__HP_aCC
           dec:       __DECC,__DECCXX,__DECC_VER,__DECCXX_VER
           borland:   __BORLANDC__,__CODEGEARC__,__TURBOC__
           comeau:    __COMO__
           kai:       __KCC
           lcc:       __LCC__
           sgi:       __sgi,sgi
           microsoft: _MSC_VER
           metrowerks: __MWERKS__
           watcom:    __WATCOMC__
           portland:  __PGI
           tcc:       __TINYC__
           unknown:   UNKNOWN"
  for ventest in $vendors; do
    case $ventest in
      *:) vendor=$ventest; continue ;;
      *)  vencpp="defined("`echo $ventest | sed 's/,/) || defined(/g'`")" ;;
    esac
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[
      #if !($vencpp)
        thisisanerror;
      #endif
    ])], [break])
  done
  ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor=`echo $vendor | cut -d: -f1`
 ])
])
票数 2
EN

Stack Overflow用户

发布于 2017-11-07 13:40:05

正如@Ronny所指出的,似乎没有确定编译器供应商的标准方法。在配置过程中检测供应商是非常重要的,比如在错误检测期间处理Autoconf错误:CC: Warning: illegal use of -xarch option, illegal value ignored: sha

@Ronny展示了如何使用预处理器宏来实现它。我们避免了这一点,因为编译器假装是GCC。Clang和Intel的编译器都定义了__GNUC__,可能其他人也会这么做。

下面是如何在configure.ac中使用版本字符串执行此操作

代码语言:javascript
运行
复制
## Determine the compiler's vendor.

COMPILER_VERSION=`"$CXX" --version 2>/dev/null`

## IBM xlC test if COMPILER_VERSION is empty
if test x"$COMPILER_VERSION" = "x"; then
   COMPILER_VERSION=`"$CXX" -qversion 2>/dev/null`
fi

## SunCC test if COMPILER_VERSION is empty
if test x"$COMPILER_VERSION" = "x"; then
   COMPILER_VERSION=`"$CXX" -V 2>&1`
fi

然后,可以像这样使用该供应商:

代码语言:javascript
运行
复制
IS_SUN_COMPILER=`echo $COMPILER_VERSION | $EGREP -i -c -E 'Sun C\+\+'`
echo "IS_SUN_COMPILER: $IS_SUN_COMPILER"
...

## This block handles SunCC.
if test "$IS_SUN_COMPILER" -ne "0"; then    
    ...    
fi
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47113907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档