Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Linux kernel module on LSP (clangd)

Tags:

linux

clangd

I have tried develop Linux kernel module on vim with LSP(clangd)
But bunch of warning and errors are appeared.

It may caused by lack of clangd some configurations.

What header files and compiler parameters are required to use clangd with kernel module sources?

My compile_flags.txt is just one line but I know it's not enough:

-I/usr/lib/modules/5.19.3-arch1-1/build/include
like image 817
KiYugadgeter Avatar asked Oct 27 '25 08:10

KiYugadgeter


1 Answers

The general recommendations around configuring projects for use with clangd are:

  1. Prefer compile_commands.json to compile_flags.txt.

    compile_flags.txt has many limitations (for example, it does not allow clangd to index the project) and is really only suitable for toy projects.

  2. Do not hand-write your compile_commands.json, generate it automatically from build system metadata.

    The way to do this is somewhat specific to your build system, but a common and versatile approach is to use bear, which wraps your build command (e.g. if you build with make, you'd run bear make), and generates compile_commands.json based on the compiler invocations made by the build command.

    See https://clangd.llvm.org/installation#project-setup for more details and more options.

I don't have specific advice for the Linux kernel; maybe others can chime in on that.

like image 65
HighCommander4 Avatar answered Oct 28 '25 21:10

HighCommander4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!