简体   繁体   English

VS 代码、GCC(MinGW/WinLibs)、Win10:#include<dirent.h> -&gt; “#include 检测到错误。 请更新您的 IncludePath”,但 #include “[abs path]” 没问题</dirent.h>

[英]VS Code, GCC(MinGW/WinLibs), Win10: #include <dirent.h> -> “#include errors detected. Please update your IncludePath”, BUT #include “[abs path]” is OK

[SOLVED] [解决了]

Environment used:使用环境:

  • Windows 10 Enterprise, Version 10.0.19042 Windows 10 企业版,版本 10.0.19042
  • Visual Studio Code, 1.54.3 Visual Studio 代码,1.54.3
  • C/C++ Extension for Visual Studio Code, Version 1.2.2: February 25, 2021 Visual Studio Code 的 C/C++ 扩展,版本 1.2.2:2021 年 2 月 25 日
  • MinGW-w64 8.0.0 MinGW-w64 8.0.0
    (I'm trying also WinLibs package with GCC 10.2.0 + LLVM/Clang/LLD/LLDB 11.0.0 + MinGW-w64 8.0.0) (我也在尝试 WinLibs package 和 GCC 10.2.0 + LLVM/Clang/LLD/LLDB 11.0.0 + MinGW-w64 8.0.0)

Briefly, the problem is:简而言之,问题是:
When I try to #include <dirent.h>, it is failed with the error: cannot open source file "dirent.h",当我尝试#include <dirent.h> 时,失败并出现错误:无法打开源文件“dirent.h”,
BUT #include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" to the file is working.但是文件的#include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" 正在工作。

Screenshots of two cases are:两个案例的截图是:
(sorry, I am not allowed to embed images yet, please open the links to the screenshots): (对不起,我还不允许嵌入图片,请打开截图链接):
#include <dirent.h> -> failed #include <dirent.h> -> 失败
#include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" -> included, no errors #include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" -> 包含,没有错误

If include the file by the absolute path specified, the code is compiled and working.如果通过指定的绝对路径包含文件,则代码被编译并工作。

Strange thing is that nearly placed direct.h header under the "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include" directory may be included with no specifying the absolute path.奇怪的是,几乎放在“C:\Program Files\mingw-w64\ mingw64 \x86_64-w64-mingw32\include”目录下的direct.h header可能包含在没有指定绝对路径的情况下。 IntelliSense helps to include it: #include <direct.h> IntelliSense 有助于包含它: #include <direct.h>

My config files must be well-configured, since compilation and GDB debugging are working well.我的配置文件必须配置好,因为编译和 GDB 调试工作正常。
I have two configurations: GCC and Win32 (MSVC compiler).我有两种配置:GCC 和 Win32(MSVC 编译器)。

c_cpp_properties.json 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
}

Variations of "includePath": ["C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include**"] doesn't help (without * symbols at all or with one symbol). "includePath": ["C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include**"] 的变体没有帮助(根本没有 * 符号或只有一个符号)。 direct.h can be found without adding includePath values.无需添加 includePath 值即可找到 direct.h。 I read somewhere that compilerPath makes environment asking the compiler about headers available.我在某处读到 compilerPath 使环境向编译器询问可用的标头。 Also, I tried to change "cStandard": "c11" to other values: "gcc11", for example.另外,我尝试将“cStandard”:“c11”更改为其他值:例如,“gcc11”。 It doesn't help.它没有帮助。

I also tried to set the value of C_Cpp.default.includePath in workspace.codeworkspace file:我还尝试在 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:任务.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\""
    }

Code of C language which uses dirent.h is simple (again, it is compiled successfully if the absolute path is specified, so it must not be related to the problem, just in case):使用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 environment variable is set to the GCC compiler's path: PATH variable -> GCC compiler path PATH 环境变量设置为 GCC 编译器路径: PATH 变量 -> GCC 编译器路径

Could anyone help with it please?有人可以帮忙吗? Or give any information what may be wrong?或者提供任何可能出错的信息? Why direct.h can be found correctly, but dirent.h is not?为什么可以正确找到direct.h,但找不到dirent.h? (and other headers also) (以及其他标题)

Thank you for any help and information.感谢您提供任何帮助和信息。

The problem solved, the solution and useful tips:问题解决了,解决方法和有用的提示:
The reason was in that I had several configurations (Win32 of MSVC compiler along with GCC).原因是我有几种配置(MSVC 编译器的 Win32 和 GCC)。 For some reason, Win32 configuration was used instead of GCC configuration.由于某种原因,使用了 Win32 配置而不是 GCC 配置。 Despite that compilation was successful after setting -I and IncludePath, #include <...> was not working well because another configuration was used (Win32 of MSVC compiler).尽管在设置 -I 和 IncludePath 后编译成功,但 #include <...> 无法正常工作,因为使用了另一个配置(MSVC 编译器的 Win32)。 As a result, Problem tab was notifying about #include problems detected.因此,问题选项卡会通知检测到 #include 问题。 And IntelliSense wasn't helping to include headers.并且 IntelliSense 并没有帮助包含标题。
Useful thing to check your current configuration :检查您当前配置的有用的东西
Ctrl + P-> "C/C++: Log Diagnostics", it shows output of your current configuration and which PATHs are used. Ctrl + P-> "C/C++: Log Diagnostics",它显示了您当前配置的 output 以及使用了哪些 PATH。

A good detailed answer about #include issues is here: vscode "#include errors detected. Please update your includePath关于#include 问题的一个很好的详细答案在这里: vscode "#include errors detected. Please update your includePath

Now, I commented Win32 configuration.现在,我评论了 Win32 配置。 I do not know if there is an option to set a configuration by default.我不知道是否有默认设置配置的选项。

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

Output of "C/C++: Log Diagnostics": 《C/C++:日志诊断》的 Output:
("Output" tab can be opened by Ctrl + Shift + U by defaul) (“输出”选项卡默认可以通过 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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