14

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.
SomeSimpleton
  • 350
  • 1
  • 2
  • 12
  • If the solutions below don't work for you, be aware that VScode doesn't support network folders well, [issue 3402](https://github.com/microsoft/vscode-cpptools/issues/3402). – Rainald62 Aug 24 '21 at 10:56

4 Answers4

17

"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"
    }
  ]
}
Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
Victor
  • 171
  • 1
  • 5
  • Awesome! I tried editing the "commandArgs" in c_cpp_properties.json, but still couldn't get the import paths to resolve. tasks.json was where I needed to add the command line args. – Eric Canton Jan 07 '23 at 01:57
  • best answer it is not even mantion in microsoft vscode ref site – user63898 Jan 09 '23 at 16:58
  • Note that the path to the include path needs to be written right after the -I flag, if you put a space after it, as "-I your\path\to\include", it will not work. – Gabriel Jefferson Mar 12 '23 at 14:13
10

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.

Eric
  • 1,183
  • 6
  • 17
  • 1
    Hello Eric, Thanks for the help and explanation. I honestly for some reason thought that those files were gonna be included somehow by VSCode. I do know how makefiles work and all I just have never used them in windows and never knew they were a thing in windows, to be honest. I am sort of new to developing in windows and I already dont like it. I will be setting up my raspberry pi with – SomeSimpleton Aug 12 '19 at 21:56
  • What you did wasn't really a Windows thing. VSCode is a cross platform code editor and MinGW (g++) is just the Windows port of the GNU Compiler Collection. So building with it on Windows by hand is pretty much identical on how you would do it on Linux. Building with MSBuild for example is very different. – Eric Aug 13 '19 at 10:58
  • I understand it really isnt a windows thing but I am not really used to VSCode nor windows. I usually just use vi and stick with that but as I was switching to windows I went and got all fancy and it sorta came back to bite me. I will look into MSBuild and will try that out. – SomeSimpleton Aug 15 '19 at 18:30
  • 1
    @SomeSimpleton The explanation of `Settings` -> `Extensions` -> `C/C++` -> `C_Cpp>Default: Include Path` says: `The value to use in a configuration if "includePath" is not specified in c_cpp_properties.json.` So I would think, that the includePath defined in the json would of course also change the include pathes of the compiler? Well I also have the same problem, the compiler doesn't find the included files, so I can not prove my claim :/ – GURKE Jul 13 '21 at 21:53
4

I have had the same issue. In my case, it was an unnecessary line in the c_cpp_properties.json file. That line was: "configurationProvider": "ms-vscode.makefile-tools".

Due to this line, it tried to configure includes from an improperly made Makefile, ignoring "includePath" from the c_cpp_properties.json file.

To solve the issue, comment out the mentioned line or adequately configure your Makefile. After that, a restart of VS Code may be necessary. Try also temporarily hide Makefile (e.g. rename to .Makefile in Linux).

DeZee
  • 95
  • 6
2

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.