ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c-OSX中动态库的Ctypes“找不到符号”

2019-12-10 02:57:58  阅读:315  来源: 互联网

标签:linker ctypes g python c-4


我制作了一个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-框架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])
...

谢谢.

解决方法:

问题很可能是您正在使用C,因此函数名称将被混淆并使用C调用约定.如果用extern“ C”声明函数,则应以可从C代码(和Python的CTypes模块)调用的方式导出该函数.

标签:linker,ctypes,g,python,c-4
来源: https://codeday.me/bug/20191210/2098729.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有