[PyQt] Simple C++ example has undefined symbol

Jens Thoms Toerring jt at toerring.de
Sat Jan 21 20:51:59 GMT 2012


Hi Gary,

On Sat, Jan 21, 2012 at 08:47:21AM -0500, Gary Fisher wrote:
> So, I wrote a little C++ class called word and provided a method
> called reverse which does nothing but "return (static char *)Null".

What does "return (static char *)Null" mean? There's no 'Null'
in C++ and casting to 'static char pointer also looks rather
strange (and shouldn't compile since you can't cast to some-
thing that's 'static'). Do you simply want to return a char
(NULL) pointer? Then

  return 0;

will be all you need (assuming that the function is defined
to return a char pointer.

> I then ran "gcc -o libword.so -shared word.cpp" to make my little
> fictional library (as referred to in the text).

If you compile C++ code you should use g++ instead of gcc.

> I then made the word.sip and configure.py files and ran configure.py.
> Prior to running make I copied libword.so into /usr/lib so the linker would
> find it.  Running make and 'make install' ran flawless, as did everything
> until now.

Do you have anything in configure.py that would tell the make
process that the resulting 'word.so' library will need the
'libword.so' library, for example a line with

makefile.extra_libs = [ 'word' ]

That will result in the resulting Makefile having '-lword'
as linker option.

> At this point, if I understand the theory here, I figured I'd start up a
> Python command line session and issue the command "import word" and
> then try "word.reverse()".  To my surprise I got the following error:
> 
> >>> import word
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: ./word.so: undefined symbol: _ZNK4Word7reverseEv

So 'word.so' is the wrapper library for your original library
written in C++ (which seems to be 'libword.so'), right? Looks
to me as if when was 'word.so' created the linker wasn't aware
that 'libword.so' would be needed when 'word.so' is used.
'word.so' is also a library, so the linker will not complain
when symbols aren't found while 'word.so' is created since
they're then expected to be found somewhere when the library is
loaded.

I would try

ldd word.so

If 'libword.so' isn't listed as one of the libraries 'word.so'
depends on then I would see this as a strong indication that
my guess isn't completely off the mark.

> I've tried numerous incantations with no luck.  I figured this would work
> without my having to go and learn all about PyQT just to get it to run.

I don't think it's a problem about PyQt but just of the way
'word.so' was created.

> The command "Readelf -s word.so" shows the symbol is listed in the
> library.

I would use 'nm' for such checks (if you run it with -C it
will also demangle the strange names the symbols of C++ code
have). Check the letter before the name of the function - if
it's a 'T' everything is fine (i.e. the symbol is defined in
that library) but if it's a 'U' the symbol is undefined and
thus must be defined somewhere else.

                               Regards, Jens
-- 
  \   Jens Thoms Toerring  ________      jt at toerring.de
   \_______________________________      http://toerring.de


More information about the PyQt mailing list