'pybind11 - ImportError: undefined symbol: _Py_ZeroStruct

I'm following the pybind11 documentation and trying to create Python bindings for a simple function Creating bindings for a simple function, but after compiling my C++ code with the following command:

g++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cc -o example$(python3-config --extension-suffix)

when I try to import the library in python using:

import example

I get an ImportError saying undefined symbol: _Py_ZeroStruct

I'm using:

  • Ubuntu : Ubuntu 20.04.4 LTS
  • Python : Python 3.8.10
  • Pip : Pip 20.0.2

What I tried to solve the issue

I looked into many articles related to the same error but couldn't really get an understanding of how I'm going to resolve my issue:

Any kind of help will be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source