在makefile中写入:
ifneq ($(KERNELRELEASE),)
MODULE_NAME = hellomodule
$(MODULE_NAME)-objs := hello.o
obj-m := $(MODULE_NAME).o
else
KERNEL_DIR = /lib/modules/`uname -r`/build
MODULEDIR := $(shell pwd)
.PHONY: modules
default: modules
modules:
make -C $(KERNEL_DIR) M=$(MODULEDIR) modules
clean distclean:
rm -f *.o *.mod.c .*.*.cmd *.ko
rm -rf .tmp_versions
endif
hechao@ubuntu:/opt/driver$ make
Makefile:16: *** missing separator. Stop.
makefile的命令行,开头必须用tab键。将
modules:
make -C $(KERNEL_DIR) M=$(MODULEDIR) modules
clean distclean:
rm -f *.o *.mod.c .*.*.cmd *.ko
rm -rf .tmp_versions
里面的命令行左顶格,再用tab键开头。
重新执行 make
hechao@ubuntu:/opt/driver$ make
make -C /lib/modules/`uname -r`/build M=/opt/driver modules
make[1]: Entering directory `/usr/src/linux-headers-4.4.0-131-generic'
Makefile:697: Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler
mkdir: cannot create directory ‘/opt/driver/.tmp_versions’: Permission denied
CC [M] /opt/driver/hello.o
Assembler messages:
Fatal error: can't create /opt/driver/.tmp_hello.o: Permission denied
In file included from include/linux/kobject.h:21:0,
from include/linux/module.h:17,
from /opt/driver/hello.c:1:
include/linux/sysfs.h: In function ‘sysfs_get_dirent’:
include/linux/sysfs.h:523:2: warning: pointer targets in passing argument 2 of ‘kernfs_find_and_get’ differ in signedness [-Wpointer-sign]
return kernfs_find_and_get(parent, name);
^
In file included from include/linux/sysfs.h:15:0,
from include/linux/kobject.h:21,
from include/linux/module.h:17,
from /opt/driver/hello.c:1:
include/linux/kernfs.h:433:1: note: expected ‘const char *’ but argument is of type ‘const unsigned char *’
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
^
make[2]: *** [/opt/driver/hello.o] Error 2
make[1]: *** [_module_/opt/driver] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-4.4.0-131-generic'
make: *** [modules] Error 2
执行 sudo make 就好了。