Chance Smith
Chance Smith

Reputation: 1255

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:

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
}

Upvotes: 7

Views: 9177

Answers (2)

siddhartha kasaraneni
siddhartha kasaraneni

Reputation: 688

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

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

Upvotes: 1

Co Villaf
Co Villaf

Reputation: 21

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"
        ],

Upvotes: 2

Related Questions