-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (36 loc) · 1.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <pybind11/embed.h> // pybind11 header for embedding Python
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <iostream>
#include <string>
#include <vector>
namespace py = pybind11;
int add(int a, int b) {
return a + b;
}
std::vector<int> generate_list(int size) {
std::vector<int> list;
for (int i = 0; i < size; i++) {
list.push_back(i);
}
return list;
}
PYBIND11_EMBEDDED_MODULE(ex, m) {
m.def("add", &add, "A function that adds two numbers");
m.def("generate_list", &generate_list, "A function that generates a list");
}
int main(int argc, char** argv) {
if (argc < 3 || std::string(argv[1]) != "--py") {
std::cerr << "Usage: " << argv[0] << " --py <script.py>" << std::endl;
return 1;
}
std::string script = argv[2];
try {
py::scoped_interpreter guard{};
py::eval_file(script);
} catch (const std::exception &e) {
std::cerr << "Error executing script: " << e.what() << std::endl;
return 1;
}
return 0;
}