cc1plus:警告:命令行选项 "-Wstrict-prototypes" 对 Ada/C/ObjC 有效,但对 C++ 无效

cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++

本文关键字:有效 ObjC 但对 无效 C++ Ada 命令行 警告 选项 -Wstrict-prototypes cc1plus      更新时间:2023-10-16

我正在构建一个用于Python的c++扩展。我看到在编译过程中生成了这个警告—当类型:

python setup.py build_ext -i

是什么导致的,我如何解决它?

顺便说一句,这是我的安装文件的副本:

#!/usr/bin/env python
    """
    setup.py file for SWIG example
    """
    from distutils.core import setup, Extension

    example_module = Extension('_foolib',
                               sources=['example_wrap.cxx', 
                                        '../wrapper++/src/Foo.cpp'
                                       ],
                               libraries=["foopp"]
                               )
    setup (name = 'foolib',
           version = '0.1',
           author      = "Me, Myself and I",
           description = """Example""",
           ext_modules = [example_module],
           py_modules = ["example"],
           )

我在Ubuntu上使用gcc 4.4.3

我可以回答部分问题,为什么你会收到这条消息。

在您的构建过程中调用gcc的c++源文件与选项-Wstrict-prototypes。对于C和Objective-C,这会导致编译器对没有声明参数类型的旧式函数声明发出警告。

对于c++,这个选项没有意义;语言甚至不允许这样的声明(原型是强制性的)。

(我不知道为什么消息提到Ada;-Wstrict-prototypes对Ada来说比c++更没有意义。这不是一个大问题,但我已经提交了这个bug报告,标记为RESOLVED/FIXED as of 2015-12-06

解决方案应该是从gcc的调用中删除-Wstrict-prototypes选项。但是由于您没有直接调用gcc,因此很难知道如何调用。

在手动创建一个虚拟example_wrap.cxx文件后,我能够使用setup.py重现警告:

% python setup.py build_ext -i
running build_ext
building '_foolib' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c example_wrap.cxx -o build/temp.linux-i686-2.7/example_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
...

所以这可能是Python的build_ext中的一个小错误。

但是,由于这只是一个警告,而不是致命错误,我认为您可以放心地忽略它。GCC警告这个无意义的选项,但是忽略它。

编辑:

查看Python-2.7.2的源代码,configure.in的这一节可能是罪魁祸首:

case $GCC in
yes)
    if test "$CC" != 'g++' ; then
        STRICT_PROTO="-Wstrict-prototypes"
    fi

(我假设在使用build_ext时调用)

只有当编译器而不是被调用为g++时,它才打开-Wstrict-prototypes选项——但在您的情况下,它使用gcc命令来编译c++源代码。而在Lib/distutils/command/build_ext.py中,build_extension()在调用self.compiler.compile()时不关注源文件语言,只在调用self.compiler.link_shared_object()时才关注源文件语言。(这似乎很奇怪;对于gcc以外的编译器,您不一定能够使用相同的命令来编译C和c++——无论如何,使用g++命令更有意义,即使您没有链接。

更新:提交了一个Python错误报告:https://bugs.python.org/issue9031,并作为这个报告的副本关闭:https://bugs.python.org/issue1222585,当我写这篇文章时,它仍然是打开的。

但正如我所说,这只是一个警告,你可以放心地忽略它。也许Python维护者可以在未来的版本中使用上述信息来修复这个问题。

从OPT环境变量中删除- strict-prototypes没有效果。工作原理是子类build_ext如下:

from distutils.command.build_ext import build_ext
from distutils.sysconfig import customize_compiler
class my_build_ext(build_ext):
    def build_extensions(self):
        customize_compiler(self.compiler)
        try:
            self.compiler.compiler_so.remove("-Wstrict-prototypes")
        except (AttributeError, ValueError):
            pass
        build_ext.build_extensions(self)

,然后在setup函数中使用my_build_ext:

setup(cmdclass = {'build_ext': my_build_ext})

-Wstrict-prototypes选项被distutils作为OPT变量的一部分从/usr/lib/pythonX.Y/config/Makefile中读取。这看起来有点粗糙,但是您可以通过在setup.py中设置os.environ['OPT']来覆盖它。

下面是一段似乎不太有害的代码:

import os
from distutils.sysconfig import get_config_vars
(opt,) = get_config_vars('OPT')
os.environ['OPT'] = " ".join(
    flag for flag in opt.split() if flag != '-Wstrict-prototypes'
)

这是一个Python 3。

from setuptools import setup
from setuptools.command.build_ext import build_ext

# Avoid a gcc warning below:
# cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid
# for C/ObjC but not for C++
class BuildExt(build_ext):
    def build_extensions(self):
        if '-Wstrict-prototypes' in self.compiler.compiler_so:
            self.compiler.compiler_so.remove('-Wstrict-prototypes')
        super().build_extensions()
setup(
    ...
    cmdclass={'build_ext': BuildExt},
    ...
)

setup.py中的以下代码片段将删除这个讨厌的标志的所有实例:

# Remove the "-Wstrict-prototypes" compiler option, which isn't valid for C++.
import distutils.sysconfig
cfg_vars = distutils.sysconfig.get_config_vars()
for key, value in cfg_vars.items():
    if type(value) == str:
        cfg_vars[key] = value.replace("-Wstrict-prototypes", "")
# ==================================

更具体地说,distutils使用与python构建时相同的选项,您可以在创建distutils.core.Extension时使用extra_compile_args添加选项,但似乎没有办法删除gcc或distutils中的现有参数。

请参阅http://bugs.python.org/issue9031了解详细信息,它已被关闭为http://bugs.python.org/issue1222585的副本,但9031详细说明了问题的这一方面

对于试图在pypy下安装py的人来说,这个在py1.0.0中采用的解决方案:

from distutils.sysconfig import get_config_var
_UNWANTED_OPTS = frozenset(['-Wstrict-prototypes'])
os.environ['OPT'] = ' '.join(
    _ for _ in get_config_var('OPT').strip().split() if _ not in _UNWANTED_OPTS

将中断pypy下的安装,因为pypy sysconfig根本不提供'OPT'变量,导致它在尝试将strip()应用于None时中止。解决方法就是注释掉整个代码块