问题

从一年前开始,我的VScode老是第一行头文件报错’iostream’ file not found,不能检查语法错误,但是又能运行代码,能运行出正确结果,就很迷。

作者在这个问题上,每次需要写c++代码的时候,就有半天一天时间搞这个,实在是血的教训( vscode???狗都不用)。但是vscode简洁的功能,运行小代码挺方便的,也是不得不用。

iostream头文件 内部 iostream头文件为什么报错_ide

关于不能检查语法错误

我的vscode settings.json文件里面莫名其妙写了这一行代码

"C_Cpp.errorSquiggles": "Disabled"

我竟然一直没发现是行代码搞的不能检查语法错误(生气 无语)。千万要删掉或注释掉这一行。

找不到头文件的解决方案

期间从网上找了很多种方法,都不能解决问题。最后发现要禁用拓展C/C++ Clang Command Adapter。不过要先确保配置的环境是正确的。

①配置好c++环境。网上搜搜就能找到。我的json文件在文章最后。

②尝试一个一个禁用插件。我的vscode禁用的这个插件就不会报错了。

iostream头文件 内部 iostream头文件为什么报错_vscode_02

有答主说是C/C++ GNU Global导致的,部分情况下,TSLint,C#插件, Cmake插件也可能导致这个报错。

所以如果环境配置没问题的话,碰到这种情况,优先考虑拓展的问题。一个一个禁用拓展,不断尝试。

解决问题

最后写代码的时候终于没有红线了。非常开心。

iostream头文件 内部 iostream头文件为什么报错_ide_03

配置文件

c_cpp_properties.json

{
    "configurations": [
        {
            "browse": {
                "path": [
                    "${workspaceRoot}"
                ],
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "name": "windows-gcc-x64",
            "includePath": [
                "${workspaceFolder}/**",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/include",
                "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++"
          
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
                
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "H:\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "cStandard": "${default}",
            "cppStandard": "${default}",
            "intelliSenseMode": "windows-gcc-x64",
            "compilerArgs": []
        }
    ],
    "version": 4
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
      "preLaunchTask": "g++.exe build active file", //和tasks中label保持一致
      "type": "cppdbg", // 配置类型,这里只能为cppdbg  
      "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)  
      "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径  
      "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可  
      "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false  
      "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
      "environment": [],
      "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
      "MIMode": "gdb", // gdb
      "miDebuggerPath": "H:\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

settings.json

{
    "files.associations": {
        "iostream": "cpp",
        "ostream": "cpp",
        "cstdlib": "cpp",
        "algorithm": "cpp",
        "iosfwd": "cpp",
        "bitset": "cpp",
        "*.tcc": "cpp",
        "complex": "cpp",
        "iomanip": "cpp",
        "stack": "cpp",
        "vector": "cpp",
        "deque": "cpp",
        "list": "cpp",
        "string": "cpp",
        "functional": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "cctype": "cpp",
        "cfenv": "cpp",
        "charconv": "cpp",
        "chrono": "cpp",
        "cinttypes": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "condition_variable": "cpp",
        "csetjmp": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cuchar": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "forward_list": "cpp",
        "unordered_map": "cpp",
        "unordered_set": "cpp",
        "exception": "cpp",
        "iterator": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "set": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "slist": "cpp",
        "fstream": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "scoped_allocator": "cpp",
        "shared_mutex": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "typeindex": "cpp",
        "typeinfo": "cpp",
        "valarray": "cpp",
        "xhash": "cpp",
        "xiosbase": "cpp",
        "xlocale": "cpp",
        "xlocmon": "cpp",
        "xlocnum": "cpp",
        "xloctime": "cpp",
        "xmemory": "cpp",
        "xstring": "cpp",
        "xtr1common": "cpp",
        "xtree": "cpp",
        "xutility": "cpp",
        "ios": "cpp",
        "game.h": "c"
    },
    // "clang.cflags": [
    //     "H:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++"
    // ],
    // "C_Cpp.default.compilerPath": "H:\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"

}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
        "label": "g++.exe build active file",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
        }
    ]
}