Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Yggdroot opened this issue Feb 21, 2025 · 0 comments
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.

Comments

@Yggdroot
Copy link

Yggdroot commented Feb 21, 2025

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
@Yggdroot Yggdroot added bug Needs Triage Issues that need to be evaluated for severity and status. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Needs Triage Issues that need to be evaluated for severity and status.
Projects
None yet
Development

No branches or pull requests

1 participant