12

I'm trying to compile python source code foo.py to C using cython.

In foo.py:

print "Hello World"

The command I'm running is cython foo.py.

The problem is that when compiling foo.c using gcc, I get the error:

undefined reference to 'main'.

0

3 Answers 3

27

when converting the code from python to c (using Cython) it converts it to c code which can be compiled into a shared object. in order to make it executable, you should add "--embed" to cython conversion command. this flag adds the 'main' function you need, so you could compile the c code into executable file. please notice you'll need the python .so runtime libraries in order to run the exec.

Sign up to request clarification or add additional context in comments.

1 Comment

9

Read the Cython documentation. This will also (hopefully) teach you what Cython is and what it isn't. Cython is for creating python extensions (not a general-purpose Python-to-C-compiler), which are shared objects/dlls. Dynamically loaded libraries don't have a main function like standalone programs, but compilers assume that they are ultimately linking an executable. You have to tell them otherwise via flags (-shared methinks, but again, refer to the Cython documentation) - or even better, don't compile yourself, use a setup.py for this (yet again, read the Cython documentation).

3 Comments

for me this answer resulted in compilation of a working extension. answer of RoeeK resolved the linking problem, but after Python failed to import with error ImportError: dynamic module does not define init function. thank you really much, I think would be nicer to write it with less lecturing style without assuming stupidity as the 3 rtfm suggests.
This answer would be perfect if it included a link to the Cython docs and the Cython FAQ.
Not a very helpful answer. It's basically a RTFM but wrong as proven by the accepted answer.
0

The usual way is to use distutils to compile the cython-generated file. This also gives you all the include directories you need in a portable way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.