Python – How to provide a C++ version when extending python

How to provide a C++ version when extending python… here is a solution to the problem.

How to provide a C++ version when extending python

I want to make C++ code callable from Python.

https://docs.python.org/3/extending/ explains how to do this, but does not mention how to specify the C++ version.

By default, distutils calls G++ with a bunch of parameters, but does not provide a version parameter.
setup.py Example:

from distutils.core import setup, Extension

MOD = "ext"

module = Extension("Hello", sources = ["hello.cpp"])

setup(
    name="PackageName",
    version="0.01",
    description="desc",
    ext_modules = [module]
)

If it’s important, I’m using Linux.

Solution

For example, you can pass compiler arguments as extra_compile_args

module = Extension(
  "Hello",
  sources = ["hello.cpp"],
  extra_compile_args = ["-std=c++20"]
)

Related Problems and Solutions