0

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.

  • It looks like your code needs protobuf. So far you've only added include_dir, so it compiles. But you need to add library_dirs and libraries to `Extension` arguments to specify where the protobuf lib/so are. – 0x26res Feb 02 '23 at 11:54
  • @0x26res I tried adding library_dirs=['path/to/protobuf.so'] and libraries=['path/to/libs'] or ['path/to/protobuf.so'], but getting the same error. I hope this is the correct way to add library_dirs and libraries. What else can be the issue? – Irrepressible_sa1 Feb 06 '23 at 05:22
  • You will also need to add something like `extra_link_args=['-lprotobuf']`. See [this](https://stackoverflow.com/questions/11207053/linking-protobuf-library-with-code-google-protocol-buffers) – ChrisD Feb 06 '23 at 06:55
  • library_dirs should be `["path/to/"] (directories) and libraries should be `["protobuf"]` (the name of the library file without the `lib` prefix or `.so` extension – 0x26res Feb 06 '23 at 07:53
  • 1. library_dirs=['path/to/protobuf/dir'], libraries=['protobuf'] \\ 2. extra_link_args=['-lprotobuf'] \\ I tried 1. and 2. both individually and combined also, Nothing is working. – Irrepressible_sa1 Feb 06 '23 at 11:14

1 Answers1

0

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.