How to compile python code into a .dll file?

17 views
Skip to first unread message

J YZ

unread,
Jun 21, 2024, 4:46:13 AM (10 days ago) Jun 21
to cython-users
  • I first use cython command to generate test.cpp file from my test.pyx . (it was so long and complex)
  • Then I tried to add main function in  test.cpp  and call the pyadd  function transformed from:```cdef public int pyadd(int a, int b): return a + b```  its ok,  but any calling include python class or method will raise pointer error(all the instance pointer is 0).
  • I also tried to compile the test.cpp file in to .dll by g++, compiling succeed:
    •  cant find any function in python (loading: cdll = ctypes.CDLL(test.dll)). this fixed by adding __declspec(dllexport) in function declareation.
    • i can call cdll.pyadd(a,b) in python, but still raise memory(pointer) error in python when calling other method, same as in c++
I'd appreciate your adcise, please.

da-woods

unread,
Jun 21, 2024, 12:59:35 PM (9 days ago) Jun 21
to cython...@googlegroups.com
Cython doesn't generate standalone C or C++ code. Cython generates Cython modules that you need to import properly (to work you MUST call the module initialization function).

You're much better off following the examples in the documentation which use setuptools to compile to a module automatically. You can just import that using `import test`. You don't need ctypes at all.

If you want to use your functions from a C++ program (not Python itself) then follow https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#using-cython-declarations-from-c. Remember that you must initialize the Python interpreter and you must initialize your module before using your functions.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/d8b0b88a-2c9c-45f9-b097-27fb1fb100a6n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages