2

I try to use a shared library written in C++ inside some python code. I'm under Linux OS.

Firstly I create the shared library using the following commands:

g++ -Wall -fPIC -O2 -c HeliosDacAPI.cpp
g++ -shared -o libHeliosDacAPI.so HeliosDacAPI.o

Then I try and load this library inside the python console:

>>> import ctypes
>>> HeliosLib = ctypes.cdll.LoadLibrary("./libHeliosDacAPI.so")

I get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/ctypes/__init__.py", line 425, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.5/ctypes/__init__.py", line 347, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./libHeliosDacAPI.so: undefined symbol: _ZN9HeliosDac18GetControlResponseEiPhi

Here is HeliosDacAPI.h

nm libHeliosDacAPI.so shows:

0000000000202088 B __bss_start
0000000000202088 b completed.7585
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000202090 B dacController
0000000000000cb0 t deregister_tm_clones
0000000000000d40 t __do_global_dtors_aux
0000000000201dd8 t __do_global_dtors_aux_fini_array_entry
0000000000202078 d __dso_handle
0000000000202080 d DW.ref.__gxx_personality_v0
0000000000201de8 d _DYNAMIC
0000000000202088 D _edata
00000000002020a0 B _end
00000000000014b8 T _fini
0000000000000d80 t frame_dummy
0000000000201dd0 t __frame_dummy_init_array_entry
0000000000001758 r __FRAME_END__
0000000000202000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000000014dc r __GNU_EH_FRAME_HDR
                 U __gxx_personality_v0@@CXXABI_1.3
0000000000000bb0 T _init
0000000000202098 B inited
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201de0 d __JCR_END__
0000000000201de0 d __JCR_LIST__
                 w _Jv_RegisterClasses
                 U __printf_chk@@GLIBC_2.3.4
0000000000000cf0 t register_tm_clones
                 U __stack_chk_fail@@GLIBC_2.4
0000000000202088 d __TMC_END__
                 U _Unwind_Resume@@GCC_3.0
0000000000001230 T _Z10SetShutterib
0000000000000db0 T _Z10WriteFrameiihP11HeliosPointi
0000000000001420 T _Z11OpenDevicesv
00000000000013d0 T _Z12CloseDevicesv
0000000000001360 T _Z13EraseFirmwarei
0000000000001290 T _Z18GetFirmwareVersioni
0000000000000f20 T _Z4Stopi
0000000000000f90 T _Z7GetNameiPc
00000000000010c0 T _Z7SetNameiPc
0000000000001150 T _Z9GetStatusi
                 U _ZdlPv@@GLIBCXX_3.4
                 U _ZN9HeliosDac11OpenDevicesEv
                 U _ZN9HeliosDac11SendControlEiPhi
                 U _ZN9HeliosDac18GetControlResponseEiPhi
                 U _ZN9HeliosDac9SendFrameEiPhi
                 U _ZN9HeliosDacC1Ev
                 U _ZN9HeliosDacD1Ev
                 U _Znwm@@GLIBCXX_3.4

I'm looking for some tips on how to debug such problem.

trojek
  • 3,078
  • 2
  • 29
  • 52

1 Answers1

5

I cannot be sure, but it seems that HeliosDac::GetControlResponse(int, ?*, int) is defined in HeliosDac.cpp which you do not compile and link into libHeliosDacAPI.so.

Indeed, from nm libHeliosDacAPI.so we clearly see that this symbol is used but Undefined (this implies that your problem doesn't come from your way to load the library from python):

                 U _ZN9HeliosDac18GetControlResponseEiPhi

You could try to compile with:

g++ -Wall -fPIC -O2 -c HeliosDacAPI.cpp
g++ -Wall -fPIC -O2 -c HeliosDac.cpp
g++ -shared -lusb -o libHeliosDacAPI.so HeliosDacAPI.o HeliosDac.o
YSC
  • 38,212
  • 9
  • 96
  • 149
  • Could you elaborate more on the actual problem, or link to some place with more information ? I'm having a related "undefined symbol" problem, but I can't find a way to generalize this answer. Posting this here because this page showed on top googling a variant of "python loading library undefined symbol". – Gloweye Aug 14 '18 at 12:49
  • @JaccovanDorp here you go: https://stackoverflow.com/q/12573816/5470596 – YSC Sep 27 '18 at 11:45
  • So, if I got it correct, there's either another shared object/DLL that you're missing, or the shared object/DLL was compiled incorrectly. Got it. – Gloweye Sep 27 '18 at 12:21