Pybind11 – ImportError: …/pybindx.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN6google8protobuf8internal26fixed_address_empty_stringB5cxx11E

Question:

I wrote some binding code to bind C++ code with python in pybindx.cpp file. I want to call some functions (implemented in C++) using python. When I use python setup.py build_ext command, the .so file ./build/lib.linux-x86_64-3.8/pybindx.cpython-38-x86_64-linux-gnu.so is getting created, but when I try to import(import pybindx) in test.py to call binded functions, It gives the following error:

ImportError: <path-to-.so-file>/pybindx.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN6google8protobuf8internal26fixed_address_empty_stringB5cxx11E

I have added <path-to-.so-file> to PYTHONPATH and LD_LIBRARY_PATH.

My setup.py file contains following code:

import os, sys
from distutils.core import setup, Extension
from distutils import sysconfig

cpp_args = ['-std=c++11']

ext_modules = [
    Extension(
        'pybindx',
        ['class1.cpp', 'class2.cpp', 'base_class1.cpp', 'base_class2.cpp', 'pybindx.cpp'],
        include_dirs=['paths/to/include/header/files', 'path/to/protobuf/include'],
        language='c++',
        extra_compile_args = cpp_args,
    ),
]
setup(
    name='pybindx',
    version='0.0.1',
    author='xxxxx',
    author_email='xxxxx',
    description='desc',
    ext_modules=ext_modules,
)

Where, class1.cpp, class2.cpp, base_class1.cpp, base_class2.cpp are the files having implementation of classes and functions which I want to bind with python.

I am new to pybind11, can someone help me with this?
Thanks!

I tried writing small example code without protobuf, where I am able to call the C++ function using test.py, but here I want to use protobuf.

Asked By: Irrepressible_sa1

||

Answers:

Anyways, fixed it. I Used Makefile rather than setup.py. I have to link static libprotobuf.a with my pybindx.so.
Added following command with other required commands in Makefile:

g++ -shared -fPIC ./build/*.o <path-to-static-protobuf>/libprotobuf.a -o pybindx.so

Where, *.o are the object files created using some command(s) in Makefile.

Answered By: Irrepressible_sa1