I'm trying to write a kernel module in VS Code on Ubuntu 20.04
The problem is that my includes seem to be completely broken.
Firstly, I have no <linux/uaccess.h>
or <asm/uaccess.h>
headers. Secondly, I have put many includes on top of my file like:
#include <linux/module.h>
#include <linux/kernel.h>
However, no definitions for kernel stuff like printk()
are found. Macros like bool
or NULL
are also not defined in my linux/types.h
.
I tried sudo apt-get install linux-headers-generic
.
Here's my linux/module.h
:
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _LINUX_MODULE_H
#define _LINUX_MODULE_H
/* Flags for sys_finit_module: */
#define MODULE_INIT_IGNORE_MODVERSIONS 1
#define MODULE_INIT_IGNORE_VERMAGIC 2
#endif /* _LINUX_MODULE_H */
My linux/kernel.h
:
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _LINUX_KERNEL_H
#define _LINUX_KERNEL_H
#include <linux/sysinfo.h>
#include <linux/const.h>
#endif /* _LINUX_KERNEL_H */
These headers are kernel-space headers, which means they cannot be found within the user-space headers (
/usr/include/linux
). In my Fedora machine, this is how I diagnose it.1. Make sure those headers exist
I use a simple
find /usr -name uaccess.h 2>/dev/null
to list all files. The result might look like below:2. How to use those kernel headers
An intuitive way to use those headers is to include them all when compiling. However, this is error-prone due to linking issues.
A (somewhat) standard way is to define it as module.
Source code:
Makefile:
You might search for the rest on running kernel modules for your machine.
I hope this will help.