VSCode无法识别来自includepath的include

VSCode not recognizing includes from includepath

本文关键字:includepath include 识别 VSCode      更新时间:2023-10-16

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

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

我已经尝试将它添加到我的include路径和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;
}
c:UsersDeskDesktopCodeCycling>cd "c:UsersDeskDesktopCodeCycling" && g++ test.cpp -o test && "c:UsersDeskDesktopCodeCycling"test
test.cpp:1:10: fatal error: zipper.h: No such file or directory
#include <zipper.h>
^~~~~~~~~~
compilation terminated.
c_cpp_properties.jsonsettings.json中的

"includePath"属性仅与内部编辑器的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"
}
]
}

您没有告诉编译器任何关于名为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
}

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

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

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

这是一个非常好的解释,基本上确切地说明了你正试图由微软做什么,并帮助我在开始使用Visual Studio代码进行c++工作时理解了这一点。

我也遇到过同样的问题。在我的情况下,这是c_cpp_properties.json文件中不必要的一行。那句话是:"configurationProvider": "ms-vscode.makefile-tools"

由于这一行,它试图从制作不正确的Makefile配置includes,忽略c_cpp_properties.json文件中的"includePath"

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

Visual Studio代码不会更改构建命令本身,即使includePath发生了更改。您应该在.vscode/tasks.json中自己更改构建命令。请参阅本教程。