Search code examples
c++visual-studio-codeinclude

C++ include error failing to find header file: No such file or directory


My c++ build task in VS Code is failing to compile. The compiler gives an error that it can't find a header file that I expect it to be able to find since I specified the corresponding include path flag in the arguments I pass to the compiler in my cppbuild task.

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "CUSTOM C/C++: g++.exe build active file",
            "command": "g++.exe",
            "args": [
                "-I C:\\Users\\MY_NAME\\Desktop\\projects\\myproject\\include",
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ],
    "version": "2.0.0"
}

My includePath member in my c_cpp_properties.json configuration is set up correctly and IntelliSense is working fine.

model.cpp:

#include <iostream>
#include <myproject/example.hpp>
int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}

The exact directory (copy-pasted from file explorer) where the header file example.hpp is stored is:

C:\Users\MY_NAME\Desktop\projects\myproject\include\myproject

Surely the include path given in the tasks.json file, and the include line from model.cpp would point it to this location?

This is the error it gives me in the VS Code terminal:

C:\Users\MY_NAME\Desktop\projects\myproject\main.cpp:2:10: fatal error: example.hpp: No such file or directory
    7 | #include <example.hpp>
      |          ^~~~~~~~~~~~~
compilation terminated.

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 

What's wrong, and how can I fix it?


Solution

  • In the tasks.json file, the include directory had a space between -I and the file directory path:

    "-I C:\\Users\\wswil\\Desktop\\projects\\asteroids-game\\include"
    

    While trying to learn how to set everything up, I read that you can have the space or not, it won't make a difference. I added it in so it was easier to read but turns out this is incorrect. There cannot be a space or it won't read the file directory.