Linux 内核模块编译

Linux Kernel Module Compilation

我在构建 helloworld Linux 内核模块时遇到问题。我正在使用来自 SUN 的 VirtualBox 以及从 Ubuntu 网站下载的 Ubuntu ISO 映像。任何帮助将不胜感激。下面是 C 代码和我收到的错误消息:

模块文件名为 hellowrld.c,它包含以下代码:

    #include <linux/module.h>    // included for all kernel modules
    #include <linux/kernel.h>    // included for KERN_INFO
    #include <linux/init.h>      // included for __init and __exit macros

    MODULE_LICENSE("GPL");

    static int __init helloworld_init(void)
    {
        printk(KERN_INFO "Hello world!\n");
        return 0;    
    }

    static void __exit helloworld_exit(void)
    {
        printk(KERN_INFO "Cleaning up module.\n");
    }

    module_init(helloworld_init);
    module_exit(helloworld_exit); 

make 文件名为 makefile.c,它包含以下代码:

    obj -m += helloworld.o

    all:
         make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

    clean:
          make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我在 运行 make 命令时收到的错误消息如下:

cc    makefile.c -o makefile
makefile.c:1:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '-' token 
obj-m helloworld.o

make: *** No targets specified no makefile found. Stop 

正确的 Makefile 如下所示...

obj-m    := helloworld.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
    rm -rf *.o *.ko *.mod.* *.symvers *.order