『壹』 linux內核模塊編譯-通過Makefile重命名.ko文件名和模塊名
假設模塊的源文件為hello.c,源碼如下:
使用該文件編譯內核模塊。
正常情況下,Makefile文件內容如下:
執行 make 命令,生成hello.ko文件。
執行 sudo insmod hello.ko 命令,安裝該模塊。
執行 lsmod 命令,查看安裝的模塊。就會看到第一行的就是hello模塊。
但是,如果想自定義模塊名稱為 xmole ,而不是默認的 hello ,如何實現呢?方法如下:
在Makefile中重命名obj-m並將obj-m的依賴關系設置為原始模塊(hello)
修改後的Makefile文件內容如下:
將obj-m設置為 xmole .o,並使 xmole .o依賴於 hello .o.
執行 make 命令後,生成 xmole .ko, 而不是 hello .ko,
安裝命令: sudo insmod xmole.ko
查看命令: lsmod ,就會看到被安裝名為 xmole 的模塊。
『貳』 如何編譯一個linux下的驅動模塊
Linux內核源碼路徑:/usr/src/linux(這個源碼是從kernel.org網站download的2.4.18版本)
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
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
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
『叄』 FAST BOOT關閉好還是打開好
開啟好,開機速度液做更快。fastboot為快速啟動,默認為開啟狀態。
開機後快速開機,在電腦系統自檢過程中,直接讀取數據而不重復檢測電腦數據,縮短BIOS自檢時間,臘陵從而提高電腦開機速度。
對於傳統的BIOS,打開主板後快速引導,可以跳過主板的自檢功能,可以縮短計算機的引導時間。
(3)內核模塊快速編譯擴展閱讀:
實現快速啟動的核心關鍵部分方法:
1.快速載入內核模塊(包括設備驅動程序)
內核模塊編譯進內核映像:盡可能減少分別需要載入的模塊,節省時間非同步內核模塊初始化:提高內核模塊的並行化處理,可以同時檢測多個硬體設備來減少啟動時間。
2.加快文件讀取效率(Sreadahead:超級readahead)
採用Sreadahead來提高SSD文件的讀取速度:啟動文件被放輪埋戚入內核頁面緩存中。
3.優化顯示驅動實現X伺服器的快速啟動(fastX)
減少顯卡驅動程序的載入時間和顯卡驅動程序的內存消耗,以加快X伺服器的啟動時間:緩存XKB(leyboard layouts)結果,供以後直接使用。X可以直接使用在內核啟動期間檢測到的使用模式。
4.優化init腳本
簡化不必要的守護進程和服務(NFS伺服器、郵件伺服器),以並行地啟動必要的守護進程服務(如Dbus、Hal、connman)
『肆』 如何單獨編譯內核模塊
第一點,就是源碼樹中有相應的頭文件和函數的實現,沒有源碼樹,你哪調用去呢?(PC上編譯的時候內核有導出符號,系統中有頭文件,這樣就可以引用內核給你的介面了,但是只能編譯你PC上版本的內核可載入的模塊)。
第二個,內核模塊中會記錄版本號的部分,需要記錄版本號的原因是不同的內核版本之間,那些介面和調用可能會有比較大的差異,因此必須要保證你的代碼和某個特定的內核對應,這樣編譯出來的模塊就可以(也是只能)在運行這個內核版本的Linux系統中載入,否則一個很簡單的異常就會導致內核崩潰,或者你的代碼根本無法編譯通過(介面名變了)。
我上面說的是編譯模塊的情況,當然如果是把模塊直接編譯到內核當中去的話,那就不用說了,沒有內核源碼,你無法編譯內核。
『伍』 如何利用automake編譯內核模塊
分布在各目錄下的Kconfig構成了一個分神胡者布式的內核配置資料庫,每個Kconfig分別描述了所屬目錄源文件相關的內核配置菜單。在內核配置make menuconfig(或xconfig等)時,從游薯Kconfig中讀出配置菜單,用戶配置完後保存到.config(在頂層目錄下生成)中。在內核編譯做顫時,主Makefile調用這個.config,就知道了用戶對內核的配置情況。
『陸』 在linux中編寫了一個小的內核模塊,怎麼編譯成.ko文件
從網上找一個編譯模塊的Makefile,放到你的模塊的文件夾裡面,然後修改裡面的路徑指定編譯的內核,以及目標名稱。make就可以了。
『柒』 在openwrt中怎麼編譯自定義的內核模塊
開發環境為ubuntu.首先搭建編譯環境。
sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev gawk asciidoc libz-dev git-core build-essential libssl-dev
下面就是下載源碼,源碼分兩種,一種是最新版但不穩定,就是trunk版,一種是相對穩定版,
如果不是最新下載,最好定期更新代碼,命令為
./scripts/feeds update –a
./scripts/feeds install –a
接著就是編譯了。編譯方法如下:
make defconfig
make menuconfig進入定製界面,選擇自己的設備類型。
make V=99
下面就是增加內核模塊的方法了
進入package目錄,創建模塊目錄
cd backfire/package
mkdir example
進入example目錄,創建Makefile文件和代碼路徑
cd example
touchMakefile
mkdir src
『捌』 如何編譯/交叉編譯內核模塊, 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= .
『玖』 如何快速編譯單一的內核模塊
是編譯多個模塊還是編譯單個模塊?
多個模塊的話在該目錄下面寫一個makefile,內容為
-------------------------------------------------------------------------
obj-m
:=
{模塊1名字}.o
{模塊2名字}.o
...
all:
make
-c
/lib/moles/2.6.32/build/
m=$(pwd)
moles
clean:
make
-c
/lib/moles/2.6.32/build/
m=$(pwd)
clean
--------------------------------------------------------------------------
單個模塊,依賴多個文件的話使用傳統寫法即可。
『拾』 linux內核模塊,怎麼編譯
我來說下吧 本身你這個問題問的有點歧義 不知道你問的是內核編譯 還是模塊編譯 兩個不是一個東西 盡管模塊載入後 也是內核的一部分 看看其他的回答 以為是單純的內核的編譯了 模塊本身在linux下面是可以分為靜態和動態載入的 要是採用靜態載入的話 就是從新編譯內核 和內核的編譯基本是一回事 但是多採用動態載入 這個也簡單點
從你的下面的模版可以看出 你是想寫驅動程序吧 驅動一般作為動態載入的就可以了 寫好你的c文件 格式和上面的差不多 然後GCC編譯 生成.o文件,不要生成可執行文件 ( 如果是玩Embedded 就下載到目標板了 minicom 的使用) 如果是就在linux機器上 直接執行 insmod lsmod rmmod 這些就好了 這里也是簡單的說下了 內核的編譯 寫驅動程序 本身就是個比較難得事情了 要個很長的時間去學習了 慢慢積累 好運