Is it possible to develop linux kernel module in CLion?
Asked Answered
C

3

13

I want to develop some small linux kernel modules in CLion. For example, I want to compile these files:

stack.h:

#ifndef _LL_STACK_H
#define _LL_STACK_H
#include <linux/list.h>

typedef struct stack_entry {
    struct list_head lh;
    void *data;
} stack_entry_t;

stack_entry_t* create_stack_entry(void *data);
void delete_stack_entry(stack_entry_t *entry);

void stack_push(struct list_head *stack, stack_entry_t *entry);
stack_entry_t* stack_pop(struct list_head *stack);
#define stack_empty(stack) list_empty((stack))

#define STACK_DATA(stack_entry, data_ptr_type) \
    ((data_ptr_type)(stack_entry)->data)

#define STACK_DATA_RESET(stack_entry, new_data) \
    do { \
        (stack_entry)->data = new_data; \
    } while(0)

#endif //_LL_STACK_H

main.c:

#include <stdio.h>
#include "stack.h"

int main() {
    printf("hello");
    return 0;
}

Is it possible to configure CMakeLists.txt to complete my task? I try to add some directories (linux, include, kernel), but I have no success.

Cacodemon answered 23/2, 2015 at 15:16 Comment(2)
Have you tried to ask CLion developers? Moreover, your main.c doesn't belong to kernel programming.Jonijonie
@AndyShevchenko, not yet. My main.c is example file, which include stack.h, which depends on <linux/list.h>.Cacodemon
B
6

Yes, It is. But you will need to write make file for building kernel module.

Update 1: I recommend QtCreator for writing linux kernel module. See my manual

Update 2: I also recommend eclipse cdt. See eclipse manual about how to prepare it for linux kernel.

Bagworm answered 24/2, 2015 at 8:55 Comment(3)
thank you for your link. I have Makefile, which can build my modules. But CLion does't support Makesfiles. Therefore, I try to developer equivalent cMake file, but I have no succes.Cacodemon
As I understood you want to use CLion as text editor, which must parse all linux headers. You should add to the cmake all include paths of linux-headers(like as on my link in qtcreator). I found link of cmake function which must writes to variable all include paths(but it doesn't work correctly, I think that you can use this function like as template for writing self function which will get all include paths ). Just search in google how to add add recursively all include paths in cmake.Bagworm
@VadimStupakiv, thank you for response. I already use QT IDE. It is OK too.Cacodemon
M
2

If you are only talking about proper auto-suggestions but are fine with invoking "make" by yourself then check this demo setup: https://gitlab.com/phip1611/cmake-kernel-module It's a simplified version of https://gitlab.com/christophacham/cmake-kernel-module/-/blob/master/CMakeLists.txt (I forked it).

I use it for both: out-of-tree kernel module development (standalone development) as well as in-tree development.

Always make sure to have latest kernel header files installed! $ sudo apt install kernel-headers-$(uname -r)

Megan answered 23/10, 2020 at 21:31 Comment(0)
W
1

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

Whittle answered 8/11, 2017 at 14:27 Comment(2)
CLion is as of some years ago already able to read compile_commands.json files.Yung
kernel module development isn't kernel developmentEvangelineevangelism

© 2022 - 2024 — McMap. All rights reserved.