You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include <iostream>
#include <pybind11/embed.h>
namespace py = pybind11;
int add_inmain(int i, int j) {
return i + j;
}
PYBIND11_EMBEDDED_MODULE(inmain, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add_inmain, "A function that adds two numbers");
}
int main()
{
py::scoped_interpreter guard{};
auto inmain = py::module_::import("inmain");
std::cout << inmain.attr("add")(1, 2).cast<int>() << std::endl;
auto second = py::module_::import("second");
std::cout << second.attr("add")(1, 2).cast<int>() << std::endl;
auto statlib = py::module_::import("statlib");
std::cout << second.attr("add")(1, 2).cast<int>() << std::endl;
return 0;
}
second.cpp
#include <pybind11/embed.h>
int add_second(int i, int j) {
return i + j;
}
PYBIND11_EMBEDDED_MODULE(second, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add_second, "A function that adds two numbers");
}
statlib.cpp
#include <pybind11/embed.h>
int add_statlib(int i, int j) {
return i + j;
}
PYBIND11_EMBEDDED_MODULE(statlib, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add_statlib, "A function that adds two numbers");
}
result
3
3
terminate called after throwing an instance of 'pybind11::error_already_set'
what(): ModuleNotFoundError: No module named 'statlib'
i'm creating bigger C++ project, where users are able to run "user scripts" written in python.
I'm using static libraries as a way to split up whole project into logical pieces, and one piece was supposed to be my data structures accesible from both python and c++.
Can anyone help me with solution?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, i'm struggling to create embedded module that is defined in static library.
CMakeLists.txt:
main.cpp
second.cpp
statlib.cpp
result
i'm creating bigger C++ project, where users are able to run "user scripts" written in python.
I'm using static libraries as a way to split up whole project into logical pieces, and one piece was supposed to be my data structures accesible from both python and c++.
Can anyone help me with solution?
Beta Was this translation helpful? Give feedback.
All reactions