Python 扩展:对 C 部分和 C++ 部分使用不同的编译器标志
Python extension: using different compiler flags for a C parts and C++ parts
对于我的 python 扩展,我有 C(来自嵌入式库)和 C++ 文件,它们被编译并链接在一起。只有 C++ 部分与 Python 接口(通过 SWIG)。这在 windows VS2015 和 linux 下的 gcc 中都有效。但是,对于 gcc,C++ 文件需要一组不同于 C 文件的编译器标志(例如 -std=c++11、-Wno-reorder),以避免关于 C 中不正确标志的警告。
在 setuptools / distutils 中有没有办法单独更改每个文件的编译器标志,例如。基于文件扩展名?
我已经使用了 中的自定义构建步骤。
更新:
主要问题是,distutils.ccompiler
不检查 C 或 C++ 的文件扩展名,运行 所有内容都带有 $CC。即使定义 CXXFLAGS 也无济于事。我会忍受警告,无论是 export
还是 setup.py 文件中使用 os.eniviron
的定义。
更新 2:
在带有 CLang 8.0.0 的 macOS 上,情况变得更糟:尝试使用 -std=c++11 编译 .c 文件不是警告而是错误。
因为 distutils 在确保所有文件都使用相同的编译器标志进行编译方面做了很大的努力,而不管它们的文件扩展名为 .c 或 .cpp。因此,即使使用 CFLAGS 和 CXXFLAGS 也没有被考虑在内,但 gcc 和 CLang 仍然以不同的方式处理它们。 Visual Studio 只是将所有内容编译为 C++。
我通过接受 C 在大多数情况下仍然是 C++ 的子集并将 C-Source 文件重命名为 .cpp 来解决我的问题,即使这些文件包含 C。这个解决方案很难看,但我得到了消除 gcc 中的警告和 CLang 的错误 - 特别是因为此解决方案再次模糊了 C 和 C++ 之间的语言障碍。
我后来采用的第二种解决方案是在distutlis 和link Python extension to that static library 之外从C 代码创建一个静态库。
还有一个选项是重载 distutils
编译器 class(比如 unix C 编译器):
import os
from distutils.unixccompiler import UnixCCompiler
cpp_flags = ['-std=c++11']
class C_CxxCompiler(UnixCCompiler):
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
_cc_args = cc_args
# add the C++ flags for source files with extensions listed below
if os.path.splitext(src)[-1] in ('.cpp', '.cxx', '.cc'):
_cc_args = cc_args + cpp_flags
UnixCCompiler._compile(self, obj, src, ext, _cc_args, extra_postargs, pp_opts)
然后你重载distutils.build_ext
命令来拦截扩展构建并在编译继续之前替换编译器:
class BuildC_CxxExtensions(build_ext):
def build_extensions(self, ext):
if self.compiler.compiler_type == 'unix':
# Replace the compiler
old_compiler = self.compiler
self.compiler = C_CxxCompiler()
# Copy its attributes
for attr, value in old_compiler.__dict__.items():
setattr(self.compiler, attr, value)
build_ext.build_extensions(self, ext)
根据您的平台,您可能需要重载其他编译器 classes MSVCCompiler
、CygwinCCompiler
、Mingw32CCompiler
或 BCPPCompiler
。
对于我的 python 扩展,我有 C(来自嵌入式库)和 C++ 文件,它们被编译并链接在一起。只有 C++ 部分与 Python 接口(通过 SWIG)。这在 windows VS2015 和 linux 下的 gcc 中都有效。但是,对于 gcc,C++ 文件需要一组不同于 C 文件的编译器标志(例如 -std=c++11、-Wno-reorder),以避免关于 C 中不正确标志的警告。
在 setuptools / distutils 中有没有办法单独更改每个文件的编译器标志,例如。基于文件扩展名?
我已经使用了 中的自定义构建步骤。
更新:
主要问题是,distutils.ccompiler
不检查 C 或 C++ 的文件扩展名,运行 所有内容都带有 $CC。即使定义 CXXFLAGS 也无济于事。我会忍受警告,无论是 export
还是 setup.py 文件中使用 os.eniviron
的定义。
更新 2:
在带有 CLang 8.0.0 的 macOS 上,情况变得更糟:尝试使用 -std=c++11 编译 .c 文件不是警告而是错误。
因为 distutils 在确保所有文件都使用相同的编译器标志进行编译方面做了很大的努力,而不管它们的文件扩展名为 .c 或 .cpp。因此,即使使用 CFLAGS 和 CXXFLAGS 也没有被考虑在内,但 gcc 和 CLang 仍然以不同的方式处理它们。 Visual Studio 只是将所有内容编译为 C++。
我通过接受 C 在大多数情况下仍然是 C++ 的子集并将 C-Source 文件重命名为 .cpp 来解决我的问题,即使这些文件包含 C。这个解决方案很难看,但我得到了消除 gcc 中的警告和 CLang 的错误 - 特别是因为此解决方案再次模糊了 C 和 C++ 之间的语言障碍。
我后来采用的第二种解决方案是在distutlis 和link Python extension to that static library 之外从C 代码创建一个静态库。
还有一个选项是重载 distutils
编译器 class(比如 unix C 编译器):
import os
from distutils.unixccompiler import UnixCCompiler
cpp_flags = ['-std=c++11']
class C_CxxCompiler(UnixCCompiler):
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
_cc_args = cc_args
# add the C++ flags for source files with extensions listed below
if os.path.splitext(src)[-1] in ('.cpp', '.cxx', '.cc'):
_cc_args = cc_args + cpp_flags
UnixCCompiler._compile(self, obj, src, ext, _cc_args, extra_postargs, pp_opts)
然后你重载distutils.build_ext
命令来拦截扩展构建并在编译继续之前替换编译器:
class BuildC_CxxExtensions(build_ext):
def build_extensions(self, ext):
if self.compiler.compiler_type == 'unix':
# Replace the compiler
old_compiler = self.compiler
self.compiler = C_CxxCompiler()
# Copy its attributes
for attr, value in old_compiler.__dict__.items():
setattr(self.compiler, attr, value)
build_ext.build_extensions(self, ext)
根据您的平台,您可能需要重载其他编译器 classes MSVCCompiler
、CygwinCCompiler
、Mingw32CCompiler
或 BCPPCompiler
。