gpt4 book ai didi

c - Linux内核模块编译

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:17 25 4
gpt4 key购买 nike

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

关于c - Linux内核模块编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com