简体   繁体   中英

Is it possible to develop linux kernel module in CLion?

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.

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 .

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM