Developing Linux kernel module with CLion

已完成

I'm trying to write a Linux kernel module in CLion. The problem I'm having is that I can't seem to get code insight to recognize any of the definitions from the Linux headers.

I realize CMake can't build it itself and I have to use kbuild. I created a dummy target for CMake just so CLion could identify my source files. I found all the include paths and command-line defines that kbuild is using and tried to add them to the project with include_directories and add_definitions, respectively. CMake can successfully build my dummy target (which is just compiling my source files to object files). CLion shows the correct directories under External Libraries > Header Search Paths in the Project tool window, but the code insight doesn't appear to recognize any definitions in the Linux headers. Some symbols get a red squiggly but can be jumped to with Ctrl+Click, but others are completely unrecognized.

I tried specifying explicit include directories rather than using execute_command to fetch to kernel version etc. and fill that in via CMake variable substitution. That had no effect.

I also tried adding the headers I used to the list of source files for the project, and that didn't help, either.

The only thing close to a solution I've found is putting the full paths to the headers in my source file. That enables CLion to recognize the definitions from those headers (but not from the headers they include). This really isn't a solution, but I think it might indicate that CLion isn't using the right search path for includes.

Anyone have any ideas/suggestions?

5
Avatar
Permanently deleted user

The approach I use to spelunk the linux kernel via clion is:

* create a `compile_commands.json` for the kernel using an intercepted build
* use a ruby script to convert `compile_commands.json` into an clion friendly `CMakeLists.txt`

This allows for both code navigation and also a reasonable editing experience.

See for more details https://github.com/habemus-papadum/kernel-grok

 

A variation of this should probably get you going with your module (and will work easily if you are developing the module in tree)

2

We really need this feature, and I wish to see that we can use CLion to develop linux kernel module some day!

:-)

1

Me too. I just started a new project to modify a kernel module that will build correctly from bash but fails to build in CLION with the following error,

error: unknown type name '__u32'

I've added this to `CMakeLists.txt`

include_directories(/usr/src/linux-headers-4.15.0-96-generic/include)
include_directories(/usr/src/linux-headers-4.15.0-96/arch/ia64/include)

to solve other issues but I can't seem to solve this issue.

2

请先登录再写评论。