Resolving Linker Errors: Multiple Definition of <span>yylloc</span>
When compiling the Device Tree Compiler (DTC) tool, developers often encounter the following linker error:
/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x20): multiple definition of `yylloc';
scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here
This error indicates that the linker has found a duplicate definition of the global variable <span>yylloc</span> in <span>dtc-parser.tab.o</span> and <span>dtc-lexer.lex.o</span>. This article will analyze the reasons behind this issue and provide solutions.
🔍 Error Analysis
-
Variable Definition Conflict:
<span>yylloc</span>is a global variable shared by Bison (the<span>.y</span>file parser) and Flex (the<span>.l</span>file lexer) used to store lexical analysis location information.- When both Bison-generated
<span>dtc-parser.tab.c</span>and Flex-generated<span>dtc-lexer.lex.c</span>define this variable, a conflict occurs during the linking stage.
Missing Header File Declaration:
- In standard Bison/Flex collaboration,
<span>yylloc</span>should be defined in the Bison-generated parser (via<span>%locations</span>or<span>%define api.location.type</span>directives). - The Flex-generated lexer should declare
<span>extern YYLTYPE yylloc;</span>by including<span>#include "dtc-parser.tab.h"</span>.
Generated Files Not Properly Synchronized:
- If the Flex file does not include the Bison-generated header file, or if Bison is not configured to handle location information, Flex may generate its own definition of
<span>yylloc</span>.
🛠️ Solutions
Method 1: Modify the Lexer (Recommended)
// File: scripts/dtc/dtc-lexer.lex.c
extern YYLTYPE yylloc; // Add extern declaration
Steps:
- Open
<span>scripts/dtc/dtc-lexer.lex.c</span> - Add
<span>extern YYLTYPE yylloc;</span>in the global variable area - Save and recompile
Method 2: Ensure Header File Inclusion
Include the Bison-generated header file at the beginning of the Flex file (<span>.l</span>):
%{
#include "dtc-parser.tab.h" // Explicitly include parser header file
%}
Method 3: Configure Bison to Generate Location Support
Add configuration directives in the Bison file (<span>.y</span>):
%locations // Enable location tracking
%define api.location.type {YYLTYPE} // Explicitly define location type
✅ Verification and Considerations
- Clean Build:
make clean && make - Regenerate Files:
- After modifying
<span>.y</span>or<span>.l</span>files, re-run Bison/Flex.
- Ensure compatibility of Bison (≥3.0) and Flex (≥2.6) versions.
Permanent Fix Suggestion: Automatically patch generated files in the project build script using
<span>sed</span><code><span>:</span>sed -i 's/YYLTYPE yylloc;/extern YYLTYPE yylloc;/' scripts/dtc/dtc-lexer.lex.c
📚 Summary
| Key Points | Description |
|---|---|
| Nature of the Error | Duplicate definition of global variable |
| Root Cause | Improper collaboration between Flex and Bison generated files |
| Solution | Add <span>extern</span> declaration or include header files |
| Preventive Measures | Explicitly configure Bison location support |
By explicitly declaring <span>yylloc</span> as an external variable, linker conflicts can be eliminated. Understanding the collaboration mechanism between Bison and Flex can effectively prevent such issues and ensure a smooth compilation process.