如何正确使用内核头?

如何正确使用内核头?

我写了一个非常简单的内核编程源代码:

#include <linux/kernel.h>
#include <linux/fcntl.h>
#include <linux/init.h>

static int __init(void){
    sys_open("ee.c", O_RDONLY);

但由于内核源代码(现在真正执行的)位于主文件夹,因此发生了以下错误。即使我创建了这样的符号链接。

rm -rf /usr/include/linux
rm -rf /usr/include/asm

ln -s /home/linux-2.6.21/include/linux /usr/include/linux
ln -s /home/linux-2.6.21/include/asm-i386 /usr/include/asm

 

In file included from /usr/include/asm/fcntl.h:1,  
             from /usr/include/linux/fcntl.h:4,  
             from v.c:2:  
/usr/include/asm-generic/fcntl.h:120: error: expected specifier-qualifier-list before ‘off_t’  
/usr/include/asm-generic/fcntl.h:143: error: expected specifier-qualifier-list before ‘loff_t’  

在使用不同文件夹的内核头之前我需要做什么,/usr/include为什么会出现这种类型的错误?

我也做了

rm -rf /usr/include/asm
ln -s /home/linux-2.6.21/include/asm-i386 /usr/include/asm

#include <linux/syscall.h> 被卡住了……
那是完整的错误消息……我认为内核头文件特别错误……

In file included from /usr/include/linux/syscalls.h:61,
                 from vv.c:4:
/usr/include/linux/list.h:959:2: warning: #warning "don't include kernel headers in userspace"
In file included from /usr/include/asm/siginfo.h:4,
                 from /usr/include/linux/syscalls.h:64,
                 from vv.c:4:
/usr/include/asm-generic/siginfo.h:75: error: expected specifier-qualifier-list before ‘clock_t’
In file included from /usr/include/asm/signal.h:5,
                 from /usr/include/linux/syscalls.h:65,
                 from vv.c:4:
/usr/include/linux/time.h:12: error: redefinition of ‘struct timespec’
/usr/include/linux/time.h:18: error: redefinition of ‘struct timeval’
In file included from /usr/include/linux/syscalls.h:65,
                 from vv.c:4:
/usr/include/asm/signal.h:32: error: conflicting types for ‘sigset_t’
/usr/include/sys/select.h:38: error: previous declaration of ‘sigset_t’ was here
In file included from /usr/include/linux/syscalls.h:66,
                 from vv.c:4:
/usr/include/linux/quota.h:47: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘dq_data_lock’
In file included from /usr/include/asm/processor.h:17,
                 from /usr/include/asm/atomic.h:5,
                 from /usr/include/linux/key.h:22,
                 from /usr/include/linux/syscalls.h:67,
                 from vv.c:4:
/usr/include/asm/msr.h:90: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h:90: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h:90: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h: In function ‘rdmsr_on_cpu’:
/usr/include/asm/msr.h:92: error: ‘l’ undeclared (first use in this function)
/usr/include/asm/msr.h:92: error: (Each undeclared identifier is reported only once
/usr/include/asm/msr.h:92: error: for each function it appears in.)
/usr/include/asm/msr.h:92: error: ‘h’ undeclared (first use in this function)
/usr/include/asm/msr.h:92: error: ‘msr_no’ undeclared (first use in this function)
/usr/include/asm/msr.h:92: error: invalid lvalue in asm output 0
/usr/include/asm/msr.h:92: error: invalid lvalue in asm output 1
/usr/include/asm/msr.h: At top level:
/usr/include/asm/msr.h:94: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h:94: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h:94: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/msr.h: In function ‘wrmsr_on_cpu’:
/usr/include/asm/msr.h:96: error: ‘msr_no’ undeclared (first use in this function)
/usr/include/asm/msr.h:96: error: ‘l’ undeclared (first use in this function)
/usr/include/asm/msr.h:96: error: ‘h’ undeclared (first use in this function)
In file included from /usr/include/asm/processor.h:18,
                 from /usr/include/asm/atomic.h:5,
                 from /usr/include/linux/key.h:22,
                 from /usr/include/linux/syscalls.h:67,
                 from vv.c:4:
/usr/include/asm/system.h: At top level:
/usr/include/asm/system.h:346: error: expected declaration specifiers or ‘...’ before ‘u8’
/usr/include/asm/system.h:346: error: expected declaration specifiers or ‘...’ before ‘u8’
/usr/include/asm/system.h:347: error: expected declaration specifiers or ‘...’ before ‘u16’
/usr/include/asm/system.h:347: error: expected declaration specifiers or ‘...’ before ‘u16’
/usr/include/asm/system.h:348: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/system.h:348: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/asm/system.h: In function ‘cmpxchg_386’:
/usr/include/asm/system.h:355: error: too many arguments to function ‘cmpxchg_386_u8’
/usr/include/asm/system.h:357: error: too many arguments to function ‘cmpxchg_386_u16’
/usr/include/asm/system.h:359: error: too many arguments to function ‘cmpxchg_386_u32’
In file included from /usr/include/linux/cpumask.h:86,
                 from /usr/include/asm/processor.h:22,
                 from /usr/include/asm/atomic.h:5,
                 from /usr/include/linux/key.h:22,
                 from /usr/include/linux/syscalls.h:67,
                 from vv.c:4:
/usr/include/linux/bitmap.h: In function ‘bitmap_zero’:
/usr/include/linux/bitmap.h:134: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_fill’:
/usr/include/linux/bitmap.h:149: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_copy’:
/usr/include/linux/bitmap.h:155: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_and’:
/usr/include/linux/bitmap.h:166: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_or’:
/usr/include/linux/bitmap.h:175: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_xor’:
/usr/include/linux/bitmap.h:184: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_andnot’:
/usr/include/linux/bitmap.h:193: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_complement’:
/usr/include/linux/bitmap.h:202: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_equal’:
/usr/include/linux/bitmap.h:211: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_intersects’:
/usr/include/linux/bitmap.h:220: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_subset’:
/usr/include/linux/bitmap.h:229: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_empty’:
/usr/include/linux/bitmap.h:237: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_full’:
/usr/include/linux/bitmap.h:245: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_weight’:
/usr/include/linux/bitmap.h:253: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_shift_right’:
/usr/include/linux/bitmap.h:261: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
/usr/include/linux/bitmap.h: In function ‘bitmap_shift_left’:
/usr/include/linux/bitmap.h:270: error: ‘BITS_PER_LONG’ undeclared (first use in this function)
In file included from /usr/include/asm/processor.h:22,
                 from /usr/include/asm/atomic.h:5,
                 from /usr/include/linux/key.h:22,
                 from /usr/include/linux/syscalls.h:67,
                 from vv.c:4:
/usr/include/linux/cpumask.h: At top level:
/usr/include/linux/cpumask.h:88: error: expected specifier-qualifier-list before ‘DECLARE_BITMAP’
/usr/include/linux/cpumask.h: In function ‘__cpu_set’:
/usr/include/linux/cpumask.h:94: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpu_clear’:
/usr/include/linux/cpumask.h:100: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_setall’:
/usr/include/linux/cpumask.h:106: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_clear’:
/usr/include/linux/cpumask.h:112: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpu_test_and_set’:
/usr/include/linux/cpumask.h:121: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_and’:
/usr/include/linux/cpumask.h:128: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:128: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:128: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_or’:
/usr/include/linux/cpumask.h:135: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:135: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:135: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_xor’:
/usr/include/linux/cpumask.h:142: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:142: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:142: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_andnot’:
/usr/include/linux/cpumask.h:150: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:150: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:150: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_complement’:
/usr/include/linux/cpumask.h:157: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:157: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_equal’:
/usr/include/linux/cpumask.h:164: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:164: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_intersects’:
/usr/include/linux/cpumask.h:171: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:171: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_subset’:
/usr/include/linux/cpumask.h:178: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:178: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_empty’:
/usr/include/linux/cpumask.h:184: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_full’:
/usr/include/linux/cpumask.h:190: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_weight’:
/usr/include/linux/cpumask.h:196: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_shift_right’:
/usr/include/linux/cpumask.h:204: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:204: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_shift_left’:
/usr/include/linux/cpumask.h:212: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:212: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpumask_scnprintf’:
/usr/include/linux/cpumask.h:273: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpumask_parse_user’:
/usr/include/linux/cpumask.h:281: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpulist_scnprintf’:
/usr/include/linux/cpumask.h:289: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpulist_parse’:
/usr/include/linux/cpumask.h:295: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpu_remap’:
/usr/include/linux/cpumask.h:303: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:303: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h: In function ‘__cpus_remap’:
/usr/include/linux/cpumask.h:311: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:311: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:311: error: ‘cpumask_t’ has no member named ‘bits’
/usr/include/linux/cpumask.h:311: error: ‘cpumask_t’ has no member named ‘bits’
In file included from /usr/include/asm/atomic.h:5,
                 from /usr/include/linux/key.h:22,
                 from /usr/include/linux/syscalls.h:67,
                 from vv.c:4:
/usr/include/asm/processor.h: At top level:
/usr/include/asm/processor.h:82: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
/usr/include/asm/processor.h:82: error: requested alignment is not a constant
In file included from vv.c:4:
/usr/include/linux/syscalls.h:114: error: expected ‘)’ before ‘*’ token
/usr/include/linux/syscalls.h:115: error: expected declaration specifiers or ‘...’ before ‘old_sigset_t’
/usr/include/linux/syscalls.h:116: error: expected declaration specifiers or ‘...’ before ‘old_sigset_t’
/usr/include/linux/syscalls.h:178: error: expected ‘)’ before ‘*’ token
/usr/include/linux/syscalls.h:394: error: expected ‘)’ before ‘cookie64’
/usr/include/linux/syscalls.h:474: error: expected ‘)’ before ‘mqdes’
/usr/include/linux/syscalls.h:475: error: expected ‘)’ before ‘mqdes’
/usr/include/linux/syscalls.h:476: error: expected ‘)’ before ‘mqdes’
/usr/include/linux/syscalls.h:477: error: expected ‘)’ before ‘mqdes’
/usr/include/linux/syscalls.h:507: error: expected declaration specifiers or ‘...’ before ‘key_serial_t’
/usr/include/linux/syscalls.h:512: error: expected declaration specifiers or ‘...’ before ‘key_serial_t’
/usr/include/linux/syscalls.h:546: error: expected declaration specifiers or ‘...’ before ‘u32’
/usr/include/linux/syscalls.h:547: error: expected declaration specifiers or ‘...’ before ‘u32’
vv.c: In function ‘init’:
vv.c:7: error: too few arguments to function ‘sys_open’

答案1

这些asm-generic标头也来自 Linux 内核,因此您实际上将一些经过清理的标头/usr/include与来自主目录中的源的一些标头混合在一起。

如果您正在编写用户空间程序,那么您也可以只使用发行版附带的已清理标头:它们缺少的唯一信息是编译要在内核中运行的代码所需的部分。

要恢复您删除的文件,请删除您创建的两个符号链接,然后运行apt-get install --reinstall linux-libc-dev

或者,如果您需要为该特定内核版本使用未清理的内核头,只需将其传递-I/home/linux-2.6.21/include给编译器,它就会优先选择那些头,而不是系统安装的头。

相关内容