vscode, mingw, g++, header files include fail from subdirectory

Viewed 348

Following the guideline on how to setup mingw g++ in vscode, I cannot seem to succeed in including headerfiles from a subdirectory. I have my working folder, in which my main.cpp lies. In that working folder, I have a subfolder called "inc" in which my header file add.h lies, and I have a subfolder "src" in which my add.cpp file lies. When I pass the argument to the compiler to include header files using the -I argument, it works fine. But this kind of contradicts the tutorial found on the vscode website that states that I should add include paths in the c_cpp_properties.json file: https://code.visualstudio.com/docs/cpp/config-mingw

You only need to add to the Include path array setting if your program includes header files that are not in your workspace or in the standard library path.

The build output message:

C:\MinGW\bin\g++.exe -g C:\Users\Alexander\Documents\GitHub\CPP_Projects\00_helloworld_min*.cpp C:\Users\Alexander\Documents\GitHub\CPP_Projects\00_helloworld_min\src*.cpp -o C:\Users\Alexander\Documents\GitHub\CPP_Projects\00_helloworld_min\main.exe C:\Users\Alexander\Documents\GitHub\CPP_Projects\00_helloworld_min\main.cpp:2:17: fatal error: add.h: No such file or directory #include "add.h"

Any ideas what I am doing wrong?

A simple minimalistic code:

#include <iostream>
#include "add.h"
 
int main()
{
    std::cout << "The sum of 15 and 5 is " << add(15, 5) << std::endl;
    return 0;
}

A screenshot of my project to illustrate my inc and src folder: vscode

My tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspaceFolder}\\*.cpp",
                "${workspaceFolder}\\src\\*.cpp",
                // "-I",
                // "${workspaceFolder}\\inc\\",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\MinGW\\bin\\g++.exe"
        }
    ]
}

My c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "GCC",
            "includePath": [
                "${workspaceFolder}\\**",
                "${workspaceFolder}\\inc\\**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x86"
        }
    ],
    "version": 4
}
0 Answers
Related