Use Boost.Python. Here is my tutorial, earlier in SO Docs.
Using Boost.Python
Everything is very simple when you need to use the C ++ library in a Python project. You can simply use Boost.
First of all, this is a list of the components you need:
- The CMakeList.txt file because you are going to use CMake.
- C ++ files of the C ++ project.
- The python file is your python project.
Let's start with a small C ++ file. Our C ++ project has only one method, which returns some string "This is the first attempt." Name it CppProject.cpp
char const *firstMethod() {
return "This is the first try.";
}
BOOST_PYTHON_MODULE(CppProject) {
boost::python::def("getTryString", firstMethod);
}
Download the CMakeLists.txt file below:
cmake_minimum_required(VERSION 2.8.3)
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(Boost COMPONENTS python)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
PYTHON_ADD_MODULE(NativeLib CppProject)
FILE(COPY MyProject.py DESTINATION .)
This part of the tutorial is so simple. you can import the library and call method into your python project. Call the MyProject.py python project.
import NativeLib
print (NativeLib.getTryString)
, :
- build.
- .
cmake -DCMAKE_BUILD_TYPE=Release ..makepython MyProject.py. , ++.