内核模块编译错误:函数声明不是原型[-Werror = strict-prototypes]

时间:2022-04-09 18:49:38

this code: Linux kernel module that directly controls the LEDs of your PS/2 keyboard (Num Lock, Caps Lock, and Scroll Lock)

此代码:Linux内核模块,可直接控制PS / 2键盘的LED(Num Lock,Caps Lock和Scroll Lock)

#include <linux/module.h>
#include <linux/kernel.h>

int check_bit(unsigned char status, int i) {
    return (status & (1<<(i)));
}

unsigned char kbd_read_status() {  // <== ERROR first appears on this line
    return inb(0x64);
}

unsigned char kbd_read_data() {
    unsigned char status;   
    do
    {
        status = kbd_read_status();
    }
    while(!check_bit(status, 0));
    return inb(0x60);
}

when run makefile :

运行makefile时:

make -C /lib/modules/3.19.0-15-generic/build M=/home/fyousry/Desktop/hellokernel modules
make[1]: Entering directory '/usr/src/linux-headers-3.19.0-15-generic'
  CC [M]  /home/fyousry/Desktop/hellokernel/New.o
/home/fyousry/Desktop/hellokernel/New.c:9:15: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 unsigned char kbd_read_status() {
               ^
/home/fyousry/Desktop/hellokernel/New.c:13:15: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 unsigned char kbd_read_data() {
               ^
cc1: some warnings being treated as errors
scripts/Makefile.build:263: recipe for target '/home/fyousry/Desktop/hellokernel/New.o' failed
make[2]: *** [/home/fyousry/Desktop/hellokernel/New.o] Error 1
Makefile:1394: recipe for target '_module_/home/fyousry/Desktop/hellokernel' failed
make[1]: *** [_module_/home/fyousry/Desktop/hellokernel] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-3.19.0-15-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2

when add #include 'sys/io.h' this error show

当添加#include'sys / io.h'这个错误显示

make -C /lib/modules/3.19.0-15-generic/build M=/home/fyousry/Desktop/hellokernel modules
make[1]: Entering directory '/usr/src/linux-headers-3.19.0-15-generic'
  CC [M]  /home/fyousry/Desktop/hellokernel/New.o
/home/fyousry/Desktop/hellokernel/New.c:3:21: fatal error: sys/uio.h: No such file or directory
 #include <sys/io.h>
                     ^
compilation terminated.
scripts/Makefile.build:263: recipe for target '/home/fyousry/Desktop/hellokernel/New.o' failed
make[2]: *** [/home/fyousry/Desktop/hellokernel/New.o] Error 1
Makefile:1394: recipe for target '_module_/home/fyousry/Desktop/hellokernel' failed
make[1]: *** [_module_/home/fyousry/Desktop/hellokernel] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-3.19.0-15-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2

1 个解决方案

#1


11  

For a proper prototype, you cannot leave your parameter list empty.
If your function does not take any parameters, you must specify that it is (void).

对于正确的原型,您不能将参数列表留空。如果您的函数不接受任何参数,则必须指定它是(void)。

kbd_read_status(void) instead of kbd_read_status( )

kbd_read_status(void)而不是kbd_read_status()

Kernel code is much more pedantic about this.
In other code, you can get away with empty parameters, but not under the most strict rules.

内核代码对此更加迂腐。在其他代码中,您可以使用空参数,但不能遵循最严格的规则。

#1


11  

For a proper prototype, you cannot leave your parameter list empty.
If your function does not take any parameters, you must specify that it is (void).

对于正确的原型,您不能将参数列表留空。如果您的函数不接受任何参数,则必须指定它是(void)。

kbd_read_status(void) instead of kbd_read_status( )

kbd_read_status(void)而不是kbd_read_status()

Kernel code is much more pedantic about this.
In other code, you can get away with empty parameters, but not under the most strict rules.

内核代码对此更加迂腐。在其他代码中,您可以使用空参数,但不能遵循最严格的规则。