2

I've run into a tough "undefined reference" error when trying to build a module in Linux with C++. I'm going to describe it at a high level, and post code later if necessary (it's proprietary, so posting it would require some name changing). Some details:

  • Module A (a library) has a class that we'll call Foo with a method called Bar. Module A builds just fine, and using nm to look at the object file shows that the constructor and Bar are both defined (they show up as 'T').
  • Module B (a library) contains a class that uses module A, with references to Foo::Foo, Foo::~Foo, and Foo::Bar. Its makefile includes -L/path/to/Foo and -lFoo. This module also builds just fine. However, when I run nm on Module B's object file, the calls to module Foo::Foo, Foo::~Foo, and Foo::Bar are undefined (they show up as 'U'). Why it builds is beyond me.
  • Module C - whose output is an executable - contains references to module B. When I try to build Module C, then it yells at me for the undefined reference from Module B to Module A's Foo and Bar methods.

    1. Why does module B build if a reference is undefined?
    2. Why is the error only reported once we get to Module C?

EDIT:

  1. I should mention that Module C's makefile also has -L/path/to/Foo and -lFoo, but it still fails. Any high-level guesses as to anything I should try? I have a feeling I'm going to have to post some code...
automatom
  • 275
  • 2
  • 10
  • 1
    When you say "a library", do you mean a `.a` file or a `.so` file? – Robᵩ Jul 17 '12 at 16:56
  • Until you link something that actually calls an undefined reference you will not get the error. Chances are there are no instantiations of the the class that needs to link to the undefined methods until the executable uses it. – AJG85 Jul 17 '12 at 17:04
  • possible duplicate of [undefined reference to symbol even when nm indicates that this symbol is present in the shared library](http://stackoverflow.com/questions/10456581/undefined-reference-to-symbol-even-when-nm-indicates-that-this-symbol-is-present) – Robᵩ Jul 17 '12 at 19:46

3 Answers3

1

I figured out why it wasn't building. It's the same problem as was had here:

undefined reference to symbol even when nm indicates that this symbol is present in the shared library

Community
  • 1
  • 1
automatom
  • 275
  • 2
  • 10
0

Does module B build or just compile? Are you actually linking module B to module A before you try to build Module C? I would be surprised if you are. In the compiling stage, the compiler only checks that all names are declared. The compiler doesn't look for an definitions (i.e. implementations). The linking stage takes care of this detail. In the linking stage, you need to specify all modules needed to build the project (in this case A, B, and C.)

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Most probably *build*. Libraries don't need all symbols resolved to link. – David Rodríguez - dribeas Jul 17 '12 at 17:09
  • @DavidRodríguez-dribeas Hmm...then there is some confusion on my part (and perhaps on the part of the OP?). The OP calls Module A "a library" then later refers to "the object file". AFAIK, object files are the result of compiling not linking. – Code-Apprentice Jul 17 '12 at 22:54
0

When you create shared library, all the symbols do not need to be defined. This behaviour can be changed with compiler commandline switches(linker option --no-allow-shlib-undefined) if needed.

tp1
  • 1,197
  • 10
  • 17