embed.cpp
Go to the documentation of this file.
1 #include <pybind11/embed.h>
2 namespace py = pybind11;
3 
4 PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) {
5  m.def("add", [](int i, int j) { return i + j; });
6 }
7 
8 int main(int argc, char *argv[]) {
9  if (argc != 2) {
10  throw std::runtime_error("Expected test.py file as the first argument");
11  }
12  auto *test_py_file = argv[1];
13 
14  py::scoped_interpreter guard{};
15 
16  auto m = py::module_::import("test_cmake_build");
17  if (m.attr("add")(1, 2).cast<int>() != 3) {
18  throw std::runtime_error("embed.cpp failed");
19  }
20 
21  py::module_::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp");
22  py::eval_file(test_py_file, py::globals());
23 }
Matrix3f m
tuple make_tuple()
Definition: cast.h:1209
PYBIND11_EMBEDDED_MODULE(test_cmake_build, m)
Definition: embed.cpp:4
object eval_file(str fname, object global=globals(), object local=object())
Definition: eval.h:112
int main(int argc, char *argv[])
Definition: embed.cpp:8
dict globals()
Definition: pybind11.h:1270
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:12