在 Visual Studio Code 中编译 C++ 代码时如何设置 bigobj 选项?

Dav*_*pin 2 c++ gcc mingw g++ visual-studio-code

我的问题: 我正在用 C++ 编写一个国际象棋引擎。编写国际象棋引擎的一部分是处理非常大的数字(可以想象高达 2^63)。我有一个正在为我的项目运行单元测试的文件,当我尝试运行构建任务以将其编译为可执行文件时,出现以下错误:

C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: too many sections (32782)
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Assembler messages:
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Fatal error: can't write 293 bytes to section .text of C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: 'File too big'
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: too many sections (32782)
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Fatal error: can't close C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: File too big
The terminal process "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g c:\Users\chopi\Desktop\chess-engine\maestro-tests\main.cpp -o c:\Users\chopi\Desktop\chess-engine\maestro-tests\main.exe" terminated with exit code: 1.
Run Code Online (Sandbox Code Playgroud)

基本上,章节太多了。因此,我去寻找答案,发现很多地方解释了如何配置bigobjVisual Studio,但没有解释如何配置Visual Studio Code

我尝试过的: 它应该像编译时传递/bigobj-bigobj或两者-Wa作为-mbig-obj选项一样简单。因此,我尝试了一些应该但没有的方法,在启用大对象的情况下编译我的代码。这就是我的样子tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

但我也尝试过:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "-bigobj",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "-Wa",
                "-mbig-obj",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

...等等。

这些配置会产生以下错误g++.exe: error: unrecognized command line option '-bigobj'

g++.exe: error: unrecognized command line option '-Wa'; did you mean '-W'?
g++.exe: error: unrecognized command line option '-mbig-obj'
Run Code Online (Sandbox Code Playgroud)

, 分别。

我想知道的是: 那么,有没有办法可以解决Visual Studio Code"too many sections"中的此错误?或者我必须硬着头皮为Visual Studio配置一切?

Dav*_*pin 5

正如 user4581301 所建议的,我必须不将-Wa-mbig-obj作为两个独立的选项分开。相反,我不得不将它们保留为一种选择:-Wa,-mbig-obj. 这遇到了一个错误,但是,根据这个答案,添加--%作为第一个参数,再加上上述有关选项的建议,让我的代码最终编译为可执行文件。这是tasks.json更改后的样子:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "--%",
                "-g",
                "-Wa,-mbig-obj",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)