Nm reports symbol is defined but ldd reports symbol is undefined

Asked
Active3 hr before
Viewed126 times

6 Answers

definedundefinedsymbol
90%

When you build your shared library you need to resolve all undefined symbols from either within the same library or another (shared) library. The linker will not resolve an undefined symbol from a library with a symbol from your application., Meta Stack Overflow ,Stack Overflow en español, 2 You are quite mistaken: the runtime loader will happily resolve undefined symbol from a shared libary with a symbol from the main executable, provided that symbol is "exported" in its dynamic table (which happens e.g. when the executable is linked with -rdynamic). – Employed Russian Jun 3 '09 at 5:06

I compile and link everything together but at runtime I get the error

/home/bar / src / libfoo.so: undefined symbol: sread.
load more v
88%

but ldd reports the symbol is undefined ,nm reports the symbol is defined ,More details see this post,Here is what the output should look like:

I compile and link everything together but at runtime I get the error

/home/bar / src / libfoo.so: undefined symbol: sread.

nm reports the symbol is defined

$nm baz | grep sread
00000000000022 f8 t sread

but ldd reports the symbol is undefined

$ldd - r baz | grep sread
undefined symbol: sread(/home/bar / src / libfoo.so)
load more v
72%

I'm having a linking problem. I need to link against a shared library libfoo.so that depends on a function read which I would like to define myself in the file read.c.,When you build your shared library you need to resolve all undefined symbols from either within the same library or another (shared) library. The linker will not resolve an undefined symbol from a library with a symbol from your application.,First, defining a function called 'read' is a bad idea(TM), because it is a standard libc function on all UNIXen. The behavior of your program is undefined when you do this.,Second, the read function you defined in libbaz.so is marked with a 't' in nm output. This means that this function is local (not visible outside libbaz.so). Global functions are marked with 'T' by nm.

I compile and link everything together but at runtime I get the error

/home/bar / src / libfoo.so: undefined symbol: sread.
load more v
65%

There are indeed some more or less good reasons to allow shared objects to have undefined symbols, and here come a few:,Now, what nm shows as undefined (U code) is not really what we’re concerned about; for object files (.o, just intermediate) will report undefined symbols for any function or data element used that is not in the same translation unit; most of those get resolved at the time all the object files get linked in to form a final shared object or executable — actually, it’s a lot more complex than this, but since I don’t care about describing here symbolic resolution, please accept it like it was true.,Thank you very much for this, I found it really usefull, was stuck with a software i’ve inherited, that compiled succesfully, but threw a bunch of linker errors while running.Using the –no-undefined flag helped me debug the error.,I have explicitly avoided using the fprintf function, mostly because that would require a further undefined symbol, so…

The remaining symbols will be keeping the U code in the shared object or executable, but most of them won’t concern us: they will be loaded from the linked libraries, when the dynamic loader actually resolve them. So for instance, the executable built from the following source code, will have the printf symbol “undefined” (for nm), but it’ll be resolved by the dynamic linker just fine:

int main() {
   printf("Hello, world!");
}
75%

The symbol is an indirect reference to another symbol. ,Do not bother to sort the symbols in any order; print them in the order encountered. ,Precede each symbol by the name of the input file (or archive member) in which it was found, rather than identifying the input file once only, before all of its symbols. ,Display the dynamic symbols rather than the normal symbols. This is only meaningful for dynamic objects, such as certain types of shared libraries.

nm[-A | -o | --print - file - name]
   [-a | --debug - syms]
   [-B | --format = bsd]
   [-C | --demangle[ = style]]
   [-D | --dynamic]
   [-fformat | --format = format]
   [-g | --extern - only]
   [-h | --help]
   [--ifunc - chars = CHARS]
   [-j | --format = just - symbols]
   [-l | --line - numbers][--inlines]
   [-n | -v | --numeric - sort]
   [-P | --portability]
   [-p | --no - sort]
   [-r | --reverse - sort]
   [-S | --print - size]
   [-s | --print - armap]
   [-t radix | --radix = radix]
   [-u | --undefined - only]
   [-V | --version]
   [-X 32_64]
   [--defined - only]
   [--no - demangle]
   [--no - recurse - limit | --recurse - limit]]
[--plugin name]
[--size - sort]
[--special - syms]
[--synthetic]
[--target = bfdname]
[--with - symbol - versions]
[--without - symbol - versions]
[objfile…]
40%

The source for the library can declare a symbol without defining it, by doing something like this, Search for InvalidateImage .The other characters are due to C++ name mangling. – Mark Plotnick Mar 31 '15 at 8:05 , Is there any kind of biological mechanism that would allow a species with a naturally short lifespan to live longer through cannibalism? ,For what it's worth, I use scripts (exports and externs) to check for missing symbols in the programs which I maintain.

The source for the library can declare a symbol without defining it, by doing something like this

extern InvalidateImage(const char * );
load more v

Other "defined-undefined" queries related to "Nm reports symbol is defined but ldd reports symbol is undefined"