| # | Likes | Tech tags | Title | Creator | Created date | 
|---|---|---|---|---|---|
| 1 | 0 | 
                        cppimport
                     
                        pybind11
                     | 
                    2023-07-11 01:32
                 | 
Binds a C++ function that adds two integers using the pybind11 and cppimport Python modules.
Note: To make the add function accessible, you need to build the C++ file by running python -m cppimport build from the console
add_ints.cpp
// cppimport
#include <pybind11/pybind11.h>
int add(int a, int b) {
    return a + b;
}
PYBIND11_MODULE(add_ints, m) {
    m.def("add", &add, "Add two integers");
}
/*
<%
setup_pybind11(cfg)
%>
*/
example.py
from add_ints import add
print(add(5, 7))
| macros | |
| PYBIND11_MODULE | pybind11.readthedocs.io | 
| pybind11::module_::def | pybind11.readthedocs.io | 
Create a binding for a C++ function and make it callable in your language of choice.