python 调用C++,传递int,char,char*,数组和多维数组

//C++文件

#include<iostream>

using namespace std;

//该文件名称:cpptest.cpp

//终端下编译指令:

//g++ -o cpptest.so -shared -fPIC cpptest.cpp

//-o 指定生成的文件名,-shared 指定微共享库,-fPIC 表明使用地址无关代码

extern "C"{//在extern “C”中的函数才能被外部调用

    int test(int int_test,char char_test,char *test_string,int int_arr[4],char char_arr2[2][2]) {

        cout<<"输出参数中的int型:";

        cout<<int_test<<endl;

        cout<<"输出参数中的char型:";

        cout<<char_test<<endl;

        cout << "输出参数中的字char*字符:";

        cout<<test_string<<endl;

        cout << "输出参数中的int数组";

        for(int x = 0;x< 4;x++){cout << int_arr[x]<<"    ";}

        cout << endl;

        cout <<"输出参数中的二维数组:";

        for(int x = 0;x<2;x++){

            for(int y = 0;y<2;y++){

            cout <<char_arr2[x][y] << "    ";

            }

        }

        cout << endl;

        return 0;

    }

}

 

 

 

//py文件

import ctypes

mylib = ctypes.cdll.LoadLibrary("cpptest.so")

char_p_test = bytes("中国","utf8")#汉字需用采用utf8编码

int_arr4 = ctypes.c_int*4

int_arr = int_arr4()

int_arr[0] = 1

int_arr[1] = 3

int_arr[2] = 5

int_arr[3] = 9

char_arr2 = ctypes.c_char*2

char_arr22 = char_arr2*2

char_arr22a = char_arr22()

char_arr22a[0][0] = b'a'

char_arr22a[0][1]=  b'b'

char_arr22a[1][0] = b'c'

char_arr22a[1][1] = b'd'

mylib.test(9999,'a',char_p_test,int_arr,char_arr22a)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值