Hi, I download Pybind11 from github and I’d like to use it with juce, but how can I add it?
Up to now I’m leaving the pybind11 folder on mac download and so I added in projucer:
Under suggestions I included the string containing the pybind11 “include” path to my global “header search path” (not included python framework in “Extra Custom Frameworks”) and wrote in my “MainComponent” #include<pybind11/embed.h> it compiles.
To make a check and ensure that it works I wrote the following code before the MainComponent class:
namespace py = pybind11;
int add(int i, int j) { return i + j; }
PYBIND11_MODULE(MainComponent, m)
{
m.doc() = "MainComponent";
m.def("add", &add, "a function that add two numbers");
}
but now a warning stop me saying 'Python.h' file not found (warinigs referring line 209 with code 'Python.h' file not found in common.h, a header contained in detail folder of pybind11/include)… nightmare… I don’t want to bother you really, but these problems are too difficult for my skills to solve
try to add extraCompilerFlags="-I/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9"
also don’t forget to add the linker flags: extraLinkerFlags="-L/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/config-3.9-darwin -lpython3.9 -ldl -framework CoreFoundation" change it for your macOS version
Wow… I don’t know how to thank you! but there’s some place or doc that could explain me your suggestion? “For me it’s a kind of magic”
One Last thing I’ll no bother you again promised: I tested it calling this code inside my MainComponent constructor:
py::scoped_interpreter guard{};
py::module math = py::module::import("math");
py::object result = math.attr("sqrt")(25);
std::cout << "Sqrt of 25 is: " << result.cast<float>() << std::endl;
py::module example = py::module::import("SourceCode");
…and wow It works, but in my app I can call only python modules or there’s some way to make a dialogue between c++ files and py files? for example I write this code in my MainComponent.cpp:
int add(int i, int j) { return i + j; }
PYBIND11_MODULE(example, m)
{
m.doc() = "pybind11 example plugin";
m.def("add", &add, "A function which adds two numbers");
}
and I created in my source folder a file called “SourceCode.py” and in this one I wrote:
import example
print(printSomething())
then, again inside MainComponent constructor I add this code:
py::module example = py::module::import("SourceCode");
But now app crashes and in terminal I have this message: libc++abi: terminating with uncaught exception of type pybind11::error_already_set: ModuleNotFoundError: No module named ‘SourceCode’
terminating with uncaught exception of type pybind11::error_already_set: ModuleNotFoundError: No module named 'SourceCode’
Really thank you in advance, I was losing my hopes in achieve this