Symbol not found, Expected in: flat namespace

3,256 views
Skip to first unread message

Peter Varo

unread,
Oct 25, 2013, 5:58:33 AM10/25/13
to cython...@googlegroups.com
Hello everyone,

I have the following setup: a huge gl.pxd file with all the definitions of gl.h, glu.h and glut.h. For example it has these lines:

    cdef extern from '<OpenGL/gl.h>':
        ctypedef unsigned int GLenum
        cdef void glBegin( GLenum mode )

I have a window.pyx file, which looks like this:

    # Import OpenGL definitions
    # headers of gl, glu and glut
    from gl cimport *

    cdef int argcp
    cdef char **argv

    cdef void render_scene():

        glClear( GL_COLOR_BUFFER_BIT )

        glBegin( GL_TRIANGLES )
        glVertex2f( -.5, -.5)
        glVertex2f( .5, 0 )
        glVertex2f( 0, -5. )
        glEnd()

        glutSwapBuffers()

    cpdef main():
        # Initialize GLUT and create Window
        glutInit( &argcp, argv )
        glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE )
        glutInitWindowPosition( 100, 100 )
        glutInitWindowSize( 1280, 720 )
        glutCreateWindow( 'My Shiny New Window' )

        # Register callbacks
        glutDisplayFunc( render_scene )

        # Enter GLUT event processing cycle
        glutMainLoop()

I also have a setup.py which looks like this:

    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('window', ['window.pyx'])]
    )

Which I call with python3 setup.py build_ext --inplace and it compiles, and the output is this:


    running build_ext
    cythoning window.pyx to window.c
    building 'window' extension
    /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -arch i386 -arch x86_64 -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c window.c -o build/temp.macosx-10.6-intel-3.3/window.o
    /usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/temp.macosx-10.6-intel-3.3/window.o -o /Users/petervaro/cygl/window.so

And I have a window_test.py which looks like this:

    import window
    window.main()

But if I want to run this python snippet I got this error:

    Traceback (most recent call last):
      File "/Users/petervaro/cygl/window_test.py", line 3, in <module>
        import window
    ImportError: dlopen(/Users/petervaro/cygl/window.so, 2): Symbol not found: _glBegin
      Referenced from: /Users/petervaro/cygl/window.so
      Expected in: flat namespace
     in /Users/petervaro/cygl/window.so

My problem is really similar to this one: http://stackoverflow.com/q/7068721/2188562 -- although afaik I don't have an external library, I want to use the builtin OpenGL lib...

Oh, and I'm on Mac OS X 10.8.5, Cython 19.2 and Python 3.3.
Any help will be appreciated!

Thanks in advance!

Stefan Behnel

unread,
Oct 25, 2013, 6:11:04 AM10/25/13
to cython...@googlegroups.com
Peter Varo, 25.10.2013 11:58:
> I have the following setup: a huge gl.pxd file with all the definitions of
> gl.h, glu.h and glut.h. For example it has these lines:
>
> cdef extern from '<OpenGL/gl.h>':
> ctypedef unsigned int GLenum
> cdef void glBegin( GLenum mode )

I'm sure others have done that before, but I'm not sure if anyone has
published these declarations under a free license and pushed them to PyPI.
Looks to me like it's worth doing. Preferably with one .pxd file per header
file, though, just for clarity.


> I also have a setup.py which looks like this:
>
> 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('window', ['window.pyx'])]
> )

You should switch to using cythonize() instead of overriding "build_ext".

http://docs.cython.org/src/reference/compilation.html#compiling-with-distutils


> My problem is really similar to this one:
> http://stackoverflow.com/q/7068721/2188562 -- although afaik I don't have
> an external library, I want to use the *builtin* OpenGL lib...

There is no OpenGL library built into Python. That library is really an
external library, so you have to explicitly link against it in your setup.py.

Stefan

Peter Varo

unread,
Oct 25, 2013, 9:12:21 AM10/25/13
to cython...@googlegroups.com
Thanks for the quick reply!

I already created the pxd files based on the original h files, it didn't take much time -- and as you mentioned I found them as part of some projects but not with the proper licensing.

Funny fact is, that I used cythonize() before -- I replaced it because I found this solution everywhere.. So are you saying, that it is safer to use cythonize? -- I replace that, thanks!

I'm pretty new to C programming and Cython also, and I thought that the h files will link to their c files.. Can you help me on this: which file/files/libs should I include in the setup.py and how exactly should I do that?

It is also a question for me: if I want to split this pxd into pieces -- how can I include the content of one into another (like the typdef GLenum which is declared in gl, but used in glu and glut as well) afaik you cant include executeable statements in pxd files (except DEFs and IF/ELIF/ELSE statements), which means no import, right?

Thanks again,
P

Peter Varo

unread,
Oct 25, 2013, 12:29:30 PM10/25/13
to cython...@googlegroups.com
Never mind -- I finally solved my problem, I have to link OpenGL and GLUT as frameworks thru the compilation, with setup.py:

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

exts = Extension( name='window',
                  sources=['window.pyx'],
                  extra_link_args=['-framework', 'OpenGL', '-framework', 'GLUT'] )

setup( name='cygl',
       ext_modules = cythonize( exts ))

Stefan Behnel

unread,
Oct 25, 2013, 12:50:31 PM10/25/13
to cython...@googlegroups.com
Peter Varo, 25.10.2013 15:12:
> if I want to split this pxd into pieces -- how can I include the content
> of one into another (like the typdef GLenum which is declared in gl, but
> used in glu and glut as well) afaik you cant include executeable
> statements in pxd files (except DEFs and IF/ELIF/ELSE statements), which
> means no import, right?

No, (c!)imports are just fine here, and the way to go. Example:

====== gl.pxd ========
cdef extern from "gl.h":
# ... decls

===== glut.pxd =======
from .gl cimport some_type, other_decl

cdef extern from "glut.h":
int some_func(some_type x)
...

Stefan

Peter Varo

unread,
Oct 25, 2013, 1:42:33 PM10/25/13
to cython...@googlegroups.com, stef...@behnel.de
Thank you very much!!! Everything is working as it should ;)
Reply all
Reply to author
Forward
0 new messages