How to develop Linux Kernel Module with vscode without incorrect error detection

Viewed 5692

I'm trying to develop a Linux kernel module with vscode on Ubuntu, but vscode will throw a lot errors about Linux system header, kernel macro, etc. (Even if it's just a almost empty driver...)

Can somebody help me configure vscode to remove these errors?
Below is my configuration and the screenshots of errors.

//c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}",
                "/usr/src/linux-headers-4.15.0-65/include"
            ],
            "defines": [
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c99",
            "cppStandard": "c++14",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "/usr/src/linux-headers-4.15.0-65/include"
                ],
                "limitSymbolsToIncludedHeaders": true
            },
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
//settings.json
{
    "files.associations": {
        "init.h": "c",
        "module.h": "c",
        "workqueue.h": "c",
        "list.h": "c",
        "types.h": "c",
        "posix_types.h": "c"
    },
    "C_Cpp.errorSquiggles": "Enabled",
    "clang.cflags": [
        "-std=c99",
        "-I/usr/src/linux-headers-4.15.0-65/include",
        "-DMODULE",
    ]
}

1 2 3 4

2 Answers

Add the following to your c_cpp_properties.json:

"defines": [
    "__GNUC__",
    "__KERNEL__"
],

That did the trick for me once I'd also sorted out my include path. Mine looks as follows:

"includePath": [
    "${workspaceFolder}/**",
    "/usr/include",
    "/usr/local/include",
    "/usr/src/linux-headers-4.15.0-88/include",
    "/usr/src/linux-headers-4.15.0-88-generic/include",
    "/usr/src/linux-headers-4.15.0-88/arch/x86/include",
    "/usr/lib/gcc/x86_64-linux-gnu/7.4.0/include"
],

I'm developing a Kernel Module on CentOS with Visual Studio Code. Here is a snippet of my c_cpp_properties.json. I know this is particular to my kernel version and distribution of linux, but maybe it will point you in the right direction. I believe I also have the full kernel source & debug downloaded.

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include",
                "/usr/local/include",
                "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include",
                "/usr/src/kernels/3.10.0-1062.1.1.el7.x86_64/include",
                "/usr/src/kernels/3.10.0-1062.1.1.el7.x86_64/arch/x86/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Here's a little more information on (what I think) each of these are pointing to:

/usr/include Header files for the C compiler.

/usr/local/include Header files for third party programs.

/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include Header files for GCC.

/usr/src/kernels/3.10.0-1062.1.1.el7.x86_64/include Header files for the particular kernel version you are using. Make sure the kernel-devl package is installed.

/usr/src/kernels/3.10.0-1062.1.1.el7.x86_64/arch/x86/include I'm pretty sure in here is architecture specific header files for the kernel.

This provides an error (and squiggle) free vscode for my setup.

Related