4

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?

SSteve
  • 10,550
  • 5
  • 46
  • 72
Niek Jonkman
  • 1,014
  • 2
  • 13
  • 31

2 Answers2

0

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.

OrdoFlammae
  • 721
  • 4
  • 13
  • Unfortunately, using "${workspaceDir}/runtime/src" and variations do not seem to solve my problem. I have checked the document you have provided. At one point they mention the following: "If a #include file or one of its dependencies cannot be found, you can also click on the green squiggles under the include statements to view suggestions for how to update your configuration." In my case I have no errors or squiggly lines in the IDE. To me it seems the compiler cannot find the correct header files, but the IDE can? – Niek Jonkman Sep 17 '18 at 11:47
  • 1
    Yeah, I don't know if there's actually a way to pass the compiler stuff like that, short of defining arguments in your config file. However, using the CMakeLists.txt file provided should get things set up like they should. – OrdoFlammae Sep 17 '18 at 11:57
  • If you don't mind trying Visual Studio 2017 (assuming you are on Windows), it now has native CMake support built in - you just hit File/Open/CMake, and select your CMakeLists.txt file. CMakeLists should have the necessary information to pass the include directories to the compiler when it runs. The interface and shortcut keys in VS2017 are different from VSC though, so it might take a bit of getting used to. – BareMetalCoder Sep 17 '18 at 14:32
0

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.

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79