【问题标题】:VS Code, GCC(MinGW/WinLibs), Win10: #include <dirent.h> -> "#include errors detected. Please update your IncludePath", BUT #include "[abs path]" is OKVS Code, GCC(MinGW/WinLibs), Win10: #include <dirent.h> -> "#include errors detected. Please update your IncludePath", 但 #include "[abs path]" 没问题
【发布时间】:2021-06-17 07:01:24
【问题描述】:

[已解决]

使用环境:

  • Windows 10 企业版,版本 10.0.19042
  • Visual Studio 代码,1.54.3
  • Visual Studio Code 的 C/C++ 扩展,版本 1.2.2:2021 年 2 月 25 日
  • MinGW-w64 8.0.0
    (我也在尝试使用 GCC 10.2.0 + 的 WinLibs 包 LLVM/Clang/LLD/LLDB 11.0.0 + MinGW-w64 8.0.0)

简单来说,问题是:
当我尝试#include 时,它失败并出现错误:无法打开源文件“dirent.h”,
但 #include “C:\Program Files\mingw-w64\mingw64\x86_64- w64-mingw32\include\dirent.h" 到文件正在工作。

两种情况的截图是:
(抱歉,我还不能嵌入图片,请打开截图链接):
#include <dirent.h> -> failed
#include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" -> included, no errors

如果通过指定的绝对路径包含文件,则代码编译并工作。

奇怪的是,几乎放在“C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include”目录下的direct.h头文件可能被包含在没有指定的情况下绝对路径。 IntelliSense 有助于将其包括在内: #include <direct.h>

我的配置文件必须配置好,因为编译和 GDB 调试运行良好。
我有两种配置:GCC 和 Win32(MSVC 编译器)。

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "GCC",
            "includePath": [
                "C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include\\**",
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
            //"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        },
        {
            "name": "Win32",
            ...
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

“includePath”的变体:["C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include**"] 没有帮助(根本没有 * 符号或只有一个符号)。 无需添加 includePath 值即可找到 direct.h。我在某处读到 compilerPath 使环境向编译器询问可用的标头。 另外,我尝试将“cStandard”:“c11”更改为其他值:例如,“gcc11”。没用。

我也尝试在workspace.codeworkspace文件中设置C_Cpp.default.includePath的值:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        //"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
        "C_Cpp.default.includePath": ["C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include"]
    },
}

tasks.json:

    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
        // "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
        "args": [
            // Add debug info (-g option) to the object file (files) and link to exe (-o option)
            "-g",
            "${fileDirname}\\**.c",
            // "${fileDirname}\\**.cpp",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
          ],
        "options": {
            //"cwd": "${workspaceFolder}"
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: \"C:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe\""
        //"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
    }

使用dirent.h的C语言代码比较简单(再次说明,指定绝对路径编译成功,所以一定与问题无关,以防万一):

#include <stdio.h>
#include "C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include\\dirent.h"
int main(int argc, char const *argv[])
{
    DIR *dir1;
    struct dirent *dirEntry;
    dir1 = opendir(".");
    if (dir1)
    {
        while ((dirEntry = readdir(dir1)) != NULL)
        {
            printf("%s\n", dirEntry->d_name);
        }
        closedir(dir1);
    }

    getchar();
}

PATH 环境变量设置为 GCC 编译器的路径: PATH variable -> GCC compiler path

有人可以帮忙吗?或者提供任何可能出错的信息? 为什么可以正确找到direct.h,但找不到dirent.h? (以及其他标题)

感谢您提供任何帮助和信息。

【问题讨论】:

  • tasks.json中c_cpp_properties.jsontasks.json都需要设置include路径,gcc的-I参数指定路径。 c_cpp_properties.json 中的 includePath 我相信仅用于智能感知
  • @drescherjm 非常感谢您的回答。我以这种格式添加了 tasks.json 的路径: "args": ["-IC:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include" ...] 并c_cpp_properties.json: "includePath": ["C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include"... 程序编译成功,但Problems选项卡显示问题反正包括在内。此外,IntelliSense 对 没有帮助。将尝试修复它。谢谢。
  • @drescherjm 谢谢,这个答案解决了这个问题:vscode "#include errors detected. Please update your includePath 原因是我有几个配置(MSVC 编译器的 Win32 也是)。由于某种原因,使用了 Win32 配置而不是 GCC 配置。 有用的东西: Ctrl + P-> "C/C++: Log Diagnostics",它显示你当前配置的输出,使用的路径。现在,不需要 -I 选项和 IncludePath 参数。非常感谢。

标签: c++ c gcc visual-studio-code mingw


【解决方案1】:

问题解决了,解决方法和有用的提示:
原因是我有几种配置(MSVC 编译器的 Win32 和 GCC)。由于某种原因,使用了 Win32 配置而不是 GCC 配置。尽管在设置 -I 和 IncludePath 后编译成功,但 #include <...> 无法正常工作,因为使用了另一个配置(MSVC 编译器的 Win32)。因此,问题选项卡会通知检测到 #include 问题。并且 IntelliSense 并没有帮助包含标题。
检查您当前配置的有用的东西
Ctrl + P-> "C/C++: Log Diagnostics",它显示了你当前配置的输出以及使用了哪些 PATH。

这里有关于#include 问题的详细解答: vscode "#include errors detected. Please update your includePath

现在,我评论了 Win32 配置。不知道有没有默认设置配置的选项。

{
    "configurations": [
        {
            "name": "GCC",
            "includePath": [
            //    "C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
        // {
        //     "name": "Win32",
...
        //     "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe",
        //     "cStandard": "c11",
        //     "cppStandard": "c++17",
        //     "intelliSenseMode": "msvc-x64"
        // },
    ],
    "version": 4
}

“C/C++:日志诊断”的输出:
(“输出”选项卡默认可以通过Ctrl+Shift+U打开)

-------- Diagnostics - 3/20/2021, 1:38:20 AM
Version: 1.2.2
Current Configuration:
{
    "name": "GCC",
    "includePath": [],
...
        "limitSymbolsToIncludedHeaders": true
    }
}
Translation Unit Configurations:
[ C:\Users\[USER]\C Projects\TestProgram\TestFile.c ]:
    Process ID: 6876
    Memory Usage: 14 MB
    Compiler Path: C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe
    Includes:
        C:\PROGRAM FILES\MINGW-W64\X86_64-8.1.0-POSIX-SEH-RT_V6-REV0\MINGW64\LIB\GCC\X86_64-W64-MINGW32\8.1.0\INCLUDE
        C:\PROGRAM FILES\MINGW-W64\X86_64-8.1.0-POSIX-SEH-RT_V6-REV0\MINGW64\LIB\GCC\X86_64-W64-MINGW32\8.1.0\INCLUDE-FIXED
        C:\PROGRAM FILES\MINGW-W64\X86_64-8.1.0-POSIX-SEH-RT_V6-REV0\MINGW64\X86_64-W64-MINGW32\INCLUDE
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: c11
    IntelliSense Mode: windows-gcc-x64
    Other Flags:
        --gcc
        --gnu_version=80100
Total Memory Usage: 55 MB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多