【问题标题】:Is it possible to develop linux kernel module in CLion?是否可以在 CLion 中开发 linux 内核模块?
【发布时间】:2015-04-24 23:11:22
【问题描述】:

我想在 CLion 中开发一些小的 linux 内核模块。比如我要编译这些文件:

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

ma​​in.c:

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

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

是否可以配置 CMakeLists.txt 来完成我的任务?我尝试add一些目录(linux、include、kernel),但没有成功。

【问题讨论】:

  • 您是否尝试过询问 CLion 开发人员?而且,你的main.c不属于内核编程。
  • @AndyShevchenko,还没有。我的main.c 是示例文件,其中包括stack.h,它依赖于&lt;linux/list.h&gt;

标签: c linux linux-kernel cmake clion


【解决方案1】:

是的,是的。但是你需要编写 make 文件来构建内核模块。

更新 1: 我推荐 QtCreator 来编写 linux 内核模块。 见我的manual

更新 2: 我也推荐eclipse cdt。 见日食manual about how to prepare it for linux kernel

【讨论】:

  • 感谢您的链接。我有 Makefile,它可以构建我的模块。但是CLion 不支持 Makesfiles。因此,我尝试开发等效的cMake 文件,但没有成功。
  • 据我了解,您想使用 CLion 作为文本编辑器,它必须解析所有 linux 标头。您应该将所有包含 linux-headers 的路径添加到 cmake 中(就像我在 qtcreator 中的链接一样)。我发现 cmake 函数的 link 必须写入变量所有包含路径(但它不能正常工作,我认为您可以使用此函数作为模板来编写将获取所有包含路径的 self 函数)。只需在谷歌中搜索如何在 cmake 中递归添加所有包含路径。
  • @VadimStupakiv,感谢您的回复。我已经使用 QT IDE。也可以。
【解决方案2】:

我通过 clion 探索 linux 内核的方法是:

  • 使用拦截的构建为内核创建compile_commands.json
  • 使用 ruby​​ 脚本将 compile_commands.json 转换为对 clion 友好的 CMakeLists.txt

这允许代码导航和合理的编辑体验。

更多详情请看https://github.com/habemus-papadum/kernel-grok

【讨论】:

  • CLion 早在几年前就已经能够读取 compile_commands.json 文件了。
  • 内核模块开发不是内核开发
【解决方案3】:

如果您只是在谈论正确的自动建议,但可以自己调用“make”,请查看此演示设置:https://gitlab.com/phip1611/cmake-kernel-module 这是https://gitlab.com/christophacham/cmake-kernel-module/-/blob/master/CMakeLists.txt 的简化版(我分叉了它)。

我将它用于以下两种情况:树外内核模块开发(独立开发)和树内开发。

始终确保安装了最新的内核头文件! $ sudo apt install kernel-headers-$(uname -r)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多