I have just created a library with Pybind11 from the C++ side:
I did it with CMake.
Here example.cpp
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
Here CMakeLists.txt (in the same directory as example.cpp)
cmake_minimum_required(VERSION 3.3)
# Notre projet est étiqueté hello
project(example)
find_package(Python COMPONENTS Interpreter Development)
find_package(pybind11 CONFIG)
# pybind11 method:
pybind11_add_module(example example.cpp)
Then I ran the commands
mkdir buildmingw
cd buildmingw
cmake .. -G "MinGW Makefiles"
mingw32-make
and a file example.cp311-win_amd64.pyd is produced.
How can I use this file now in Python?
I already tried putting test.py in the same directory as example.cp311-win_amd64.pyd and then I launched:
python test.py
where test.py looks like this:
import example
print( example.add(7,13 ) )
I get the error message:
D:InformatikNachhilfeInfoUniKadalaSchmittC++PythonBindC++buildmingw4>python tes
t.py
Traceback (most recent call last):
File “D:InformatikNachhilfeInfoUniKadalaSchmittC++PythonBindC++buildmingw4test.py”, line 1, in
import example
ImportError: DLL load failed while importing example: Das angegebene Modul wurde nicht gefunden.
What do I do wrong? How can I correctly import my library?