當前位置:首頁 » 編程軟體 » 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 瀏覽:763
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:662
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:309
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:287
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:815
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:160
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:91
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:505
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:655
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:479