1

I'm pretty new to python and newer to cython. Since I realized cython is better for me as it is pretty fast, I decided to switch to cython by creating .c files of each .pyx files and I try to compile them manually using the following various techniques:

I create .c file from .pyx using following : I create setup.py file

from distutils.core import setup
from Cython.Build import cythonize

    setup(
        name = "My hello app",
        ext_modules = cythonize("test.pyx"),
    )

It creates a test.c file in same directory. When I try to compile the file using Dev C++, I get following error :

18 C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'

I also tried building .pyx into .c from command prompt using following command :

cython test.pyx

and then doing the following from command prompt to compile using gcc:

python setup.py build_ext --inplace

to create .c file which again fail to compile.

If I try following this link to compile .c file generated by manually including path for Python Includes, I get following error.

I get following error.

PS : I used the gcc -c -IC:\Python27\include -o test.o test.c comand from command line to compile the .c file

PS2 : If I tried manually compiling from cygwin's gcc, it said Fatal Error : No file named Python.h

PS3 : I've manually entered paths for include dirs and libraries in Dev C++ compiler options but the issue persists. The exact error message is as follows :

enter image description here

I tried everything I can find on internet but unable to get any proper solution and I've been facing this issue this since almost a month for which I'm kinda missing deadlines. So It would be very helpful if anyone can guide me in correct path on how to compile .c files generated from Cython.

Thanks in advance

EDIT 1 : I tried the following command to compile my .c file : (In VC++ 2008) E:\My Proj Dir>cl /EHsc test.c /I C:\Python27\include /link C:\Python27\libs\libpython27.a /MACHINE:AMD64

Also tried E:\My Proj Dir>cl /EHsc test.c /I C:\Python27\include /LIBPATH C:\Python27\libs\libpython27.a /MACHINE:AMD64

I get following error : libpython27.a(dmmeh.o) : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

When I tried using /MACHINE:x64, I face the following
test.obj : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

PS : I got AMD64 by importing platform module in my itnterpreter and running platform.machine()

Community
  • 1
  • 1
Debasish Kanhar
  • 1,123
  • 2
  • 15
  • 27
  • The easiest thing is probably to use one of the Python distributions that come with a compiler already set up e.g. [WinPython](https://winpython.github.io/) or [Anaconda](https://store.continuum.io/cshop/anaconda/). You'll probably find the 32 bit versions much easier to get working than the 64 bit versions. – DavidW Sep 02 '15 at 14:02
  • Hi. Thanks for the quick reply. I followed https://github.com/cython/cython/wiki/InstallingOnWindows to install Cython on Anaconda, Configured by PyCharm to use Anaconda Interpreter. And http://docs.cython.org/src/tutorial/cython_tutorial.html#cython-hello-world to build a simple program. I'm able to create .c file. When I compile it using MS VC++ Command Prompt using "cl /EHsc test.c" it says "Fatal Error : No Python.h". Also followed https://github.com/cython/cython/wiki/CythonExtensionsOnWindows this to setup variables. – Debasish Kanhar Sep 02 '15 at 20:22
  • Also, if I try doing import test from iPython notebook (Of Anaconda) I dont get any output of test.pyx file. When I try to do import, does it import .c file or .pyx? So how do I make my python script faster? Sorry for deviating from topic. Is "Import filename" enough to make faster version of script or compiling and running the executable from .c file is faster method? – Debasish Kanhar Sep 02 '15 at 20:25
  • I don't know why it isn't working - I think you have to compile from the Anaconda command line with a correctly set up environment? (but I don't actually use Anaconda myself...) These type of problems are often very difficult to solve, so I'm reluctant to get involved too much further. [For your second question: when you type import filename it loads filename.dll, which it builds from the .c file. Any functions in that should run faster] – DavidW Sep 03 '15 at 07:32
  • Hi, I did proceed in the error I was facing, and somehow resolved the issue of include but now facing with new error. Kindly see my edit to question. – Debasish Kanhar Sep 03 '15 at 07:49
  • Have edited the question. Kindly have a look. – Debasish Kanhar Sep 03 '15 at 07:56
  • Cython works effortlessly on Linux/Android. But on windows there are linker issues. – 0xB00B Jun 04 '21 at 12:48

0 Answers0