python利用Ctypes调用C/C++代码


前言

python调用C/C++代码,我们只是简单调用,因为需要利用C语言中内存动态分配,如果需要在python中使用C++类这种复杂项目,建议使用Boost.python库。这里将C/C++代码编译成so库文件,然后python引用Ctypes库,但是python、C/C++、Ctypes库类型转换需要注意。


一、类型转换

C类型                       Python类型                        ctypes 类型
char                        1-character/string                c_char
wchar_t                     1-character/Unicode、string       c_wchar
char                        int/long                          c_byte
char                        int/long                          c_ubyte
short                       int/long                          c_short
unsigned short              int/long                          c_ushort
int                         int/long                          C_int
unsigned int                int/long                          c_uint
long                        int/long                          c_long
unsigned long               int/long                          c_ulong
long long                   int/long                          c_longlong
unsigned long long          int/long                          c_ulonglong
float                       float                             c_float
double                      float                             c_double
char *(NULL terminated)     string or none                    c_char_p
wchar_t *(NULL terminated)  unicode or none                   c_wchar_p
void *                      int/long or none                  c_void_p

二、使用步骤

1.将C/C++编译成so库文件

利用gcc将需要调用的C/C++模块编译成so库文件:

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

2.代码示例

C/C++:C++代码需要extern C才可进行后续编库,C语言不需要

#include <iostream>
#include "FileOp.h"
extern "C"{
    const char* readFileByBlock(uint a, const char *filename){
        char *c= strdup(readFileBlock(a,filename).c_str());
        return c;
    }

    void writeFileByAddr(uint addr,const char *data,const char *filename){
        string d=data;
        writeFileByAddr(addr,d,filename);
        cout<<"write file by addr success!"<<endl;
    }

    int writeFileByNew(const char *data,const char *filename){
        string d=data;
        int newaddr=writeFileByNewAddr(d,filename);
        cout<<"write file by newaddr success!"<<endl;
        return newaddr;
    }
    int main() {
        string a=readFileBlock(4092,"data_file");
        cout<<a<<endl;
        writeFileByAddr(0,"12345,1231,34125,34124,124234,241234,51234","data_file");
        string b=readFileBlock(4092,"data_file");
        cout<<b<<endl;
        int c=writeFileByNew("12345,1231,34125,34124,124234,241234,51234","data_file");
        cout<<c<<endl;

python:

import ctypes
ll= ctypes.cdll.LoadLibrary
lib=ll("./test.so")
# all=lib.main()
# print all
lib.readFileByBlock.restype = ctypes.c_char_p
lib.readFileByBlock.argtypes = [ctypes.c_int,ctypes.c_char_p]
lib.writeFileByAddr.argtypes = [ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p]
lib.writeFileByNew.argtypes = [ctypes.c_char_p,ctypes.c_char_p]
lib.writeFileByNew.restype = ctypes.c_int
file=ctypes.c_char_p(bytes("./cmake-build-debug/data_file"))
sec=lib.readFileByBlock(0,file)[:-1].split(",")
print (sec)
list=[]
for i in range(0,12):
    list.append('0')
str=ctypes.c_char_p(bytes(list))
lib.writeFileByAddr(0,str,file)
sec=lib.readFileByBlock(0,file)[:-1].split(",")
print (sec)
# str=ctypes.c_char_p(bytes("123,1241,23423,345,5675,567,567,896,23,234,2342"))
new=lib.writeFileByNew(str,file)
print(new)
print ("finish")

总结

以上就是简单的python调C/C++代码,注意类型转换也就是返回参数以及传入参数的类型。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值