gpt4 book ai didi

c - fatal error : linux/kthread. h:没有这样的文件或目录编译终止

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:06 25 4
gpt4 key购买 nike

我正在尝试熟悉 kthreads 并编写了一个非常简单的程序来用 C 语言测试它,指导来自:http://tuxthink.blogspot.com/2011/02/kernel-thread-creation-1.html .我在 MacOSX 上的 VMware 中运行 Ubuntu。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "cycle.h"
#include <linux/kthread.h>
#include <linux/sched.h>

int main(){
static struct task_struct *kthread;

thread1 = kthread_create(thread_fn, NULL, "thread1");
wake_up_process(thread1);
kthread_stop(thread1);

return 0;
}

当我尝试使用 gcc (gcc test5.c -o test5.out) 编译它时,我得到“ fatal error :linux/kthread.h:没有这样的文件或目录编译终止。”

当我去查看/usr/include/linux/时,没有 kthread.h 文件,所以这似乎是合理的。当我搜索 kthread.h 时,我在/usr/src/linux-headers-3.2.0-31/include/linux 中找到了一个,在/usr/src/linux-headers-3.2.0-29/include/linux 中找到了一个,但是在我尝试将其中一个复制到/usr/include/linux/后,我不断收到错误消息:

In file included from /usr/include/linux/kthread.h:4:0,
from test5.c:5:
/usr/include/linux/err.h:22:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ERR_PTR’
/usr/include/linux/err.h:27:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PTR_ERR’
/usr/include/linux/err.h:32:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘IS_ERR’
/usr/include/linux/err.h:37:33: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘IS_ERR_OR_NULL’
/usr/include/linux/err.h:49:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ERR_CAST’
/usr/include/linux/err.h:55:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PTR_RET’
In file included from test5.c:5:0:
/usr/include/linux/kthread.h:7:10: error: expected declaration specifiers or ‘...’ before numeric constant
/usr/include/linux/kthread.h:7:13: error: expected declaration specifiers or ‘...’ before numeric constant
/usr/include/linux/kthread.h:58:2: error: unknown type name ‘spinlock_t’
/usr/include/linux/kthread.h:59:19: error: field ‘work_list’ has incomplete type
/usr/include/linux/kthread.h:64:19: error: field ‘node’ has incomplete type
/usr/include/linux/kthread.h:66:2: error: unknown type name ‘wait_queue_head_t’
/usr/include/linux/kthread.h:67:2: error: unknown type name ‘atomic_t’
/usr/include/linux/kthread.h:128:1: error: unknown type name ‘bool’
test5.c: In function ‘main’:
test5.c:11:2: error: ‘thread1’ undeclared (first use in this function)
test5.c:11:2: note: each undeclared identifier is reported only once for each function it appears in
test5.c:11:12: error: ‘thread_fn’ undeclared (first use in this function)

任何有关如何解决此问题的想法都将不胜感激!

最佳答案

这些是内核线程,用于内核空间,而不是用户空间线程!您应该使用适当的 Makefile 将代码更改为内核模块或使用 pthreads对于用户空间线程。也许你应该从 HelloWorld 开始内核模块

关于c - fatal error : linux/kthread. h:没有这样的文件或目录编译终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13129492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com