当前位置:首页 » 编程软件 » linux模块编译

linux模块编译

发布时间: 2022-01-10 15:38:45

linux编译内核和编译内核模块有什么区别

当然需要。。。

第一点,就是源码树中有相应的头文件和函数的实现,没有源码树,你哪调用去呢?(PC上编译的时候内核有导出符号,系统中有头文件,这样就可以引用内核给你的接口了,但是只能编译你PC上版本的内核可加载的模块)。

第二个,内核模块中会记录版本号的部分,需要记录版本号的原因是不同的内核版本之间,那些接口和调用可能会有比较大的差异,因此必须要保证你的代码和某个特定的内核对应,这样编译出来的模块就可以(也是只能)在运行这个内核版本的Linux系统中加载,否则一个很简单的异常就会导致内核崩溃,或者你的代码根本无法编译通过(接口名变了)。

我上面说的是编译模块的情况,当然如果是把模块直接编译到内核当中去的话,那就不用说了,没有内核源码,你无法编译内核。

② linux编译模块的内核版本和现在使用的内核版本不一致的话,怎么将现在使用的内核版本配成编译所要的

修改Makefile中的KDIR参数,你现在的Makefile是怎样写的?
参考Makefile:
obj-m := moles.o
moles-objs := mymod.o

KDIR=/lib/moles/`uname -r`/build
PWD =$(shell pwd)

default:
make -C $(KDIR) M=$(PWD) moles

clean:
rm -rf *.o .* .cmd *.ko *.mod.c .tmp_version

③ 如何编译/交叉编译内核模块, Linux 2.6.

椤�build 能够编译内核树目录内的内核模块,也能够编译内核树目录外的内核模块(外部内核模块)。. 编译外部内核模块的命令: #cd <your-mole-dir> #make -C <path-to-kernel> M=`pwd` 其中<your-mole-dir> 为要编译的内核模块所在目录,<path-to-kernel> 为内核源码所在的目录。 对于发行版本的Linux ,可以用: #make -C /lib/moles/`uname -r`/build M=`pwd` 注意:使用Kbuild 之前,必须先成功编译过内核源码。 说明: .#make -C <path-to-kernel> M=`pwd` moles 作用与上面的命令一样 .以前的内核版本可以使用 #make -C <path-to-kernel> SUBDIRS=`pwd` moles. 安装外部内核模块 #make -C <path-to-kernel> M=`pwd` moles_install 默认安装目录为:/lib/moles/`uname -r`/extra ,可以通过INSTALL_MOD_PATH 宏在默认安装路径前加前缀。 例如: #make -C <path-to-kernel> INSTALL_MOD_PATH=/opt M=`pwd` moles_install 则编译后的模块会放在/opt/lib/moles/`uname -r`/extra 通过宏INSTALL_MOD_DIR 可以修改是否放在'extra' 下,例如: #make -C <path-to-kernel> INSTALL_MOD_DIR=golf M=`pwd` moles_install 则编译后的模块会放在/lib/moles/`uname -r`/golf . 编译单个文件 #make -C <path-to-kernel> M=`pwd` <filename>. 其他命令 #make -C <path-to-kernel> M=`pwd` clean #make -C <path-to-kernel> M=`pwd` help.Kbuild 文件 Linux的Kbuild 会在内核模块目录下查找Kbuild 文件,如果有,则在编译时会使用该文件。示例: 假设有这么几个文件:8123_if.c 8123_if.h 8123_pci.c 8123_bin.o_shipped( 二进制的模块文件) Kbuild 文件的内容: obj-m := 8123.o 8123-y:8123_if.o 8123_pci.o 8123_bin.o Makefile的内容: #为了兼容旧版本的Kbuild ifneq($(KERNELRELEASE),) include Kbuildelse# 正常的Makefile KDIR:=/lib/moles/`uname -r`/buildall::$(MAKE) -C $(KDIR) M=`pwd` $@ # 其他targetgenbin:echo "X" > 8123_bin_shippedendif 注意,没有源码的二进制.o 文件必须以原文件名加_shipped 结尾,例如8123_bin.o_shipped,KBuild 会把8123_bin.o_shipped 复制为8123_bin.o ,然后一起编译。 应该用: ifeq ($(obj),) obj= .

④ Linux动态模块怎样编译

编译模块的make file 必须是Makefile,不能是makefile. //why?

ifneq ($(KERNELRELEASE),)
obj-m := your.o
mytest-objs := file1.o file2.o file3.o
else
KDIR := /lib/moles/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) moles
endif

把your换成你的source name ,然后保存为Mafefile ,make 一次就可以了。

⑤ 如何编译一个linux下的驱动模块

这是一个简单而完整的实例,对于理解Linux下的驱动模块是非常有帮助的。

1.源码如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
进入目录:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然后拷入上面书上的源码。
2.编译代码:
1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I选项指定内河源码,也就是内核源码树路径。编译结果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在网上查询有网友提示没有引入kernel.h
解决:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次编译仍然报KERN_ALERT没有声明
修改编译条件-I,再次编译:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接着我尝试在2.6内核的虚拟机上进行编译
编译过程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers

3.执行代码,加载驱动模块:
2.4内核加载模块:
insmod ./hello.o
但是此时并没有输出printk打印的信息。但是可以在/var/log/messages 中看到打印的信息,这是由于KERN_ALERT优先级不够高。这里
需要修改为:KERN_EMERG。再次编译,加载模块即可以看到结果
2.6内核加载模块:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能会出现insmod命令找不到的错误,这可能有下面几个原因:
<1> 你的系统没有安装mole-init-tools工具,关于此问题,只需安装即可,但是一般装完系统是有这个命令的。
<2> 环境变量没有添加导致不能使用该命令。使用echo $PATH即可查看PATH环境变量,发现没有/sbin这个路径,所以你当然不能使用insmod这个命令了。解决的方法很简单,只需在命令行输入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin这个目录下,你可以使用whereis insmod查看)。
<3> insmod这个命令需要在root权限下才能使用。
加载完成后你可以输入lsmod查看hello这个模块哦。

4.卸载驱动模块:rmmod hello.
加载模块后就可在屏幕上看到如下信息:Hello world enter.
卸载时就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world

另外,如果有多个文件,则按下列方式编写Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o

⑥ linux内核模块,怎么编译

我来说下吧 本身你这个问题问的有点歧义 不知道你问的是内核编译 还是模块编译 两个不是一个东西 尽管模块加载后 也是内核的一部分 看看其他的回答 以为是单纯的内核的编译了 模块本身在linux下面是可以分为静态和动态加载的 要是采用静态加载的话 就是从新编译内核 和内核的编译基本是一回事 但是多采用动态加载 这个也简单点
从你的下面的模版可以看出 你是想写驱动程序吧 驱动一般作为动态加载的就可以了 写好你的c文件 格式和上面的差不多 然后GCC编译 生成.o文件,不要生成可执行文件 ( 如果是玩Embedded 就下载到目标板了 minicom 的使用) 如果是就在linux机器上 直接执行 insmod lsmod rmmod 这些就好了 这里也是简单的说下了 内核的编译 写驱动程序 本身就是个比较难得事情了 要个很长的时间去学习了 慢慢积累 好运

⑦ linux 模块编译,hello.c的内容如下

hello.c:1:22: net/sock.h: No such file or directory

是linux/mole.h 的内容22行的问题吧。

⑧ linux编译内核时怎么单个编译一个特定模块

从网上找一个编译模块的makefile,放到你的模块的文件夹里面,然后修改里面的路径指定编译的内核,以及目标名称。make就可以了。

⑨ linux模块编译后加载不成功

从dmesg的输出来看,内核已经export了一个同样的symbol,你加载的驱动再次输出一遍就有问题了。

可能原因:

  1. usbnet模块可能在系统启动后已经加载了,你不需要手动再次加载。这个又可能由两个原因造成:(1)你在menuconfig中选择USBNET模块为[*]模式(驱动集成到内核),而非[M]模式(驱动以模块方式动态插入内核);(2)你已经选择了[M]模式,但是按照系统的默认配置在系统启动过程中会自动动态加载这个驱动,无需手动加载;

  2. 虽然选择了USBNET模块并重新编译了内核,但是新编译的内核并没有更新到系统上,系统还是使用的老的内核。

现在你应该可以排除一下上面的猜测的几种可能原因吧?

⑩ linux 模块编译显示没有头文件

编写linux内核模块,需要自己编写Makefile,同时在Makefile里面制定自己的内核路径,这样才能处理提示没有头文件错误。

编译命令:

exportPATH=$PATH:#编译工具链路径
exportARCH=#CPU类别(例如arm)
exportCROSS_COMPILE=arm-none-linux-gnueabi-#(编译工具xx-gcc的前缀xx)
make-C#编译好的内核模块运行的Linuxkernel内核源代码目录树M=$`pwd`moles

Linux模块编译例子:

exportPATH=$PATH:/usr/local/arm/4.2.2-eabi/usr/bin
#forSamsungs5pc100
exportARCH=arm
exportCROSS_COMPILE=arm-none-linux-gnueabi-
make-C/home/wenxy/src/s5pc100/linux-2.6.35.5M=$`pwd`moles
热点内容
单片机android 发布:2024-09-20 09:07:24 浏览:764
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:663
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:310
子弹算法 发布:2024-09-20 08:41:55 浏览:288
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:816
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:161
sql数据库安全 发布:2024-09-20 08:31:32 浏览:93
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:506
编程键是什么 发布:2024-09-20 07:52:47 浏览:657
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:480