内核交叉编译笔记:yylloc重复定义(GCC 10)

使用安装了10或更高版本GCC的主机进行内核交叉编译,编译到设备树parser时ld报错yylloc重复定义:

...
  LEX     scripts/dtc/dtc-lexer.lex.c
  YACC    scripts/dtc/dtc-parser.tab[ch]
  HOSTCC  scripts/dtc/dtc-lexer.lex.o
  HOSTCC  scripts/dtc/dtc-parser.tab.o
  HOSTLD  scripts/dtc/dtc
/usr/lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/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
collect2: error: ld returned 1 exit status
make[1]: *** [scripts/Makefile.host:116: scripts/dtc/dtc] Error 1
make: *** [Makefile:1263: scripts_dtc] Error 2

解决方案

编辑scripts/dtc/dtc-lexer.lex.c,添加extern关键字:

- YYLTYPE yylloc;
+ extern YYLTYPE yylloc;

软件环境

项目
主机发行版 OpenSUSE Tumbleweed 20220110-0
主机编译器 gcc (SUSE Linux) 11.2.1 20220103 [revision d4a1d3c4b377f1d4acb34fe1b55b5088a3f293f6]
目标架构 arm64
交叉编译工具链 gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu