简体   繁体   English

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

[英]cc1plus: warning: command line option “-Wstrict-prototypes” is valid for Ada/C/ObjC but not for C++

I am building a C++ extension for use in Python. 我正在构建用于Python的C ++扩展。 I am seeing this warning being generated during the compilation process - when a type: 我看到在编译过程中生成此警告-类型为:

python setup.py build_ext -i

What is causing it, and how do I fix it? 是什么原因引起的,如何解决?

BTW, here is a copy of my setup file: 顺便说一句,这是我的安装文件的副本:

#!/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"],
           )

I am using gcc 4.4.3 on Ubuntu 我在Ubuntu上使用gcc 4.4.3

I can answer part of the question, why you're getting the message. 我可以回答部分问题,为什么会收到消息。

Something in your build process is invoking gcc on a C++ source file with the option -Wstrict-prototypes . 构建过程中的某些事情是使用选项-Wstrict-prototypes在C ++源文件上调用gcc。 For C and Objective-C, this causes the compiler to warn about old-style function declarations that don't declare the types of arguments. 对于C和Objective-C,这会使编译器警告未声明参数类型的旧式函数声明。

For C++, this option doesn't make sense; 对于C ++,此选项没有任何意义。 such declarations aren't even allowed by the language (prototypes are mandatory). 这种语言甚至都不允许这样的声明(原型是强制性的)。

(I don't know why the message mentions Ada; -Wstrict-prototypes makes even less sense for Ada than for C++. It's not a huge deal, but I've submitted this bug report , marked as RESOLVED/FIXED as of 2015-12-06.) (我不知道为什么消息中提到Ada; -Wstrict-prototypes与A ++相比, -Wstrict-prototypes对Ada的意义甚至更小。这不是什么大问题,但是我已提交了此错误报告 ,从2015年起标记为RESOLVED / FIXED- 12-06。)

The solution should be to remove the -Wstrict-prototypes option from the invocation of gcc. 解决方案应该是从gcc调用中删除-Wstrict-prototypes选项。 But since you're not invoking gcc directly, it's difficult to know how to do that. 但是由于您不是直接调用gcc,所以很难知道该怎么做。

I was able to reproduce the warning using your setup.py , after manually creating a dummy example_wrap.cxx file: 在手动创建虚拟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++
...

So it's probably a minor bug in Python's build_ext . 因此,这可能是Python的build_ext一个小错误。

But since it's only a warning, not a fatal error, I'd say you can safely ignore it. 但是由于这只是警告,而不是致命错误,我想您可以放心地忽略它。 gcc warns about the meaningless option, but then it just ignores it. gcc警告无意义的选项,但随后它会忽略它。

EDIT : 编辑

Looking through the Python-2.7.2 sources, this section of configure.in might be the culprit: 查看Python-2.7.2的源代码, configure.in这一部分可能是罪魁祸首:

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

(I'm assuming that's invoked when using build_ext .) (我假设是在使用build_ext时调用的。)

It turns on the -Wstrict-prototypes option only if the compiler is not being invoked as g++ -- but in your case it's using the gcc command to compile C++ source code. 原来在-Wstrict-prototypes只有当编译器被援引为选项g++ -但在你的情况下,它使用gcc命令编译C ++源代码。 And in Lib/distutils/command/build_ext.py , build_extension() doesn't pay attention to the source file language when invoking self.compiler.compile() , only when invoking self.compiler.link_shared_object() . 而在Lib/distutils/command/build_ext.pybuild_extension()调用时不注重对源文件语言self.compiler.compile()调用只有当self.compiler.link_shared_object() (Which seems odd; for compilers other than gcc, you wouldn't necessarily be able to use the same command to compile C and C++ -- and it makes more sense to use the g++ command anyway, even if you're not linking.) (这似乎很奇怪;对于gcc以外的编译器,您不一定能够使用相同的命令来编译C和C ++-无论如何,即使不进行链接,使用g++命令也更有意义。 )

UPDATE: A Python bug report was submitted: https://bugs.python.org/issue9031 , and closed as a duplicate of this one: https://bugs.python.org/issue1222585 , which is still open as I write this. 更新:提交了一个Python错误报告: https : //bugs.python.org/issue9031 ,并作为以下内容的副本进行了关闭: https : //bugs.python.org/issue1222585 ,在我撰写本文时仍处于打开状态。

But as I said, it's only a warning and you can probably safely ignore it. 但是正如我所说,这只是一个警告,您可以放心地忽略它。 Perhaps the Python maintainers can use the above information to fix the problem in a future release. 也许Python维护人员可以使用以上信息在将来的版本中解决该问题。

Removing -Wstrict-prototypes from the OPT environment variable has no effect. 从OPT环境变量中删除-Wstrict-prototypes无效。 What works is to subclass build_ext as follows: 起作用的是将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)

and then use my_build_ext inside the setup function: 然后在setup函数中使用my_build_ext

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

-Wstrict-prototypes option is read by distutils from /usr/lib/pythonX.Y/config/Makefile as part of OPT variable. distutils从/usr/lib/pythonX.Y/config/Makefile读取-Wstrict-prototypes选项作为OPT变量的一部分。 It seems hackish, but you can override it by setting os.environ['OPT'] in your setup.py. 似乎有点黑,但是您可以通过在setup.py中设置os.environ['OPT']来覆盖它。

Here is a code that seems not too harmful: 这是一个看起来不太有害的代码:

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'
)

The following code fragment in setup.py will remove all instances of this pesky flag: 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", "")
# ==================================

This is a Python 3.x solution with setuptools. 这是带有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},
    ...
)

More specifically, distutils uses the same options that python was built with, you can add options using extra_compile_args in creating the distutils.core.Extension but there does not appear to be a way to remove existing arguments in gcc or distutils. 更具体地说,distutils使用与python相同的选项,可以在创建distutils.core.Extension使用extra_compile_args添加选项,但是似乎没有办法删除gcc或distutils中的现有参数。

See http://bugs.python.org/issue9031 for details, it has been closed as a duplicate of http://bugs.python.org/issue1222585 , but 9031 details this aspect of the problem 有关详细信息,请参见http://bugs.python.org/issue9031 ,它已作为http://bugs.python.org/issue1222585的副本关闭,但9031详细说明了问题的这一方面

For the sake of anyone arriving here after trying to install pydoop under pypy, this solution which was adopted in pydoop 1.0.0: 为了任何人尝试在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

breaks the installation under pypy because pypy sysconfig does not supply the 'OPT' variable at all, causing it to abort when it tries to apply strip() to None. 中断pypy下的安装,因为pypy sysconfig根本不提供'OPT'变量,导致它在尝试将strip()应用于None时中止。 Solution is just to comment out the whole block. 解决方案只是注释掉整个块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 gcc 的选项“-Wstrict-prototypes”对 C++ 无效? - Why is gcc's option “-Wstrict-prototypes” not valid for C++? 从C ++调用Python,如何摆脱`-Wstrict-prototypes`警告? - Calling Python from C++, how to get rid of the `-Wstrict-prototypes` warning? cc1plus:错误:使用g ++无法识别的命令行选项“-std = c ++ 11” - cc1plus: error: unrecognized command line option “-std=c++11” with g++ cc1plus:错误:无法识别的命令行选项“-std=c++0x” - cc1plus: error: unrecognized command line option "-std=c++0x" 无法解析cc1plus:错误:无法识别的命令行选项“ -std = c ++ 11” - Failed to resolve cc1plus: error: unrecognized command line option “-std=c++11” cc1plus:对任何其他警告的无法识别的命令行选项警告 - cc1plus: unrecognized command line option warning on any other warning 为什么“cc1plus:警告:无法识别的命令行选项”用于“no-”选项仅在有另一个警告时由g ++标记? - why is “cc1plus: warning: unrecognized command line option” for “no-” options only flagged by g++ when there is another warning? cc1plus:错误:无法识别的命令行选项“ -Wno-implicit-fallthrough” [-Werror] - cc1plus: error: unrecognized command line option “-Wno-implicit-fallthrough” [-Werror] cc1plus:错误:无法识别的命令行选项“ -fsysroot = <path> ”和g ++ - cc1plus: error: unrecognized command line option “-fsysroot=<path>” with g++ cc1plus:错误:无法识别的命令行选项“ -std = gnu ++ 11” - cc1plus: error: unrecognized command line option “-std=gnu++11”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM