distutils icon indicating copy to clipboard operation
distutils copied to clipboard

Unable to build extension with mixed C and C++ sources

Open junwei-wang opened this issue 4 years ago • 4 comments

	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.

junwei-wang avatar Feb 04 '21 17:02 junwei-wang

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.

jaraco avatar Dec 11 '21 16:12 jaraco

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

mgorny avatar Jan 24 '22 13:01 mgorny

@jaraco, seems like this bug was closed by accident.

mgorny avatar Jul 02 '23 01:07 mgorny

Gah. What a mess. Thanks for letting me know.

jaraco avatar Jul 03 '23 01:07 jaraco