LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2012, 08:23 AM   #1
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Rep: Reputation: 10
Cython


hi guys
i'm having a problem with cython.i want to convert hello world program that's been written in python to c.here's the code in python
Code:
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:
   def hello(self, widget, data=None):
     print "Hello World"
  
   def delete_event(self, widget, event, data=None):
     print "delete event occured"
     return False
  
   def destroy(self, widget, data=None):
      gtk.main_quit()
  
   def __init__(self):
      self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
      self.window.connect("delete_event", self.delete_event)
      self.window.connect("destroy", self.destroy)
      self.window.set_border_width(10)
      self.button = gtk.Button("Hello world")
      self.button.connect("clicked", self.hello, None)
      self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
      self.window.add(self.button)
      self.button.show()
      self.window.show()
   def main(self):
      gtk.main()
if __name__ == "__main__":
   hello = HelloWorld()
   hello.main()
i issued below command:
Code:
cython hello.py -o hello.c
and it went well.but when i want to copile it with gcc, i'm getting an error
Code:
gcc -g hello hello.c
hello.c:4:20: fatal error: Python.h: No such file or directory
compilation terminated.
i have Python.h in both /usr/include/python2.6/ and /usr/include/python2.7
!!!!!!!
 
Old 01-18-2012, 01:49 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
To compile the C code, try something like
Code:
make CFLAGS+='-I /usr/include/python2.7' hello
Let make use it's built-in knowledge of how to build a program from a C source file, and where to use commandline args.

--- rod.
 
Old 01-18-2012, 02:02 PM   #3
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster
Rep: Reputation: 10
i issued this command:
Code:
make CFLAGS+='-I /usr/include/python2.7' hello
and i got a whole bunch of errors :
Code:
cc -I /usr/include/python2.7    hello.c   -o hello
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cckpGD49.o: In function `__pyx_pf_11hello_world_10HelloWorld_hello':
hello.c:(.text+0x3b): undefined reference to `PyDict_Size'
hello.c:(.text+0x5a): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xcd): undefined reference to `PyDict_GetItem'
hello.c:(.text+0xfb): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x17c): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x219): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x2fe): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x303): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x30b): undefined reference to `_Py_NoneStruct'
/tmp/cckpGD49.o: In function `__pyx_pf_11hello_world_10HelloWorld_1delete_event':
hello.c:(.text+0x35e): undefined reference to `PyDict_Size'
hello.c:(.text+0x384): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x405): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x433): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x4ae): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x52f): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x5d2): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x6dd): undefined reference to `_Py_ZeroStruct'
hello.c:(.text+0x6eb): undefined reference to `_Py_ZeroStruct'
/tmp/cckpGD49.o: In function `__pyx_pf_11hello_world_10HelloWorld_2destroy':
hello.c:(.text+0x7ba): undefined reference to `PyDict_Size'
hello.c:(.text+0x7d9): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x84c): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x87a): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x8fb): undefined reference to `PyDict_GetItem'
hello.c:(.text+0x998): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xa8b): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0xb09): undefined reference to `PyObject_Call'
hello.c:(.text+0xb9d): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xba2): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0xbaa): undefined reference to `_Py_NoneStruct'
/tmp/cckpGD49.o: In function `__pyx_pf_11hello_world_10HelloWorld_3__init__':
hello.c:(.text+0xca6): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0xd67): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0xdd5): undefined reference to `PyTuple_New'
hello.c:(.text+0xe33): undefined reference to `PyObject_Call'
hello.c:(.text+0xede): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0xf47): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0xf8f): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1005): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1045): undefined reference to `PyTuple_New'
hello.c:(.text+0x10bb): undefined reference to `PyObject_Call'
hello.c:(.text+0x118d): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x11d5): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x124b): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x128b): undefined reference to `PyTuple_New'
hello.c:(.text+0x1301): undefined reference to `PyObject_Call'
hello.c:(.text+0x13d3): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x141b): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1499): undefined reference to `PyObject_Call'
hello.c:(.text+0x1588): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1606): undefined reference to `PyObject_Call'
hello.c:(.text+0x1683): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x16ec): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1734): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x17aa): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x17ea): undefined reference to `PyTuple_New'
hello.c:(.text+0x1844): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x184c): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x1856): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x1877): undefined reference to `PyObject_Call'
hello.c:(.text+0x1949): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1991): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1a52): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1ac8): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1b3e): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1b7e): undefined reference to `PyTuple_New'
hello.c:(.text+0x1c04): undefined reference to `PyObject_Call'
hello.c:(.text+0x1cd6): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1d1e): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1d94): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1dd4): undefined reference to `PyTuple_New'
hello.c:(.text+0x1e32): undefined reference to `PyObject_Call'
hello.c:(.text+0x1f04): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1f4c): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x1fca): undefined reference to `PyObject_Call'
hello.c:(.text+0x206e): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x20b6): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x2134): undefined reference to `PyObject_Call'
hello.c:(.text+0x21c8): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x21cd): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x21d5): undefined reference to `_Py_NoneStruct'
/tmp/cckpGD49.o: In function `__pyx_pf_11hello_world_10HelloWorld_4main':
hello.c:(.text+0x2320): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x239e): undefined reference to `PyObject_Call'
hello.c:(.text+0x2432): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x2437): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x243f): undefined reference to `_Py_NoneStruct'
/tmp/cckpGD49.o: In function `__Pyx_InitCachedConstants':
hello.c:(.text+0x24cf): undefined reference to `PyTuple_New'
hello.c:(.text+0x252e): undefined reference to `PyTuple_New'
hello.c:(.text+0x258a): undefined reference to `PyTuple_New'
/tmp/cckpGD49.o: In function `__Pyx_InitGlobals':
hello.c:(.text+0x262a): undefined reference to `PyInt_FromLong'
/tmp/cckpGD49.o: In function `inithello_world':
hello.c:(.text+0x268e): undefined reference to `PyTuple_New'
hello.c:(.text+0x26db): undefined reference to `PyString_FromStringAndSize'
hello.c:(.text+0x276c): undefined reference to `Py_InitModule4'
hello.c:(.text+0x27b5): undefined reference to `PyImport_AddModule'
hello.c:(.text+0x2805): undefined reference to `PyObject_SetAttrString'
hello.c:(.text+0x2883): undefined reference to `PyObject_SetAttrString'
hello.c:(.text+0x296f): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x2a23): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x2aa1): undefined reference to `PyObject_Call'
hello.c:(.text+0x2b98): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x2bf2): undefined reference to `PyDict_New'
hello.c:(.text+0x2c92): undefined reference to `PyObject_SetItem'
hello.c:(.text+0x2d53): undefined reference to `PyObject_SetItem'
hello.c:(.text+0x2e14): undefined reference to `PyObject_SetItem'
hello.c:(.text+0x2ed5): undefined reference to `PyObject_SetItem'
hello.c:(.text+0x2f96): undefined reference to `PyObject_SetItem'
hello.c:(.text+0x3065): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x314f): undefined reference to `PyObject_RichCompare'
hello.c:(.text+0x328b): undefined reference to `PyObject_Call'
hello.c:(.text+0x330b): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x33bf): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x343d): undefined reference to `PyObject_Call'
hello.c:(.text+0x34d3): undefined reference to `PyDict_New'
hello.c:(.text+0x3522): undefined reference to `PyObject_SetAttr'
hello.c:(.text+0x3620): undefined reference to `PyErr_Occurred'
hello.c:(.text+0x3629): undefined reference to `PyExc_ImportError'
hello.c:(.text+0x3639): undefined reference to `PyErr_SetString'
/tmp/cckpGD49.o: In function `__Pyx_RaiseArgtupleInvalid':
hello.c:(.text+0x368c): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x36bf): undefined reference to `PyErr_Format'
/tmp/cckpGD49.o: In function `__Pyx_RaiseDoubleKeywordsError':
hello.c:(.text+0x36d2): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x36ed): undefined reference to `PyErr_Format'
/tmp/cckpGD49.o: In function `__Pyx_ParseOptionalKeywords':
hello.c:(.text+0x3771): undefined reference to `PyString_Type'
hello.c:(.text+0x37cb): undefined reference to `_PyString_Eq'
hello.c:(.text+0x384b): undefined reference to `_PyString_Eq'
hello.c:(.text+0x387e): undefined reference to `PyDict_SetItem'
hello.c:(.text+0x38ae): undefined reference to `PyDict_Next'
hello.c:(.text+0x38de): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x38f5): undefined reference to `PyErr_Format'
hello.c:(.text+0x3903): undefined reference to `PyString_AsString'
hello.c:(.text+0x3909): undefined reference to `PyExc_TypeError'
hello.c:(.text+0x3924): undefined reference to `PyErr_Format'
/tmp/cckpGD49.o: In function `__Pyx_GetName':
hello.c:(.text+0x3946): undefined reference to `PyObject_GetAttr'
hello.c:(.text+0x3954): undefined reference to `PyExc_NameError'
hello.c:(.text+0x3963): undefined reference to `PyErr_SetObject'
/tmp/cckpGD49.o: In function `__Pyx_Import':
hello.c:(.text+0x39a6): undefined reference to `PyObject_GetAttrString'
hello.c:(.text+0x39cd): undefined reference to `PyList_New'
hello.c:(.text+0x39e9): undefined reference to `PyModule_GetDict'
hello.c:(.text+0x39f7): undefined reference to `PyDict_New'
hello.c:(.text+0x3a2f): undefined reference to `PyObject_CallFunctionObjArgs'
/tmp/cckpGD49.o: In function `__Pyx_FindPy2Metaclass':
hello.c:(.text+0x3b08): undefined reference to `PyObject_GetAttrString'
hello.c:(.text+0x3b16): undefined reference to `PyErr_Clear'
hello.c:(.text+0x3b28): undefined reference to `PyClass_Type'
/tmp/cckpGD49.o: In function `__Pyx_CreateClass':
hello.c:(.text+0x3b5d): undefined reference to `PyDict_SetItemString'
hello.c:(.text+0x3b7e): undefined reference to `PyDict_GetItemString'
hello.c:(.text+0x3bcc): undefined reference to `PyObject_CallFunctionObjArgs'
/tmp/cckpGD49.o: In function `__pyx_binding_PyCFunctionType_NewEx':
hello.c:(.text+0x3c0e): undefined reference to `_PyObject_GC_New'
hello.c:(.text+0x3c6a): undefined reference to `PyObject_GC_Track'
/tmp/cckpGD49.o: In function `__pyx_binding_PyCFunctionType_dealloc':
hello.c:(.text+0x3c80): undefined reference to `PyObject_GC_UnTrack'
hello.c:(.text+0x3cf3): undefined reference to `PyObject_GC_Del'
/tmp/cckpGD49.o: In function `__pyx_binding_PyCFunctionType_descr_get':
hello.c:(.text+0x3d02): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x3d24): undefined reference to `PyMethod_New'
/tmp/cckpGD49.o: In function `__pyx_binding_PyCFunctionType_init':
hello.c:(.text+0x3d39): undefined reference to `PyCFunction_Type'
hello.c:(.text+0x3d70): undefined reference to `PyType_Ready'
/tmp/cckpGD49.o: In function `__Pyx_GetStdout':
hello.c:(.text+0x3da4): undefined reference to `PySys_GetObject'
hello.c:(.text+0x3db2): undefined reference to `PyExc_RuntimeError'
hello.c:(.text+0x3dc2): undefined reference to `PyErr_SetString'
/tmp/cckpGD49.o: In function `__Pyx_Print':
hello.c:(.text+0x3e0a): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3e21): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x3e56): undefined reference to `PyFile_WriteObject'
hello.c:(.text+0x3e81): undefined reference to `PyString_AsString'
hello.c:(.text+0x3e8f): undefined reference to `PyString_Size'
hello.c:(.text+0x3ee4): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3f10): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x3f2e): undefined reference to `PyFile_SoftSpace'
/tmp/cckpGD49.o: In function `__Pyx_PrintOne':
hello.c:(.text+0x3f69): undefined reference to `PyFile_SoftSpace'
hello.c:(.text+0x3f80): undefined reference to `PyFile_WriteString'
hello.c:(.text+0x3fa5): undefined reference to `PyFile_WriteObject'
hello.c:(.text+0x3fc3): undefined reference to `PyFile_WriteString'
/tmp/cckpGD49.o: In function `__Pyx_AddTraceback':
hello.c:(.text+0x400e): undefined reference to `PyString_FromString'
hello.c:(.text+0x404a): undefined reference to `PyString_FromFormat'
hello.c:(.text+0x405a): undefined reference to `PyString_FromString'
hello.c:(.text+0x4074): undefined reference to `PyModule_GetDict'
hello.c:(.text+0x410e): undefined reference to `PyCode_New'
hello.c:(.text+0x411c): undefined reference to `_PyThreadState_Current'
hello.c:(.text+0x413a): undefined reference to `PyFrame_New'
hello.c:(.text+0x415a): undefined reference to `PyTraceBack_Here'
/tmp/cckpGD49.o: In function `__Pyx_InitStrings':
hello.c:(.text+0x4264): undefined reference to `PyUnicodeUCS4_DecodeUTF8'
hello.c:(.text+0x4286): undefined reference to `PyString_InternFromString'
hello.c:(.text+0x42aa): undefined reference to `PyString_FromStringAndSize'
/tmp/cckpGD49.o: In function `__Pyx_PyObject_IsTrue':
hello.c:(.text+0x42e7): undefined reference to `_Py_TrueStruct'
hello.c:(.text+0x42f7): undefined reference to `_Py_ZeroStruct'
hello.c:(.text+0x4309): undefined reference to `_Py_NoneStruct'
hello.c:(.text+0x4325): undefined reference to `PyObject_IsTrue'
collect2: ld returned 1 exit status
make: *** [hello] Error 1
i don't know what's wrong!!!
and this code has been generated by cython:
http://tinypaste.com/3bad7d61
 
Old 01-18-2012, 03:37 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I don't know about your familiarity with C, but I will point out that any C program must have a function 'main()', and that is the default entry point for the program. The C code that you've generated doesn't have any such function, and that accounts for the error about not finding it.
I don't know anything about Cython, but I can speculate about a couple of ideas. It seems possible that you must explicitly tell Cython to generate code that includes a main() function. It is also possible that you must tell the linker to include a support library that may include the main() function and possibly also includes definitions for the other symbols listed in the error output. If the latter is the case, you could potentially build the program by adding the library to the make commandline:
Code:
make CFLAGS+='-I /usr/include/python2.7' LDFLAGS+='-L /some/path/to/cython/libs -lcythonSupport' hello
I made up the name of the library, of course. If it were a real library, its full name would actually be 'libcythonSupport.so', and make/gcc would know what the notation I used means.

I would consult the Cython documentation to see if this kind of thing is discussed.

--- rod.
 
Old 01-20-2012, 01:04 AM   #5
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster
Rep: Reputation: 10
i went through cython's documents but nothing came up !!!
 
Old 01-20-2012, 04:20 AM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

I managed to run your code. In fact I just follow instructions given here. That is:
0) installed dev package.
Code:
sudo apt-get install python-dev
1) saved your example code to hello_world.pyx
2) created setup.py:
Code:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("hello_world", ["hello_world.pyx"])]
)
3) run
Code:
$ python setup.py build_ext --inplace
4) Test. From ipython (or python):
Code:
import hello_world
hello_world.HelloWorld()
Hope that helps.
 
Old 01-20-2012, 08:13 AM   #7
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster
Rep: Reputation: 10
i did as you said and then i ran the python in shell.i could import hello_world and i could run my program. but my question is how can i run the c version?
there's a hello_world.c file generated by the process you said, but i don't know how to run it !!!
 
Old 01-20-2012, 08:48 AM   #8
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
As described in this talk, to create executable

1) add main() function using --embed option to cython:
Code:
$ cython --embed hello_world.pyx
2) Compile the code:
Code:
$ gcc $CFLAGS -I/usr/include/python2.6    -o hw hello_world.c -lpython2.6 -lpthread -lm -lutil -ldl
3) Run
Code:
$ ./hw
 
Old 01-20-2012, 09:42 AM   #9
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster
Rep: Reputation: 10
it worked !!!
thank you so much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration