The project provides an example of a library written in C++ with the CMake build system, as well as a code wrapping module for working in Python. The C++ library can be used separately, and you can also use a wrapper for Python.
- Ideological inspiration
- Official tutorial
- Official Pybind11 examples
- Interesting tutorial for pybind11
- About CMake expansion setup.py (wrapping)
- cmake
- pybind11
git clone https://github.com/AntonSHBK/simple_cpp_python_porting.git
For use in a project in conjunction with Cmake:
if (NOT TARGET cpp_code)
add_subdirectory(cpp)
endif()
target_link_libraries(${PROJECT_NAME} cpp_code)
The project also uses the OpenMP library, this project can serve as an example of use. It is important that the Pybind11 build project has an import of the OpenMP library
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries( ${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()
mkdir build && cd build
# unix
cmake ..
make
# win (my machine)
cmake .. -G "MinGW Makefiles"
make
Testing the library's performance. For test i use CTest. For verbose output you will use:
cd build
ctest --verbose
The deployment was tested on a Linux system, for use on Windows, you need to look at the official documentation.
pip3 install pybind11
pip3 install . --break-system-packages
Run testing library while in the working directory:
python3 python_test.py
#include <omp.h>
#include <pybind11/pybind11.h>
#include <my_lib/my_include_file.h>
#include "python_header.h"
namespace py = pybind11;
int add(const int &a, const int &b) {
return a + b;
}
PYBIND11_MODULE(CppToPython, m) {
m.def("add", &add); // добавили функцию в модуль
m.def("bob", &talker::bob); // добавили функцию в модуль
py::class_<talker::SomeTalker>(m, "SomeTalker")
.def(py::init<std::string>())
.def("get_omp_max_treads", &talker::SomeTalker::get_omp_max_treads)
.def("get_text_parallel", &talker::SomeTalker::get_text_parallel);
};
There is a Docker container for testing. Running container:
cd /docker
docker-compose up --build
Result:
import CppToPython
print(CppToPython.add(40, 10))
print(CppToPython.bob(40, 10))
print(CppToPython.sum_thread_ids())
print(CppToPython.get_max_threads())
this_talker = CppToPython.SomeTalker('Hello world!')
print(this_talker.get_omp_max_treads())
this_talker.get_text_parallel(4)