pypa / pypa/setuptools

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

Open
#4,845 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Needs Triage
Dominant language
Python
Stars
2.9k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with setup.py and myext.c using python setup.py build_ext --inplace, then trace the build_ext path that constructs the MSVC command. Confirm how language="c++", /TP, and the /Tcmyext.c option interact; done means the extension is compiled as C++ on the reported Windows environment and the behavior has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.