cc1plus:警告:命令行选项 "-Wstrict-prototypes" 对 Ada/C/ObjC 有效,但对 C++ 无效
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
我正在构建一个用于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时中止。解决方法就是注释掉整个代码块
- 欧拉项目#8答案是大以获得有效答案
- 调整大小后指向元素值的指针unordered_map有效?
- 为什么是0;C++中的有效语句
- 最高有效数字侧的第N位
- GCC对可能有效的代码抛出init list生存期警告
- 有效地使用std::unordered_map来插入或增加键的值
- c++中O(n^(1/3))中一个数的除数的有效计数
- 使用无符号字符数组有效存储内存
- 自定义先决条件对移动分配运算符有效吗
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 为什么这种直接初始化有效?(C++17)
- 递归函数有效,但无法记忆
- 在C++中初始化向量映射的最有效方法
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 钳制迭代器是否有效
- 如何有效地在 std::vector 中插入一对?
- C++模板 t 不是有效的模板类型
- 检查由括号、方括号和大括号组成的一组方括号是否有效?
- cc1plus:警告:命令行选项 "-Wstrict-prototypes" 对 Ada/C/ObjC 有效,但对 C++ 无效