我是linux的新手 . 我正在练习系统调用,我想将struct复制到用户 .

所以我写了一个使用copy_to_user()的系统调用 .

但是当我编写和编译test.c来测试我的系统调用时出现了一些错误 .

在linux / sched.h中定义了:

struct pacct_struct {
    int ac_flag;
    long ac_exitcode;
    unsigned long ac_mem;
    cputime_t ac_utime, ac_stime;
    unsigned long ac_minflt, ac_majflt;
};

然后我编写了一个使用这个结构的程序(test.c)

#include "linux/sched.h"
#include <stdlib.h>
int main(){
    struct pacct_struct *ts;
    ts = (struct pacct_struct *)malloc(sizeof(struct pacct_struct));
    return 0;
}

和gcc显示以下错误消息:

test.c:6:44: error: invalid application of 'sizeof' to incomplete type 'struct pacct_struct'

我想知道是否可以通过include头文件使用内核结构 . 如果是这样,我会错过什么吗?如何让pacct_struct成为'完整类型'?

谢谢 .

addition

我用gcc -E检查预处理器

我发现似乎没有包含“struct pacct_struct” .

接下来,我将查看包含的sched.h,它是在编译内核后由“make headers_install”创建的 .

该文件只包含一些克隆标志,如“#define CSIGNAL 0x000000ff”

因此,我尝试在dir“/usr/src/linux-2.6.39.4/include/linux”中包含sched.h的原始源文件,但它继续显示相同的错误“不完整类型” .

然后我再次检查预处理器 .

即使我包含原始头文件,我仍然找不到'struct pacct_struct'

#ifdef __KERNEL__ 之后sched.h中的任何内容都消失了,发生了什么?