Solved! - Please check the answer.
I wrote a library where headers and python bindings are auto-generated. For example dummy_bind.cpp
for dummy_message.h
and each _bind.cpp
file has PYBIND11_MODULE
call in it for their specific class. There are dozens of other _bind.cpp files for other headers. What should be the module name for each file when calling the PYBIND11_MODULE
like:
PYBIND11_MODULE(protocol_name, m)
{
/// …
}
If I use protocol_name
in each PYBIND11_MODULE(protocol_name, m)
call, when compiling I get multiple definition error like: multiple definition of PyInit_protocol_name
. If I generate special module name for each message like PYBIND11_MODULE(protocol_name_dummy, m)
the extension is compiled but I think I need to import each module one by one which is not viable.
Should I do all exports inside a single PYBIND11_MODULE
call? Thanks in advance.
I've actually solved this by generating proxy functions in _bind.cpp
files. For instance, in message1_bind.cpp
I've defined a function void init_message1(pyinit11::module& m)
and then in main_bind.cpp
I call them all inside PYBIND11_MODULE(protocol_name, m)
so I only have only one PYBIND11_MODULE()
call. Here's a minimal example to describe the method better:
message1_bind.cpp
:
#include <pybind11/pybind11.h>
#include "messages/message_1.h"
void init_message1(pybind11::module& m)
{
pybind11::class_<protocol_name::Message1>(m, "Message1").def(pybind11::init<>());
m.def("serialize_message1", &protocol_name::serialize_message_1);
m.def("deserialize_message1", &protocol_name::deserialize_message_1);
}
And then inside main_bind.cpp
#include <pybind11/pybind11.h>
#include "message1_bind.cpp"
/// ... all other includes
PYBIND11_MODULE(protocol_name, m)
{
init_message1(m);
/// init_message2(m) and so on..
}