Troubleshooting Techniques
1. Nm Command: Symbol Inspection
## List symbols in object files
nm program.o
nm -C libexample.so ## Demangle C++ symbols
2. Ldd Command: Library Dependencies
## Check library dependencies
ldd ./executable
Symbol Resolution Workflow
graph TD
A[Compilation] --> B[Generate Object Files]
B --> C[Linker Analysis]
C --> D{Symbol Resolution}
D -->|Success| E[Executable Created]
D -->|Failure| F[Error Diagnosis]
Advanced Debugging Techniques
Linker Verbose Mode
Flag |
Purpose |
Example |
-v |
Detailed linking information |
gcc -v main.c |
--verbose |
Comprehensive linker output |
ld --verbose |
Debugging Flags
## Compilation with debug symbols
gcc -g program.c -o program
Common Troubleshooting Scenarios
Undefined Reference Resolution
// header.h
#ifndef HEADER_H
#define HEADER_H
int calculate(int a, int b);
#endif
// implementation.c
#include "header.h"
int calculate(int a, int b) {
return a + b;
}
// main.c
#include "header.h"
int main() {
int result = calculate(5, 3);
return 0;
}
Compilation Command
## Correct linking order matters
gcc main.c implementation.c -o program
Tool |
Function |
Usage |
strace |
System call tracing |
strace ./program |
ltrace |
Library call tracing |
ltrace ./program |
objdump |
Object file analysis |
objdump -T libexample.so |
Linker Script Customization
## Custom linker script
ld -T custom_linker.ld input.o -o output
Memory and Symbol Analysis
Valgrind for Comprehensive Checking
## Memory and symbol validation
valgrind ./program
Best Practices
- Always compile with warning flags
- Use
-Wall -Wextra
for comprehensive checks
- Understand library dependencies
- Verify symbol visibility
LabEx Insights
At LabEx, we recommend a systematic approach to symbol troubleshooting, combining theoretical knowledge with practical debugging techniques.
Advanced Techniques
Symbol Interposition
// Override standard library functions
int puts(const char *str) {
// Custom implementation
}
Weak Symbol Handling
__attribute__((weak)) void optional_function() {
// Optional implementation
}