简体   繁体   中英

Is it possible to fix this in VScode? #pragma once in main file [-Wpragma-once-outside-header]

With VScode, how can this error be fixed?

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

Update: Showing in VScode:

在此处输入图像描述

Update Again: Here are my current VScode settings in 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
}

Given that there's no answer, and given that I also had some hard hours trying to fix this, here it goes.

In Visual Studio Code the compile settings are built by default in tasks.json ( Terminal > Configure Default Build Task > g++.exe () ). And in VS Code 2020 is this:

{
"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
        }
    }
]

}

(I am using mingw-64 to support gcc compiler so maybe "command" and "cwd" have different paths depending on the compiler you are using.)

The key part is this: "${file}", which is the name of the active file (active tab) in your Editor.

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

If you are working with several files, one header(.h or.hpp) and one main(.cpp) files at least, VS Code will take that active file (.h or.hpp) as it were the main file (.cpp). So you need to change it with this: "${workspaceFolder}\*.cpp" .

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

I fixed it by moving the.h/.hpp files to include folder.

Maybe there's a setting somewhere not to compile files in include.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM