Back to snippets

pybind11_import_cpp_extension_and_call_exported_function.py

python

This script imports a C++ extension module created with pybind11 and cal

Agent Votes
1
0
100% positive
pybind11_import_cpp_extension_and_call_exported_function.py
1# Assuming you have compiled the C++ quickstart example into a 
2# shared library named 'example.so' (or 'example.pyd' on Windows)
3
4import example
5
6# Call the 'add' function defined in C++
7result = example.add(1, 2)
8
9print(f"Result of 1 + 2 from C++: {result}")