gpt4 book ai didi

c - 使用 linux 内核中定义的结构时出错

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

我在 linux 内核的 /usr/src/linux-3.2/include/linux/unistd.h 中定义了一个结构:

#ifndef _LINUX_UNISTD_H_
#define _LINUX_UNISTD_H_

struct threadinfo_struct {
int pid;
int nthreads;
int *tid;
};

/*
* Include machine specific syscall numbers
*/
#include <asm/unistd.h>

#endif /* _LINUX_UNISTD_H_ */

编译并安装内核后,从中启动,我尝试编译并运行这个程序:

#include <stdio.h>
#include <linux/unistd.h>
int main(void) {
struct threadinfo_struct *ti = (struct threadinfo_struct*) malloc(sizeof(struct threadinfo_struct));
// ...
return 0;
}

但是,当我尝试这样做时,我在编译程序时遇到错误:

test.c: In function 'main':
test.c:4:78: error: invalid application of 'sizeof' to incomplete type 'struct threadinfo_struct'

为什么会出现此错误,我该如何解决?考虑到我对 linux 内核还很陌生,这对我来说很难找到很多信息。

最佳答案

文件 /usr/src/linux-3.2/include/linux/unistd.h 不在标准包含路径中。

用户空间应用程序有自己的构建环境。您包含位于 /usr/include/linux/unistd.h 的文件。大多数内部内核结构都没有为用户空间应用程序定义。

如果你真的需要定义这个结构,那么你需要将文件从 linux 树复制到你的项目目录,或者通过添加 -isystem/usr/src/linux-3.2/调整 gcc 命令包含/ 选项。

但是,后者会造成很大的困惑,所以最好只复制文件。

关于c - 使用 linux 内核中定义的结构时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001700/

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