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

问题描述 投票:31回答:7

我正在构建一个用于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

c++ python gcc swig
7个回答
38
投票

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

构建过程中的某些内容是使用选项-Wstrict-prototypes在C ++源文件上调用gcc。对于C和Objective-C,这会导致编译器警告没有声明参数类型的旧式函数声明。

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

(我不知道为什么这个消息提到了Ada; -Wstrict-prototypes对于Ada而言对C ++的意义不大。这不是一个大问题,但我已经提交了this bug report,截至2015-12-06标记为已解决/已修复。)

解决方案应该是从gcc的调用中删除-Wstrict-prototypes选项。但由于你没有直接调用gcc,所以很难知道如何做到这一点。

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

% 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时会调用它。)

只有当编译器没有被调用为-Wstrict-prototypes时,它才会打开g++选项 - 但在你的情况下,它使用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维护者可以使用上述信息来解决未来版本中的问题。


20
投票

从OPT环境变量中删除-Wstrict-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)

然后在my_build_ext函数中使用setup

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

14
投票

-Wstrict-prototypes选项由来自/usr/lib/pythonX.Y/config/Makefile的distutils读取,作为OPT变量的一部分。它似乎是hackish,但您可以通过在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'
)

12
投票

setup.py中的以下代码片段将删除此pesky标志的所有实例:

# 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", "")
# ==================================

7
投票

这是一个带有setuptools的Python 3.x解决方案。

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},
    ...
)

2
投票

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

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


1
投票

为了在尝试在pypy下安装pydoop之后到达这里的任何人,这个解决方案在pydoop 1.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时中止。解决方案只是评论整个块。

© www.soinside.com 2019 - 2024. All rights reserved.