Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在linux中使用makefile编译内核模块_C_Linux_Makefile_Linux Kernel_Kernel Module - Fatal编程技术网

C 在linux中使用makefile编译内核模块

C 在linux中使用makefile编译内核模块,c,linux,makefile,linux-kernel,kernel-module,C,Linux,Makefile,Linux Kernel,Kernel Module,我是内核编程新手。我已经写了一个hello world程序,但我无法完成它。我在/usr/src下创建了一个make文件,然后执行sudomake命令来运行它。但它给出了以下错误: make -C /lib/modules/3.2.0-23-generic-pae/build M=/usr/src modules make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae' make[2]: *** No ru

我是内核编程新手。我已经写了一个hello world程序,但我无法完成它。我在/usr/src下创建了一个make文件,然后执行sudomake命令来运行它。但它给出了以下错误:

make -C /lib/modules/3.2.0-23-generic-pae/build M=/usr/src modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make[2]: *** No rule to make target `/usr/src/hello.c', needed by  `/usr/src   /hello.o'.  Stop.
make[1]: *** [_module_/usr/src] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make: *** [all] Error 2
生成文件:

obj-m += hello.o

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

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
hello.c在~Desktop/inet中/

#include <linux/module.h>   
#include <linux/kernel.h>   
int init_module(void)
{
   printk(KERN_INFO "Hello world 1.\n");

 /* 
 * A non 0 return means init_module failed; module can't be loaded. 
 */
  return 0;
}

void cleanup_module(void)
{
  printk(KERN_INFO "Goodbye world 1.\n");
}
在makefile中,我注意到每一行只有一个选项卡空间

请告诉我这个问题

将Makefile移动到模块源hello.c所在的位置。这意味着在~/Desktop/inet/中。看起来您已经将Makefile放在/usr/src中了/


只需使用make而不是sudo make

格式太糟糕了。请正确设置代码格式并输出消息。是否正在运行make from/usr/src?您的源文件位于$HOME/Desktop/inet中?错误是因为make不知道如何构建一个不存在的文件/usr/src/hello.c,因为您的文件不在那里。我对内核构建系统的了解还不够,不知道这里有什么正确的修复方法,但这就是问题所在。您的Makefile应该是~Desktop/inet/而不是/usr/src。如果我的内存正确,您需要将init_模块声明为u属性_u构造函数。类似于作为析构函数的cleanup_模块。
$ sudo mv /usr/src/Makefile ~/Desktop/inet/
$ cd ~/Desktop/inet/
$ make