python调用C++

编译命令:g++ test.c fPIC -shared -o libtest.so

call.py:

      1 import ctypes
      2 from ctypes import *
      3 so = ctypes.CDLL("./libtest.so")
      4 s = create_string_buffer('/000'*32)
      5 so.display(s)
      6 print repr(s.value)
      7 so.display_int(1)
test.c:

      1 #include <iostream>
      2 #include <string>
      3 using namespace std;
      4 
      5 class TestLib{
      6     public:
      7     void display(char* strTest);
      8     void display(int a);
      9 };
     10 
     11 void TestLib::display(char* strTest)
     12 {
     13     //strTest = "helloo";
     14     strcpy(strTest,"hellooworld!!");
     15     cout<<"fist display"<<endl;
     16 }
     17 
     18 void TestLib::display(int a)
     19 {
     20     cout<<"second dispay"<<endl;
     21 }
     22 
     23 extern "C"{
     24     TestLib obj;
     25     void display(char* str)
     26     {
     27         obj.display(str);
     28     }
     29 
     30     void display_int(){
     31         obj.display(2);
     32     }
     33 }

调用C++时,传入的是字符串。


参考资料:

ctypes使用方法:http://blog.csdn.net/jhonguy/article/details/7698350

python和python相互调用:http://www.linuxidc.com/Linux/2011-10/44837.htm

python 如何在一个文件中调用另一个文件的类




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值