c_cpp_properties.json 中的 includePath 在 VSCode 中不适用于 C

includePath in c_cpp_properties.json not working in VSCode for C

我在 VSCode 工作,在 Ubuntu 18.04 上使用 C/C++ 扩展。

我试图包含 gmodule.h,它在第 2 行,主文件的字符 10 上引发了错误 gmodule.h: No such file or directory

所以,问题在于 gmodule.h 不在 /usr/include 中,而是在 /usr/include/glib-2.0 中。意识到这一点,我将这个文件夹添加到 c_cpp_properties.json 中的 includePath 变量中。但是,它仍然会引发相同的错误。

当使用 #include <glib-2.0/gmodule.h> 而不是 #include <gmodule.h> 时,它确实有效,但这只会将问题转移到 gmodule.h 本身,因为 glib-2.0 文件夹中的其他包含仍然没有在 gmodule.h.

内工作

总而言之,问题是添加到 c_cpp_properties.json 中的 includePath 不会改变任何东西,我想知道如何进行这项工作,因为我想使用 gmodule。

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "/usr/include/glib-2.0/*"
            ]
        }
    ],
    "version": 4
}

现在我只是想包含 gmodule.h 但还没有对它做任何事情,所以这是我的主文件:

#include <stdio.h>
#include <gmodule.h>

int main() {
    printf("hai\n");
    return 0;
}

c_cpp_properties.json 控件,其中 intellisense 在 IDE 中解析包含文件。 IDE 和构建任务是独立的事物,因此在 VS Code 中独立配置和运行。

您的问题的解决方案是将包含路径添加到您的 tasks.json 文件中,如下所示:

"args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
        "--include-directory=/usr/include/glib-2.0/"
 ],

根据 pkg-config 的报告,我通过为 glib 添加两条路径来设法使 IntelliSense 工作:

$ pkg-config --cflags glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include

.vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/glib-2.0/**",
                "/usr/lib/x86_64-linux-gnu/glib-2.0/include/**"
            ],
            "defines": [],
            ...
        }
    ],
    ...
}

对我来说解决这个问题的方法是只键入不带 /* 或 /** 的文件夹路径 然后它提示我是否要为此文件夹启用 Intellisense,我允许了它并且成功了。