Linux内核模块编译

时间:2015-11-08 00:04:47

标签: c linux makefile linux-kernel

我在构建helloworld Linux内核模块时遇到了问题。我使用SUN的VirtualBox和我从Ubuntu网站下载的Ubuntu ISO映像。任何帮助将不胜感激。 Bellow是我得到的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 

1 个答案:

答案 0 :(得分:3)

正确的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
相关问题