Qt_logo_neon_2022.svg.png今天给客户提供技术支持的时候遇上了一个问题,客户说遇上了单个源文件太大,链接器报错的问题。
拿过来代码一看,好家伙,一个cpp文件有十几万行代码。然后客户提供了信息说在MSVC上没有这个问题。

报错大概是这样的

C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: debug\a2lParser.o: too many sections (48293)
C:\Users\CHIPTI~1\AppData\Local\Temp\ccB3KbAA.s: Assembler messages:
C:\Users\CHIPTI~1\AppData\Local\Temp\ccB3KbAA.s: Fatal error: can't write 36 bytes to section .text of debug\a2lParser.o: 'File too big'
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: debug\a2lParser.o: too many sections (48293)
C:\Users\CHIPTI~1\AppData\Local\Temp\ccB3KbAA.s: Fatal error: can't close debug\a2lParser.o: File too big
mingw32-make[1]: *** [Makefile.Debug:1377: debug/a2lParser.o] Error 1
mingw32-make[1]: Leaving directory 'D:/Workspace/build-MyXcpLib-Desktop_Qt_5_15_2_MinGW_64_bit-Debug'
mingw32-make: *** [Makefile:45: debug] Error 2
21:42:59: 进程"C:\Qt\Tools\mingw810_64\bin\mingw32-make.exe"退出,退出代码 2 。
Error while building/deploying project MyXcpLib (kit: Desktop Qt 5.15.2 MinGW 64-bit)
When executing step "Make"

然后定位到这两行输出:a2lParser.o: File too bigFatal error: can't write 36 bytes to section .text of debug\a2lParser.o: 'File too big'

分析的结论是汇编器、链接器无法处理这么大的文件。
其实处理方法也很简单,增加一个bigobj的编译器选项即可

-Wa,-mbig-obj

注意这是个完整的编译器选项开关,不能单独写个-Wa或者-mbig-obj
对于QMake可以这么写

QMAKE_CXXFLAGS += -Wa,-mbig-obj

参考链接

https://stackoverflow.com/questions/63369666/how-to-set-bigobj-option-when-compiling-c-code-in-visual-studio-code
https://github.com/google/googletest/issues/1841
https://digitalkarabela.com/mingw-w64-how-to-fix-file-too-big-too-many-sections/
https://www.cerberus-x.com/community/index.php?threads/file-too-big-in-debug-mode.360/