ubuntu18.04 vscode kernel module program ERROR: identifier "KBUILD_MODNAME" is undefine

Viewed 1185

My environment: Ubuntu 18.04, kernel: linux-5.3.0-53 vscode: lastest

Command for fixing linux/module.h not find asm/xxx.h

cd /usr/src/linux-headers-5.3.0-53/include

sudo ln -s asm-generic/ asm

My c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include",
                "/usr/local/include",
                "/usr/src/linux-headers-5.3.0-53/include",
                "/usr/src/linux-headers-5.3.0-53-generic/include",
                "/usr/src/linux-headers-5.3.0-53/arch/x86/include",
                "/usr/src/linux-headers-5.3.0-53/include/uapi",
                "/usr/lib/gcc/x86_64-linux-gnu/7.5.0/include"
            ],
            "defines": [
                "__GNUC__",
                "__KERNEL__"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

My hello_module.c, a simple Linux kernel module program.

//a simple linux kernel module program.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");  //error here

static int hello_init(void){
    printk(KERN_ALERT "hello world\n");
    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT "goodbye world\n");
}

module_init(hello_init);
module_exit(hello_exit);

then ERROR:

identifier "KBUILD_MODNAME" is undefined

How to fix? Please help me..

1 Answers

KBUILD_MODNAME is automatically defined by the Makefile when you build your kernel module, so you will not find it anywhere in the kernel header files.

To stop vscode from complaining about undefined KBUILD_MODNAME identifier, just add a dummy definition to your c_cpp_properties.json file in the "defines" section

"defines": [
    "KBUILD_MODNAME=\"hello_module\"",
    "__GNUC__",
    "__KERNEL__"
],                

That should make vscode happy =)

The value you define for KBUILD_MODNAME does not really matter, since it is automatically defined during the make process.

I have not understood if this is your only problem, or if you also have problems to compile the module. If yes let me know and we will figure out how to solve that too!

Related