您的位置:首页 > 编程语言 > Python开发

9. Python与C之导入变量

2019-11-25 18:18 204 查看
  1. 通过对应类型的类方法in_dll的方式来加载。

    struct Test
    {
    int a;
    int b;
    Test(int x=5,int y = 6):a(x),b(y){}
    };
    extern "C" int hello = 1;
    extern "C" int * hellop = &hello;
    extern "C" Test t(1,2);
    extern "C" void show()
    {
    hello = 10;
    }
    from ctypes import *
    class Test(Structure):
    _fields_ = [ ("a",c_int),("b",c_int)]
    handle = CDLL("./test.so")
    hello  = c_int.in_dll(handle,"hello")
    hellop = POINTER(c_int).in_dll(handle,"hellop")
    ttest  = Test.in_dll(handle,"t")
    handle.show()
    print(hello,hellop[0],ttest.a,ttest.b)
  2. 编译执行

    g++ -m32 -shared -fPIC -o test.so test.cpp

    python test.py

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ctypes Python