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] Cannot cythonize C++ source if function isn't explicitly used in a new Python function in the same file #6185

Open
danilo-bc opened this issue May 6, 2024 · 0 comments

Comments

@danilo-bc
Copy link

Describe the bug

This started as a Stack Overflow question and was pointed to be a possible bug.

Code to reproduce the behaviour:

hello.cpp

#include <iostream>

void hello_from_cpp() {
    std::cout << "Hello from C++!" << std::endl;
}

cpp_hello.pyx

# distutils: language = c++
# cython: language_level = 3
cpdef extern void hello_from_cpp()

def myfun():
    hello_from_cpp()

setup.py

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    ext_modules=cythonize([
        Extension("cpp_hello", ["cpp_hello.pyx", "hello.cpp"])
        ]),
)

main.py

import cpp_hello

if __name__ == "__main__":
    cpp_hello.hello_from_cpp()

Built with python setup.py build_ext --inplace or python setup.py build_ext -i, tested with python main.py. The code compiles and presents the string in the terminal, but if the lines defining myfun in cpp_hello.pyx are removed, it fails to compile:

[1/1] Cythonizing cpp_hello.pyx
running build_ext
building 'cpp_hello' extension
creating build
creating build/temp.linux-x86_64-cpython-310
x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -I/usr/include/python3.10 -c cpp_hello.cpp -o build/temp.linux-x86_64-cpython-310/cpp_hello.o
cpp_hello.cpp: In function ‘int __pyx_pymod_exec_cpp_hello(PyObject*)’:
cpp_hello.cpp:3027:84: error: ‘hello_from_cpp’ was not declared in this scope
 3027 |     PyObject* wrapped = __Pyx_CFunc_9cpp_hello_void__lParen__rParennoexcept_to_py_(hello_from_cpp);
      |                                                                                    ^~~~~~~~~~~~~~
cpp_hello.cpp: At global scope:
cpp_hello.cpp:2362:18: warning: ‘PyObject* __Pyx_CFunc_9cpp_hello_void__lParen__rParennoexcept_to_py_(void (*)())’ defined but not used [-Wunused-function]
 2362 | static PyObject *__Pyx_CFunc_9cpp_hello_void__lParen__rParennoexcept_to_py_(void (*__pyx_v_f)(void)) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

Expected behaviour

I should be able to expose the C++ function to Python using cpdef extern void hello_from_cpp() without requiring additional code.

OS

Linux/WSL2

Python version

3.10.12

Cython version

3.0.10

Additional context

I also tried the alternative declaration cdef extern from "hello.cpp", but I can't create a homonimous function hello_from_cpp() as I wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants