using catkin_make to build a shared c++ lib for python3

asked 2022-09-06 04:05:45 -0500

mysqo gravatar image

updated 2022-09-06 04:08:55 -0500

I have a library written in c++ that I would like to use in one of my stand-alone python3 scripts. The c++ library is compiled using catkin_make, and the output is a .so lib under workspace/devel/lib/libmathlib.so. As a compile option I have added the -fpic:

add_compile_options(-std=c++17 -fpic)
add_library(mathlib src/math_utils.cpp)
target_link_libraries(mathlib ${catkin_LIBRARIES})

In the library I have an example of a function defined:

extern "C"
{
    void MathLib::print() 
    {
        std::cout << "Hello world" << std::endl;
    }
}

And in my python3 script, I would like to call this print function:

#!/usr/bin/env python3
import ctypes
mathlib = ctypes.cdll.LoadLibrary('workspace/devel/lib/libmathlib.so')
mathlib.print()

But I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ctypes/__init__.py", line 361, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python3.6/ctypes/__init__.py", line 366, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: workspace/devel/lib/libmathlib.so: undefined symbol: print
edit retag flag offensive close merge delete