Hello,
I realize this is the first point on the FAQ. I can compile, import, and execute the add example with no problems with the following command:
c++ -Wall -O3 -fPIC -shared -std=c++11 -I $HOME/anaconda3/include/python3.6m -I $HOME/anaconda3/include/python3.6m/pybind11 python3.6m-config --libs -L $HOME/anaconda3/lib example.cpp -o example.so
I explicitly link to the anaconda folder and the pybind11 folder in my anaconda directory for python 3.6.
When I use the same command to compile the Pet object oriented example:
c++ -Wall -O3 -fPIC -shared -std=c++11 -I $HOME/anaconda3/include/python3.6m -I $HOME/anaconda3/include/python3.6m/pybind11 python3.6m-config --libs -L $HOME/anaconda3/lib Pet.cpp -o Pet.so
I get the following error:
Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import Pet
Traceback (most recent call last):
File "", line 1, in
ImportError: dynamic module does not define module export function (PyInit_Pet)
Why does this happen for the object oriented example, but not the function?
#include <pybind11.h>
#include <string>
namespace py = pybind11;
class Pet
{
public:
std::string name;
Pet(const std::string &name) : name(name) { }
void setName(const std::string &name_) { name = name_; }
const std::string &getName() const { return name; }
};
PYBIND11_MODULE(example, m)
{
py::class_<Pet>(m,"Pet")
.def(py::init<const std::string &>())
.def("setName", &Pet::setName)
.def("getName", &Pet::getName);
}
PYBIND11_MODULE(example, m)
^ This should be PYBIND11_MODULE(Pet, m) for Pet.cpp/Pet.so.
That did the trick. Thanks!
Most helpful comment
^ This should be
PYBIND11_MODULE(Pet, m)for Pet.cpp/Pet.so.