python - 未找到符号,应在 : flat namespace 中

标签 python python-3.x compilation installation cython

我有一个巨大的 gl.pxd 文件,其中包含 gl.hglu.hglut 的所有定义。 h.例如它有这些行:

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

我有一个 window.pyx 文件,它看起来像这样:

# 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()

我还有一个 setup.py,它看起来像这样:

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'])]
)

我用 python3 setup.py build_ext --inplace 调用它并编译,输出是这样的:

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

我有一个如下所示的 window_test.py:

import window
window.main()

但是如果我想运行这个 python 片段,我会得到这个错误:

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

我的问题和这个很相似: What is the meaning of this ImportError when importing a Cython generated .so file? -- 尽管据我所知我没有外部库,但我想使用内置 OpenGL 库...

哦,我正在使用 Mac OS X 10.8.5、Cython 19.2 和 Python 3.3。 任何帮助将不胜感激!

提前致谢!

最佳答案

即使 OpenGL 和 GLUT 在系统上(内置)我必须在编译过程中将它们作为框架链接,所以 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 ))

关于python - 未找到符号,应在 : flat namespace 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19573488/

相关文章:

python - 当字符位于 unicode 范围内时,如何用空格填充字符?

Python - 如何使用 xml.etree.ElementTree 迭代每个 xml 节点返回列表?

python - 打印直到python中的特定字符位置

assembly - OpenCL 在线编译:从 cl::program 或 cl::kernel 获取程序集

c++ - 如何使用 g++ 编译和链接库 (SDL) 以便我拥有独立程序?

c++ - 获取指针和常规 int 的奇怪值

python - 同一页面上具有相同 radio 字段的多个 WTForm

python - 使用 Numpy 进行低效正则逻辑回归

python - python : 语法错误

django - 如何使用 BeautifulSoup 搜索出现在另一个元素之前的元素?