2

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

#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 内核非常陌生,这对我来说很难找到很多信息。

4

1 回答 1

1

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

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

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

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

于 2012-10-21T20:14:28.113 回答