Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
BATAN suggested himself)
From the beginning we create a C ++ header file (Forgive me C ++-shniki :-))
Person.hpp
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;
namespace person{
class CppPerson{
public:
string name;
int age;
CppPerson();
CppPerson(const string &name, const int &age);
~CppPerson();
void setName(const string &name);
string getName();
int getAge();
};
}
#endif
#include <string>
#include "Person.hpp"
namespace person{
CppPerson::CppPerson(){}
CppPerson::CppPerson(const string &name, const int &age){
this->name = name;
this->age = age;
}
CppPerson::~CppPerson(){}
void CppPerson::setName(const string &name){
this->name = name;
}
string CppPerson::getName(){
return name;
}
int CppPerson::getAge(){
return age;
}
}
from libcpp.string cimport string
cdef extern from "Person.hpp" namespace "person":
cdef cppclass CppPerson:
CppPerson(string name, int age) except +
void setName(string name)
string getName()
int getAge()
cdef class Person:
cdef CppPerson *cpp_person
def __cinit__(self, name, age):
self.cpp_person = new CppPerson(name.encode(), age)
if self.cpp_person == NULL:
raise MemoryError('Not enough memory.')
def __dealloc__(self):
del self.cpp_person
def setName(self, name):
self.cpp_person.setName(name.encode())
def getName(self):
return self.cpp_person.getName().decode()
def getAge(self):
return self.cpp_person.getAge()
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules = cythonize(Extension(
"person_module", # the extesion name
sources=["person_module.pyx", "Person.cpp"], # the Cython source and
# additional C++ source files
language="c++", # generate and compile C++ code
)))
from person_module import Person
def main():
alexandr = Person("Александр", 35)
print(alexandr.getName())
print(alexandr.getAge())
if __name__ == '__main__':
main()
For Windows: https://docs.microsoft.com/en-us/visualstudio/pyth...
And for everyone else: https://docs.python.org/3/extending/building.html
Look at this https://www.boost.org/doc/libs/1_65_1/libs/python/...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question