VSCode 无法识别来自 includepath 的包含 [英] VSCode not recognizing includes from includepath

查看:59
本文介绍了VSCode 无法识别来自 includepath 的包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

#include 无效的 zipFolder(){zipper::Zipper zipFile("logs.zip");zipFile.add("C:\\Cycling");zipFile.close();}int main(int argc, char const *argv[]){返回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.cpp:1:10: 致命错误: zipper.h: 没有那个文件或目录#include ^~~~~~~~~~编译终止.

解决方案

您没有告诉编译器有关名为 Zipper.h 的文件的任何信息,或者它的位置,或任何与之相关的信息.g++ test.cpp -o test"只是告诉编译器编译一个名为 test.cpp 的源文件并链接它.您必须了解 Visual Studio Code 不是 IDE,无法自行编译.您应该在 .vscode 目录中有一个名为 c_cpp_properties.json 的文件.例如,我使用的那个看起来像这样并且配置为 mingw64.

<代码>{配置":[{"name": "Win32",包含路径":[${workspaceFolder}/Source/**"],"compilerPath": "C:\\mingw-w64\\mingw64\\bin\\gcc.exe","intelliSenseMode": "gcc-x64",浏览":{小路": [${workspaceFolder}"],limitSymbolsToIncludedHeaders":真,数据库文件名":"}}],版本":4}

这会告诉 Visual Studio Code 你的源文件和库在哪里.这是用于 IntelliSense 的(语法高亮、错误曲线、代码完成等).然而,这与构建您的项目完全无关.您的编译器现在不知道您在 Visual Studio Code 中设置的包含路径.所以要编译你的项目,你必须告诉你的编译器他需要知道的一切.Visual Studio Code 只是执行您在任务中指定的内容.这与转到该目录并在命令提示符中键入相同的内容相同.因此,我建议您阅读有关如何使用 g++ 编译 c++ 项目的信息,您的问题根本与 Visual Studio Code 无关.如果您打算做一些比单个源文件大一点的事情,我强烈建议您学习 CMake.一旦你有更多的源文件和包含/库链接,通过手动调用 gcc get 进行编译真的很复杂.一旦你设置了你的 Cmake,你就可以在 Visual Studio Code 中指定一个类似于这个任务来构建你的项目:

<代码>{"版本": "2.0.0",任务": [{"label": "构建",类型":外壳","command": "cmake --build Build",团体": {"种类": "建造",isDefault":真}}]}

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

这很好地解释了 Microsoft 正在尝试做的事情,并帮助我在开始使用 Visual Studio Code 进行 C++ 工作时理解了这一点.

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.

解决方案

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.

这篇关于VSCode 无法识别来自 includepath 的包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆