Unable to build extension with mixed C and C++ sources
Extension(
'model.A',
sources=['model/a.i',
'model/a.cpp',
'model/b.c'],
swig_opts=["-c++"],
extra_compile_args=["-std=c++11"],
),
The code above won't compile on macOS. clang complains that -std=c++ could not be used for C source code (i.e. b.c).
There is a quick fix for my issue on this line: https://github.com/pypa/distutils/blob/35e1c9d4d788094c254485dfdefa0c24cd5820ee/distutils/ccompiler.py#L571
cc_args = copy(cc_args)
extra_postargs = copy(extra_postargs)
if ext == '.c':
if '-std=c++11' in cc_args:
cc_args.remove('-std=c++11')
if '-std=c++11' in extra_postargs:
extra_postargs.remove('-std=c++11')
However, this is a not decent solution to the cause of the issue.
This sounds like a feature request rather than a bug. It appears to me you're attempting to build an extension that includes both C++ and C sources, but in the parameters to the extension, you're specifying C++ to the compiler. I'm not surprised the compiler complains.
I think you're after a feature that does not yet exist.
I agree, the patch provided isn't particularly robust. I suspect what you're after is a more sophisticated extension-building mechanism that understands how to build and link from different source languages.
I welcome your or others to work on a solution.
Not sure if it's going to be helpful but Gentoo has historically carried a large distutils patch with some parts related to C++ compiling. Unfortunately, it was undocumented and nobody really cared to figure out why exactly it was needed. It can be grabbed e.g. from https://gitweb.gentoo.org/fork/cpython.git/commit/?h=gentoo-3.10.0_p1&id=11d9404f6bd865155fbc861b483f2d99afcd74e4
@jaraco, seems like this bug was closed by accident.
Gah. What a mess. Thanks for letting me know.