Python,c++ 混编
请教一下各位大佬;怎么在Python中调用c++代码?
程序代码:
//c++
//Python调用c++(类)动态链接库
#include <iostream>
using namespace std;
class TestLib
{<!-- -->
public:
void display();
void display(int a);
};
void TestLib::display() {<!-- -->
cout<<"First display"<<endl;
}
void TestLib::display(int a) {<!-- -->
cout<<"Second display:"<<a<<endl;
}
extern "C" {<!-- -->
TestLib obj;
void display() {<!-- -->
obj.display();
}
void display_int(int a) {<!-- -->
obj.display(a);
}
}
程序代码:
import ctypes
dll = ctypes.cdll.LoadLibrary
lib = dll('./libpycallcpp.so') #刚刚生成的库文件的路径
lib.display()
lib.display_int(0)
[此贴子已经被作者于2023-1-28 21:03编辑过]
