-2

[SOLVED]

Environment used:

  • Windows 10 Enterprise, Version 10.0.19042
  • Visual Studio Code, 1.54.3
  • C/C++ Extension for Visual Studio Code, Version 1.2.2: February 25, 2021
  • 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)

Briefly, the problem is:
When I try to #include <dirent.h>, it is failed with the error: cannot open source file "dirent.h",
BUT #include "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" to the file is working.

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 "C:\Program Files\mingw-w64\mingw64\x86_64-w64-mingw32\include\dirent.h" -> included, no errors

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. IntelliSense helps to include it: #include <direct.h>

My config files must be well-configured, since compilation and GDB debugging are working well.
I have two configurations: GCC and Win32 (MSVC compiler).

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). direct.h can be found without adding includePath values. I read somewhere that compilerPath makes environment asking the compiler about headers available. Also, I tried to change "cStandard": "c11" to other values: "gcc11", for example. It doesn't help.

I also tried to set the value of C_Cpp.default.includePath in workspace.codeworkspace file:

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

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):

#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

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? (and other headers also)

Thank you for any help and information.

Michael
  • 1
  • 3
  • 1
    You need to set the include path in both `c_cpp_properties.json` and `tasks.json` in tasks.json the `-I` parameter of gcc specifies the path. The `includePath` in `c_cpp_properties.json` is only used for intellisense I believe – drescherjm Mar 19 '21 at 20:58
  • @drescherjm thanks a lot for your answer. I added the path to tasks.json in this format: "args": ["-IC:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include" ...] And to c_cpp_properties.json: "includePath": ["C:\\Program Files\\mingw-w64\\mingw64\\x86_64-w64-mingw32\\include"... The program compiled successfully, but the Problems tab shows the problems of including anyway. Also, IntelliSense doesn't help with . Will try to fix it. Thanks. – Michael Mar 19 '21 at 21:49
  • @drescherjm thank you, the problem solved with this answer: [vscode "#include errors detected. Please update your includePath](https://stackoverflow.com/questions/51227662/vscode-include-errors-detected-please-update-your-includepath/57742056#57742056) The reason was in that I had several configurations (Win32 of MSVC compiler also). For some reason, Win32 configuration was used instead of GCC configuration. **Useful thing:** Ctrl + P-> **"C/C++: Log Diagnostics"**, it shows output of your current configuration, PATHs used. Now, -I option and IncludePath parms are not required. Thanks a lot. – Michael Mar 19 '21 at 22:25

1 Answers1

0

The problem solved, the solution and useful tips:
The reason was in that I had several configurations (Win32 of MSVC compiler along with GCC). For some reason, Win32 configuration was used instead of GCC configuration. Despite that compilation was successful after setting -I and IncludePath, #include <...> was not working well because another configuration was used (Win32 of MSVC compiler). As a result, Problem tab was notifying about #include problems detected. And IntelliSense wasn't helping to include headers.
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.

A good detailed answer about #include issues is here: vscode "#include errors detected. Please update your includePath

Now, I commented Win32 configuration. 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":
("Output" tab can be opened by Ctrl + Shift + U by defaul)

-------- 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
Michael
  • 1
  • 3