2

I have a large Qt project under Ubuntu. Just found that G++ lets me compile AND link code where I'm calling an declared but undefined method. It crashes at runtime at that call.

I couldn't reproduce this behavior with a test project, although I enforced the same g++ command line.

The questions are: why does it let me do that? How can I make the linker generate an error?

Edits (based on the comments):

  1. I know it's not optimized away, as it crashes at runtime when I call that method.

  2. I declared and called another identical method with a dummy name - I think something along the lines of gfdsgfdhgasfdhgfa() will do :) - same thing.

  3. The app crashes when the undefined method is called. Sorry for missing this important detail.

  4. The undefined method is not a slot.

  5. Yes, I'm clearing the build dir. I'm using qmake.

  6. Just found there's an utility called nm. If I'm running it with the -u (show undefined only) option on the output .so I can see this method in the list. Why is GCC assuming it's external?

user3671607
  • 328
  • 1
  • 11

1 Answers1

0

It looks like by default GCC (not only G++) assumes all undefined symbols are externals. Visual Studio doesn't.

Relevant question: Force GCC to notify about undefined references in shared libraries

--allow-shlib-undefined
   --no-allow-shlib-undefined
       Allows  (the  default)  or  disallows  undefined  symbols  in  shared
       libraries (It is meant, in shared libraries _linked_against_, not the
       one we're creating!--Pavel Shved). This switch is similar to --no-un-
       defined except  that it determines  the  behaviour when the undefined
       symbols are in a shared library rather than a regular object file. It
       does not  affect  how  undefined  symbols in regular object files are
       handled.

       The  reason  that  --allow-shlib-undefined is the default is that the
       shared library being specified at link time may not be  the  same  as
       the one that is available at load time, so the symbols might actually
       be resolvable at load time.  Plus there are some systems,  (eg  BeOS)
       where  undefined  symbols in shared libraries is normal.  (The kernel
       patches them at load time to select which function is most  appropri-
       ate for the current architecture.  This is used for example to dynam-
       ically select an appropriate memset function).  Apparently it is also
       normal for HPPA shared libraries to have undefined symbols.
user3671607
  • 328
  • 1
  • 11