VSCode 无法识别来自 includepath 的包含

Som*_*ton 6 c++ zip zipper visual-studio-code

我遇到了一个问题,VSCode 会识别我包含的 zipper.h,然后突然翻过来告诉我没有这样的文件或目录。我不确定这是否是我的代码或包含或 vs 代码的问题。

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

我尝试将它添加到我的包含路径和 Windows 环境路径中。出于同样的原因,它不断失败。我对我做错了什么感到非常困惑。它不识别这些链接吗?编译时我应该通过 g++ 链接库吗?

#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;
}
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)

Vic*_*tor 21

"includePath"属性c_cpp_properties.jsonsettings.json与内部编辑器的 IntelliSense 功能相关,与编译无关。为了告诉编译器必要的包含路径,您需要在构建任务(在tasks.json)中指定相应的编译器选项,即"-Ipath/to/my/include/files".

这是我的文件中的构建任务示例tasks.json(查看"args"属性 - 它包含编译器选项"-I${workspaceFolder}/../..",即从当前目录向上两级):

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*ric 11

您没有告诉编译器有关名为 Zipper.h 的文件或该文件的位置或与之相关的任何信息。“g++ test.cpp -o test”只是告诉编译器编译一个名为 test.cpp 的源文件并链接它。你必须明白,Visual Studio Code 不是 IDE,不能自行编译。您的 .vscode 目录中应该有一个名为 c_cpp_properties.json 的文件。例如,我使用的看起来像这样,并且配置为 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
}
Run Code Online (Sandbox Code Playgroud)

这会告诉 Visual Studio Code 源文件和库的位置。这就是用于 IntelliSense(语法突出显示、错误波形图、代码完成等)的内容。然而,这与构建您的项目绝对无关。您的编译器现在不知道您在 Visual Studio Code 中设置的包含路径。因此,要编译您的项目,您必须告诉编译器他需要知道的一切。Visual Studio Code 只是执行您在任务中指定的内容。这与进入该目录并在命令提示符中输入相同的内容相同。因此,我建议您阅读如何使用 g++ 编译 c++ 项目,您的问题与 Visual Studio Code 根本无关。如果您计划做一些比单个源文件更大的事情,我强烈建议您学习 CMake。一旦您有更多源文件并包含/要链接的库,通过手动调用 gcc get 进行编译就变得非常复杂。设置 Cmake 后,您只需在 Visual Studio Code 中指定一项与此类似的任务来构建您的项目:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "cmake --build Build",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我还建议您阅读以下内容: https ://code.visualstudio.com/docs/cpp/config-mingw

这是一个非常好的解释,基本上准确地解释了 Microsoft 正在尝试做的事情,并在我开始使用 Visual Studio Code 进行 C++ 工作时帮助我理解这一点。


小智 5

我也有同样的问题。就我而言,这是文件中不必要的行c_cpp_properties.json。该行是:"configurationProvider": "ms-vscode.makefile-tools"

由于这一行,它尝试从文件中Makefile忽略不正确的配置来配置包含。"includePath"c_cpp_properties.json

要解决该问题,请注释掉上述行或充分配置您的Makefile. 之后,可能需要重新启动 VS Code。也尝试暂时隐藏Makefile(例如.Makefile在 Linux 中重命名为)。