Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode not recognizing includes from includepath

I am having an issue where VSCode will recognize my include of zipper.h and then out of nowhere flip on me and tell me that there is no such file or directory. I am not sure if this is an issue with my code or includes or vs code.

https://i.gyazo.com/2d35a31abf83546d8633d991bcb4752a.png https://i.gyazo.com/96ad7825e8d1c390035a4db2f789bbcf.png

I have tried adding it both to my include path and windows environment path. it keeps failing for the same reason. I am very confused on what I'm doing wrong. Is it not recognizing those links? Should I be linking the libraries through g++ when compiling?

#include <zipper.h>

void zipFolder()
{
    zipper::Zipper zipFile("logs.zip");
    zipFile.add("C:\\Cycling");
    zipFile.close();
}

int main(int argc, char const *argv[])
{
    return 0;
}
c:\Users\Desk\Desktop\Code\Cycling>cd "c:\Users\Desk\Desktop\Code\Cycling\" && g++ test.cpp -o test && "c:\Users\Desk\Desktop\Code\Cycling\"test
test.cpp:1:10: fatal error: zipper.h: No such file or directory
 #include <zipper.h>
          ^~~~~~~~~~
compilation terminated.
like image 222
SomeSimpleton Avatar asked Aug 12 '19 09:08

SomeSimpleton


3 Answers

You did not tell your compiler anything about a file called Zipper.h or where it is loacted, or anything related to it. "g++ test.cpp -o test" just tells the compiler to compile a source file called test.cpp and link it. You have to understand that Visual Studio Code is not an IDE and can't compile by itself. You should have an file called c_cpp_properties.json file located in your .vscode directory. The one that i use for example looks like this and is configured for mingw64.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/Source/**"                
            ],
            "compilerPath": "C:\\mingw-w64\\mingw64\\bin\\gcc.exe",
            "intelliSenseMode": "gcc-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

This tells Visual Studio Code where your source files and libraries are. This is what is used for IntelliSense (Syntax Highlights, Error Squiggles, Code Completion, etc). However this has absolutly nothing to do with building your project. Your compiler doesn't now know about the include path's you set in Visual Studio Code. So to compile your project you have to tell your compiler everything he needs to know. Visual Studio Code simply executes what you specify in the task. It's the same as going to that directory and type in the same thing in your command promt. So i recommend you to read up on how to compile a c++ project with g++, your problem is not related to Visual Studio Code at all. If youre planning on doing something thats a bit bigger than just a single source file i strongly suggest you to learn CMake. Compiling by manually calling gcc get's really complicated once you have more source files and includes / libraries to link. Once you have set up your Cmake you can just specify a task in Visual Studio Code similar to this one to build your project:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "cmake --build Build",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

I also recommend you to read this: https://code.visualstudio.com/docs/cpp/config-mingw

This is a really good explanation of basicly exactly what you are trying to do by Microsoft and helped me understanding this when i started to use Visual Studio Code for my c++ work.

like image 172
Eric Avatar answered Oct 06 '22 13:10

Eric


"includePath" property both in c_cpp_properties.json and settings.json relates only to the internal editor's IntelliSense feature and has nothing to do with compilation. In order to tell the compiler the necessary include paths, you need to specify a correspondent compiler option in your build task (in tasks.json), namely "-Ipath/to/my/include/files".

Here is a build task example from my tasks.json file (look at "args" property - it contains compiler option "-I${workspaceFolder}/../..", i.e. two levels up from the current directory):

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++-9 build active file ver(1)",
      "command": "/usr/bin/g++-9",
      "args": [
        "-std=c++17",
        "-I${workspaceFolder}/../..",
        "-g",
        "${workspaceFolder}/*.cpp",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "compiler: /usr/bin/g++-9"
    }
  ]
}
like image 4
Victor Avatar answered Oct 06 '22 13:10

Victor


Visual Studio Code not changes build command itself, even if includePath changes. You should change build command yourself in .vscode/tasks.json. See this tutorial.

like image 1
Vadim Chuprakov Avatar answered Oct 06 '22 14:10

Vadim Chuprakov