How to List Functions in a Linux Static Library
The nm command displays symbolic information from object files, libraries, and executables. It’s the standard tool for inspecting what functions and variables are defined or referenced in a static library (.a file).
Basic usage
List all symbols in a library:
nm lib.a
This outputs symbol names, but the default format can be hard to parse. You’ll see output like:
archive.a:file.o:
0000000000000000 T function_name
U external_function
0000000000001234 D global_variable
The letters indicate the symbol type:
T— function or data in the text (code) sectionD— initialized global data- B` — uninitialized global data (BSS section)
U— undefined symbol (external reference)a— absolute symbolR— read-only data
More useful formats
Display symbols with cleaner output:
nm -D lib.a
However, -D works better with shared objects. For static libraries, use:
nm --defined-only lib.a
This shows only defined symbols, filtering out external references.
For a more readable format with symbol types clearly labeled:
nm -B lib.a
The -B flag uses BSD-style output (equivalent to --format=bsd).
Filtering symbols
Show only functions (text symbols):
nm lib.a | grep ' T '
Show only undefined symbols (external dependencies):
nm lib.a | grep ' U '
Show symbols sorted by size:
nm -S lib.a | sort -k2 -n
Extracting function names only
If you just need clean function names without addresses or metadata:
nm lib.a | grep ' T ' | awk '{print $3}'
Or using a more modern approach with nm‘s output formats:
nm --format=posix lib.a | grep ' T ' | cut -d' ' -f1
Checking multiple libraries
Inspect all .a files in a directory:
for lib in *.a; do
echo "=== $lib ==="
nm "$lib" | grep ' T '
done
Integration with build systems
When debugging linking issues, check what symbols are exported:
nm -g lib.a
The -g flag shows only global symbols, which is useful when a library has many static (file-scope) symbols you don’t care about.
For detailed information on symbol versions or relocations, use:
nm -l lib.a
This includes source filenames and line numbers where available.
Related tools
For shared objects (.so files), nm still works but objdump provides more detail:
objdump -t lib.so
For a tree view of dependencies between object files in a library:
ar t lib.a
This lists the object files contained in the archive, which you can then inspect individually with nm.
Troubleshooting Common Issues
When encountering problems on Linux systems, follow a systematic approach. Check system logs first using journalctl for systemd-based distributions. Verify service status with systemctl before attempting restarts. For network issues, use ip addr and ss -tulpn to diagnose connectivity problems.
Package management issues often stem from stale caches. Run dnf clean all on Fedora or apt clean on Ubuntu before retrying failed installations. If a package has unmet dependencies, try resolving them with dnf autoremove or apt autoremove.
Related System Commands
These commands are frequently used alongside the tools discussed in this article:
- systemctl status service-name – Check if a service is running
- journalctl -u service-name -f – Follow service logs in real time
- rpm -qi package-name – Query installed package information
- dnf history – View package transaction history
- top or htop – Monitor system resource usage
Quick Verification
After applying the changes described above, verify that everything works as expected. Run the relevant commands to confirm the new configuration is active. Check system logs for any errors or warnings that might indicate problems. If something does not work as expected, review the steps carefully and consult the official documentation for your specific version.
