OSX中动态库的Ctypes“未找到符号”

3 投票
1 回答
2871 浏览
提问于 2025-04-15 23:06

我做了一个C++库,并从中构建了一个.dylib动态库。但是当我用ctypes加载它时,失败了。看起来有些东西没有正确链接。我不知道为什么。错误信息(相关部分):

    cscalelib.setup_framebuffer(flip,surface.frame_buffer,surface.texture,surface._scale[0],surface._scale[1])
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", line 325, in __getattr__
    func = self.__getitem__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ctypes/__init__.py", line 330, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x56ecd0, setup_framebuffer): symbol not found

这是我正在进行中的C++代码,虽然还没完成,但目前的部分应该可以正常工作。

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <vector.h>

void setup_framebuffer(bool flip,GLuint frame_buffer_id,GLuint texture_id,int width,int height){
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frame_buffer_id);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture_id, 0);
    glPushAttrib(GL_VIEWPORT_BIT);
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //Load the projection matrix
    if (flip){
        gluOrtho2D(0,width,height,0);
    }else{
        gluOrtho2D(0,width,0,height);
    }
}
void end_framebuffer(){
    glPopAttrib();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //Load the projection matrix
    gluOrtho2D(0,1280,720,0); //Set an orthorgraphic view
}
void add_lines(bool antialias,vector< vector<double> > coordinates,double w,double r,double g, double b,double a){
    glDisable(GL_TEXTURE_2D);
    if (antialias){
        glEnable(GL_LINE_SMOOTH); //Enable line smoothing.
    }
    glColor4d(r,g,b,a);
    glLineWidth(w);
    glBegin(GL_LINE_STRIP);
    for (int x = 0; x < coordinates.size(); x++) {
        glVertex2d(coordinates[x][0],coordinates[x][1]);
    }
    glEnd();
    if (antialias){
        glDisable(GL_LINE_SMOOTH); //Disable line smoothing.
    }
    glEnable(GL_TEXTURE_2D);
}

我用以下命令编译它:

g++ -dynamiclib CPPEXTSCALELIB.cp -framework opengl -arch i386 -o CPPEXTSCALELIB.dylib

这是Python代码,"..."表示不相关的部分。

...
from ctypes import *
...
cscalelib = CDLL(os.path.dirname(sys.argv[0]) + "/CPPEXTSCALELIB.dylib")
...
def setup_framebuffer(surface,flip=False):
    #Create texture if not done already
    if surface.texture is None:
        create_texture(surface)
    #Render child to parent
    if surface.frame_buffer is None:
        surface.frame_buffer = glGenFramebuffersEXT(1)
    cscalelib.setup_framebuffer(flip,surface.frame_buffer,surface.texture,surface._scale[0],surface._scale[1])
...

谢谢。

1 个回答

3

这个问题很可能是因为你在使用C++,所以函数的名字会被“搞乱”,并且会使用C++特有的调用方式。如果你用extern "C"来声明这个函数,那么它就会以一种可以被C代码调用的方式导出(也可以被Python的CTypes模块调用)。

撰写回答