143

So I've been trying to get dynamic libraries to work in my XCode project under Mac OS X. So far no joy.

I am able to load the dylib file, but when I call dlsym to get the function pointer, it returns 0 and dlerror says symbol not found.

So I am wondering if there is a simple way to list the symbols that are exported from a dylib file. Any ideas would be great.

bdesham
  • 15,430
  • 13
  • 79
  • 123
Gerald
  • 23,011
  • 10
  • 73
  • 102

4 Answers4

184

man 1 nm

https://web.archive.org/web/20160316222941/https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/nm.1.html

For example:

nm -gU /usr/local/Cellar/cairo/1.12.16/lib/cairo/libcairo-trace.0.dylib
Denis Murphy
  • 1,137
  • 1
  • 11
  • 21
MK.
  • 33,605
  • 18
  • 74
  • 111
  • 2
    Under OS X Leopard (10.5, yeah, I know...) you should use `nm -gu` , i.e. the `-u` option is lowercase. – András Aszódi Apr 23 '15 at 08:17
  • 4
    man 1 nm is self-contained and will continue working when command line changes :p – MK. Oct 16 '15 at 12:33
  • 11
    @MK. `man 1 nm` lists a lot of things one can do with nm.  Again, it's an external resource one can read through to discover a solution, but not a solution itself to the requested _“a simple way to list the symbols that are exported from a dylib file”_.  “`nm -gU ….dylib`” is, however. – Slipp D. Thompson Apr 27 '16 at 04:32
  • How about for macOS frameworks:`/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa` – vaughan May 18 '23 at 14:59
66

Use otool:

otool -TV your.dylib

OR

nm -g your.dylib
linuxbuild
  • 15,843
  • 6
  • 60
  • 87
  • 5
    On new macOS, otool -T will show `otool: -T functionality obsolete` use objdump -t – joseph.smeng Mar 03 '17 at 13:36
  • While it's true that `otool -T` reports `functionality obsolete` in newer OS X, it's also true that (for dynamic libraries) `objdump -t` will report `The file was not recognized as a valid object file.` – TML Jul 25 '17 at 19:33
16

Use nm -a your.dylib

It will print all the symbols including globals

Omkar Ramtekkar
  • 161
  • 1
  • 2
0

Use Mach-OView for viewing all the Symbols in dylib

https://sourceforge.net/projects/machoview/

Sahil Doshi
  • 1,073
  • 10
  • 23
  • I was excited to have learned of this GUI but it crashed for me after I loaded a dylib. I'm using macOS Mojave. – chrisdembia Jun 19 '19 at 16:43