Skip to content

[BUG] Even after setting language="c++", the compilation still uses /Tcmyext.c #4845

Open
@Yggdroot

Description

@Yggdroot

setuptools version

75.8.0

Python version

3.12

OS

Windows11

Additional environment information

VS2022

Description

When compiling a C extension using setuptools, despite setting language="c++" and extra_compile_args=["/TP"] in the setup.py file, MSVC still compiles the file as C, using the /Tcmyext.c option instead of compiling it as C++.

Actual Behavior:
The compilation command still uses /Tcmyext.c and compiles the file as C instead of C++.

Expected behavior

MSVC should compile the myext.c file as C++ according to the /TP option and language="c++" setting.

How to Reproduce

myext.c

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#define THREAD_LOCAL thread_local

THREAD_LOCAL int counter = 0;  

static PyObject* increment(PyObject* self, PyObject* args) {
    counter += 1;
    return PyLong_FromLong(counter);
}

static PyMethodDef MyMethods[] = {
    {"increment", increment, METH_NOARGS, "Increment thread-local counter"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef mymodule = {
    PyModuleDef_HEAD_INIT, "myext", NULL, -1, MyMethods
};

PyMODINIT_FUNC PyInit_myext(void) {
    return PyModule_Create(&mymodule);
}

setup.py

from setuptools import setup, Extension

module = Extension(
    "myext",
    sources=["myext.c"],  # C file
    extra_compile_args=["/TP"],  # Force C++ compilation
    language="c++"  # Specify language as C++
)

setup(
    name="MyExtension",
    ext_modules=[module],
)

Run the compilation command:

python setup.py build_ext --inplace

Output

"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\foo\AppData\Local\Programs\Python\Python312\include -IC:\Users\foo\AppData\Local\Programs\Python\Python312\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" /Tcmyext.c /Fobuild\temp.win-amd64-cpython-312\Release\myext.obj /TP
myext.c
myext.c(11): error C2054: expected '(' to follow 'thread_local'
myext.c(11): error C2085: 'counter': not in formal parameter list
myext.c(11): error C2143: syntax error: missing ';' before '='
myext.c(14): error C2065: 'counter': undeclared identifier
myext.c(15): error C2065: 'counter': undeclared identifier

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions