转载
https://www.geeksforgeeks.org/how-to-call-c-c-from-python/
geek.cpp
#include <iostream>
class Geek{
public:
void myFunction(){
std::cout << "Hello Geek!!!" << std::endl;
}
};
extern "C" {
Geek* Geek_new(){
return new Geek();
}
void Geek_myFunction(Geek* geek){
geek -> myFunction();
}
}
int main()
{
// Creating an object
Geek t;
// Calling function
t.myFunction();
return 0;
}
pycallgeek.py
# import the module
from ctypes import cdll
# load the library
lib = cdll.LoadLibrary('./libgeek.so')
# create a Geek class
class Geek(object):
# constructor
def __init__(self):
# attribute
self.obj = lib.Geek_new()
# define method
def myFunction(self):
lib.Geek_myFunction(self.obj)
# create a Geek class object
f = Geek()
# object method calling
f.myFunction()