C++/G++ including headers from another folder

Viewed 4646

I have been given a C++ project (for school) that I need to run and add additional code to. I would like to run the project in Visual Studio Code. I have downloaded the C/C++ extension as well as the Code Runner extension. When I try to run the main.cpp file I get the following error:

enter image description here

The ANTLRInputStream.h file is located in another src folder located in the runtime folder. I can just change the include to something like this:

#include "../runtime/src/ANTLRInputStream.h" 

But that would give me another error where inside the ANTLRInputStream there are a bunch of includes that also refer to header files located elsewhere.

I have the following properties file:

{
"configurations": [
    {
        "name": "MinGW",
        "compilerPath": "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc.exe",
        "includePath": [
            "${workspaceFolder}/**",
            "runtime/src"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "intelliSenseMode": "gcc-x64"
    }
],
"version": 4
}

Where I have tried some variations like:

  • runtime/src
  • ${rootFolder}/**
  • ${rootFolder}/runtime/src

And some similair ones, but they don't work and I don't really know if they should work (as I don't really have a clue on how to setup C++ projects in VSCode). How would I make all my includes work and being able to run the program without changing every include manually to find its location?

2 Answers

It appears (from the CMakeLists.txt file in the root folder, that your project was meant as a CMake project. You can find info on the CMake extension for VSC here.

From the screenshot, the error message is:

main.cpp:2:10: fatal error: ANTLRInputStream.h: No such file or directory

This error is coming from the compiler, g++, not Visual Studio Code. VSCode is simply showing you the compiler's output within its own output window.

To fix this, you have to tell the compiler where to look for the file using the -I option, for example:

g++ -I../runtime/src main.cpp -o main

The includePath attribute in c_cpp_properties.json is used to tell VSCode (as opposed to the compiler) where to find header files. This answer of mine talks about includePath in more detail.

Related