includePath in c_cpp_properties.json not working in VSCode for C

Viewed 7700

I'm working in VSCode with the C/C++ extension on Ubuntu 18.04.

I'm trying to include gmodule.h and it raises the error gmodule.h: No such file or directory on line 2, character 10 of the main file.

So, the problem lies with gmodule.h being not in /usr/include but in /usr/include/glib-2.0. Realizing this, I added this folder to the includePath variable in c_cpp_properties.json. However, it still raises the same error.

When using #include <glib-2.0/gmodule.h> instead of #include <gmodule.h>, it does work but this only shifts the problem to gmodule.h itself, as other includes that lie in the glib-2.0 folder still don't work inside of gmodule.h.

All in all, the problem is that add to the includePath in c_cpp_properties.json doesn't change anything and I want to know how to make this work, since I would like to use 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
}

for now I'm just trying to include gmodule.h and not do anything with it yet, so this is my main file:

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

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

The c_cpp_properties.json controls, among other things, where intellisense in the IDE resolves include files. The IDE and the build tasks are independent things and as a result, configured and operate independently in VS Code.

The solution to your problem is to add the include path to your tasks.json file, as follows:

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

I managed to make IntelliSense work by adding two paths for glib as reported by pkg-config:

$ 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": [],
            ...
        }
    ],
    ...
}

What solved it for me was to just type folder path without /* or /** Then it prompted me if I want to enable Intellisense for this folder and I allowed it and it worked.

Related