5

使用 VScode,如何修复此错误?

#pragma once in main file [-Wpragma-once-outside-header]

更新: 在 VScode 中显示:

在此处输入图像描述

再次更新: 这是我当前的 VScode 设置c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Mac",
      "includePath": ["${workspaceFolder}/**"],
      "defines": [],
      "macFrameworkPath": [
        "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "clang-x64"
    }
  ],
  "version": 4
}
4

1 回答 1

1

鉴于没有答案,并且考虑到我也有一些艰难的时间试图解决这个问题,就这样吧。

在 Visual Studio Code 中,编译设置默认在tasks.json中构建(终端 > 配置默认构建任务 > g++.exe ())。在 VS Code 2020 中是这样的:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
        "args": [
            "-g",
            "${workspaceFolder}\\*.cpp",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

(我正在使用 mingw-64 来支持gcc编译器,因此“command”“cwd”可能具有不同的路径,具体取决于您使用的编译器。)

关键部分是:“ ${file}”,它是编辑器中活动文件(活动选项卡)的名称。

"args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],

如果您正在处理多个文件,至少一个头文件(.h 或 .hpp)和一个主文件(.cpp),VS Code 将使用该活动文件(.h 或 .hpp),因为它是主文件(.h 或 .hpp)。 cp)。所以你需要用这个来改变它:"${workspaceFolder}\*.cpp"

"args": [
            "-g",
            "${workspaceFolder}\\*.cpp",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
于 2020-04-04T03:14:41.487 回答