T
T
The-TS2020-02-23 21:15:49
Python
The-TS, 2020-02-23 21:15:49

How to use C++ method in Python code?

Let's say there is a Python code where the method is called (let's say t.test()). How to make the test() method written in C++

Like a module for Python in C++
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2020-02-23
@Pyhon3x

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

Further implementation of
Person.cpp
#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;
    }
}

Then Cython
person_module.pyx comes into play
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()

Next
setup.py
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
      )))

Now you need to feed all this miracle to Cython.
In the same folder where all these files are located, run cmd (Or run cmd from the beginning and then
go to this directory)

In cmd , write
python setup.py build_ext --inplace
Well,
the test.py test
from person_module import Person

def main():
    alexandr = Person("Александр", 35)
    print(alexandr.getName())
    print(alexandr.getAge())

if __name__ == '__main__':
    main()

Cython Documentation

A
Alexander, 2020-02-23
@NeiroNx

For Windows: https://docs.microsoft.com/en-us/visualstudio/pyth...
And for everyone else: https://docs.python.org/3/extending/building.html

A
ardazishvili, 2020-02-24
@ardazishvili

Look at this https://www.boost.org/doc/libs/1_65_1/libs/python/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question