7

With VScode, how can this error be fixed?

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

Update: Showing in VScode:

enter image description here

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
}
Chance Smith
  • 1,211
  • 1
  • 15
  • 32
  • 5
    I actually suspect that you are compiling your header file. Typically when a warning pops up you should fix your code rather than disable that warning. Also note that it is not necessary a compilation warning. For compilation warnings you should post a build log. – user7860670 Nov 06 '18 at 13:15
  • Do you get this message *without* building? Then it's a bug in VSCode. If you get it when building, then you try to build the header file instead of a source file (that should `#include` the header file). – Some programmer dude Nov 06 '18 at 13:20
  • VScode is giving the error before a build. Here is a command VScode is running to build... `g++ -std=c++11 main.cpp -o main` – Chance Smith Nov 06 '18 at 13:22
  • 2
    Then maybe this error comes from `"intelliSenseMode": "clang-x64" `, see [Is is possible to disable this warning in clang? warning: #pragma once in main file](https://stackoverflow.com/questions/16726352/is-is-possible-to-disable-this-warning-in-clang-warning-pragma-once-in-main-f), if it really does then it may be worth to submit a bug report. Or just use a proper IDE. – user7860670 Nov 06 '18 at 13:26
  • 1
    One trivial answer that’s good in the long term is to not use `#pragma once`—it’s not standard, because it can’t be implemented correctly on all systems. – Davis Herring May 20 '19 at 15:54

2 Answers2

2

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"
        ],
Co Villaf
  • 21
  • 3
1

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

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