繁体   English   中英

Linux内核模块编译

[英]Linux Kernel Module Compilation

我在构建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 

正确的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM