導航:首頁 > 源碼編譯 > 本機編譯驅動

本機編譯驅動

發布時間:2022-12-31 05:19:31

Ⅰ 如何編寫驅動程序

代碼:

#include<linux/mole.h>

#include<linux/kernel.h>

#include<asm/io.h>

#include<linux/miscdevice.h>

#include<linux/fs.h>

#include<asm/uaccess.h>

//流水燈代碼

#define GPM4CON 0x110002e0

#define GPM4DAT 0x110002e4

static unsigned long*ledcon=NULL;

static unsigned long*leddat=NULL;

//自定義write文件操作(不自定義的話,內核有默認的一套文件操作函數)

static ssize_t test_write(struct file*filp,const char __user*buff,size_t count,loff_t*offset)

{

int value=0;

int ret=0;

ret=_from_user(&value,buff,4);

//底層驅動只定義基本操作動作,不定義功能

if(value==1)

{

*leddat|=0x0f;

*leddat&=0xfe;

}

if(value==2)

{

*leddat|=0x0f;

*leddat&=0xfd;

}

if(value==3)

{

*leddat|=0x0f;

*leddat&=0xfb;

}

if(value==4)

{

*leddat|=0x0f;

*leddat&=0xf7;

}

return 0;

}

//文件操作結構體初始化

static struct file_operations g_tfops={

.owner=THIS_MODULE,

.write=test_write,

};

//雜設備信息結構體初始化

static struct miscdevice g_tmisc={

.minor=MISC_DYNAMIC_MINOR,

.name="test_led",

.fops=&g_tfops,

};

//驅動入口函數雜設備初始化

static int __init test_misc_init(void)

{

//IO地址空間映射到內核的虛擬地址空間

ledcon=ioremap(GPM4CON,4);

leddat=ioremap(GPM4DAT,4);

//初始化led

*ledcon&=0xffff0000;

*ledcon|=0x00001111;

*leddat|=0x0f;

//雜設備注冊函數

misc_register(&g_tmisc);

return 0;

}

//驅動出口函數

static void __exit test_misc_exit(void)

{

//釋放地址映射

iounmap(ledcon);

iounmap(leddat);

}

//指定模塊的出入口函數

mole_init(test_misc_init);

mole_exit(test_misc_exit);

MODULE_LICENSE("GPL");

(1)本機編譯驅動擴展閱讀:

include用法:

#include命令預處理命令的一種,預處理命令可以將別的源代碼內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程序代碼;可以定義類似標識符功能的宏,在編譯時,預處理器會用別的文本取代該宏。

插入頭文件的內容

#include命令告訴預處理器將指定頭文件的內容插入到預處理器命令的相應位置。有兩種方式可以指定插入頭文件:

1、#include<文件名>

2、#include"文件名"

如果需要包含標准庫頭文件或者實現版本所提供的頭文件,應該使用第一種格式。如下例所示:

#include<math.h>//一些數學函數的原型,以及相關的類型和宏

如果需要包含針對程序所開發的源文件,則應該使用第二種格式。

採用#include命令所插入的文件,通常文件擴展名是.h,文件包括函數原型、宏定義和類型定義。只要使用#include命令,這些定義就可被任何源文件使用。如下例所示:

#include"myproject.h"//用在當前項目中的函數原型、類型定義和宏

你可以在#include命令中使用宏。如果使用宏,該宏的取代結果必須確保生成正確的#include命令。例1展示了這樣的#include命令。

【例1】在#include命令中的宏

#ifdef _DEBUG_

#define MY_HEADER"myProject_dbg.h"

#else

#define MY_HEADER"myProject.h"

#endif

#include MY_HEADER

當上述程序代碼進入預處理時,如果_DEBUG_宏已被定義,那麼預處理器會插入myProject_dbg.h的內容;如果還沒定義,則插入myProject.h的內容。

Ⅱ vs2010如何編譯驅動

1、安裝VS2010,安裝WDK 7.0(DDK);

2、新建VC++->Empty Project

3、打開Configuration Manager 並新建一個名稱為「 dirver 」的Solution Configuration 並將「dirver」 設為Active Solution Configuration .

4、打開View-> property Manager。

5、在"dirver" solution configuration 上點擊右鍵,選擇Add new property Sheet。取名為「dirverProperty」. 並對他進下以下設置。

5.1. C\C++ - General - Debug Information Format = Program Database (/Zi)
5.2. C\C++ - Preprocessor - Preprocessor Definitions = _X86_ [add also DBG for Debug config]
【WIN32;_DEBUG;_X86_;i386;STD_CALL;CONDITION_HANDLING;WIN32_LEAN_AND_MEAN;NT_UP;SRVDBG;DBG;_IDWBUILD;_WIN32_WINNT=0x0400;% (PreprocessorDefinitions)】
5.3. C\C++ - Code Generation - Enable C++ Exceptions = No
5.4. C\C++ - Code Generation - Basic Runtime Checks = Default
5.5. C\C++ - Code Generation - Buffer Security Check = No (/GS-)
5.6. C\C++ - Advanced - Calling Convention = __stdcall (/Gz)
5.7. C\C++ - Advanced - Compile As = Compile as C Code (/TC) [if you are going to use plain C]
5.8. Linker - General - Output File = $(OutDir)\$(ProjectName).sys
5.9. Linker - General - Enable Incremental Linking = Default
5.10. Linker - Input - Additional Dependencies = ntoskrnl.lib hal.lib $(NOINHERIT) [add here needed libs here e.g. ntoskrnl.lib hal.lib]
【不知道上面是不是筆誤,應該為:ntoskrnl.lib;hal.lib;%(AdditionalDependencies)】
5.11. Linker - Input - Ignore All Default Libraries = Yes (/NODEFAULTLIB)
5.12. Linker - Manifest File - Generate Manifest = No
5.13. Linker - System - SubSystem = Native (/SUBSYSTEM:NATIVE)
5.14. Linker - System - Driver = Driver (/DRIVER)
5.15. Linker - Advanced - Entry Point = DriverEntry
5.16. Linker - Advanced - Base Address = 0x10000
5.17. Linker - Advanced - Randomized Base Address = Disable (/DYNAMICBASE:NO)
【這個也是錯誤的:應該置空】
5.18. Linker - Advanced - Data Execution Prevention (DEP) = Disable (/NXCOMPAT:NO)
【這個也是錯誤的:應該置空】

6. Config VC++ Directories
6.1 Open Open up property manager by clicking on Menu View->Property Manager.
6.2 Expand the project node and then the Configuration|Platform nodes, you will see "Microsoft.cpp.<Platform>.users" file for each Configuration|Platform. These are the files

for the global settings, similar to the old tools/Options/VC++ Directories.
6.3 Multi-Select "Microsoft.cpp.<Platform>.users", right click and bring up the property page window
6.4 In the property page window, click on "VC++ Directories" (for example) in the left pane, add new paths for the directories such as "Include Directories". separated by

semicolons
(eg:Include Directories config As:
$(ddkroot)\INC
$(ddkroot)\INC\WNET
$(ddkroot)\INC\DDK\WNET
Library Directories config As:
$(ddkroot)\LIB\WNET\I386
)
6.5 Make sure to save the settings before shutting down Visual Studio.
6.6 Re-launch Visual Studio and the new settings will be in effect.
6.7 Note: If you would like to only change the settings for one project, you can right click on the project and bring up the property page. Change the settings for 「VC++

Directories」, these settings will be persisted to the project file.
七. OK. Have done. Now you can test it with simple code, e.g.:

#include "ntddk.h"

NTSTATUS
DriverEntry(PDRIVER_OBJECT DriverObject,PUNICODE_STRING RegistryPath)
{
return STATUS_UNSUCCESSFUL;
}

特別說明:
1.
Visual Studio 2010 在智能設備開發方面只支持Windows Phone OS 7.0。如果你要為Windows CE 5.0和Windows Mobile 6.5開發應用程序,請安裝Visual Studio 2008。
2.
做驅動開發時,SDK的版本要和WDK的版本一致,即Win7 WDK要配Win7 SDK,否則會出現編譯錯誤。VS2010里集成了Windows SDK 7.0A。
3.
如果出現類似如下編譯錯誤,解決方法是:拷貝C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\sal.h,然後覆蓋掉C:\WinDDK\7600.16385.1\inc\api\sal.h。

C:\Program Files\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C2143: syntax error : missing ')' before 'const'
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C2143: syntax error : missing '{' before 'const'
.......

Ⅲ 如何編譯一個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設備驅動開發詳解》一書中的步驟實現經典例子"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

Ⅳ 如何編譯驅動程序

驅動的編譯和上層應用程序的編譯完全不同,作為初學者應該先了解一下,即使你還不懂得怎麼寫驅動程序。
首先安裝DDK,然後隨便找一個例子來測試。在菜單中找到BUILD環境菜單執行,不同的系統要使用不同的BUILD環境。會打開一個DOS窗口,這時CD到那個例子程序,輸入 build –cZ回車就可以了。 驅動程序都是用一個由DDK提供的叫build.exe的工具編譯的。此程序以一個名為SOURCES的文件作為輸入,該文件中包含目標可執行文件的名稱、類型和要創建的可執行文件的路徑,注意這個文件沒有後綴名。
SOURCES的文件格式:
TARGETNAME=drivername ,
- 本參數用於指定生成的設備驅動程序名稱(不需後綴名),所產生的文件
- 為drivername.sys.

TARGETPATH=./lib
- 本參數用於指定生成的設備驅動程序所存放的路徑. 一般採用./lib.

TARGETTYPE=DRIVER
- build能夠生成許多不同的目標對象,設備驅動程序一般選用 DRIVER.

INCLUDES=path1;path2;...
- 本參數是可選的, 用於指定其他的#include文件的搜索路徑.

TARGETLIBS=lib1;lib2;...
- 本參數是可選的, 用於指定其他的lib庫文件的搜索路徑.

SOURCES=file1.c file2.c ...
- 本參數用於指定需被編譯的全部源文件名稱, 後綴名不能省略,文件名之間用空格分開.
SOURCES文件是必需的,如果沒有它則表示沒有任何源文件需要編譯。
如果要換行可以用 『/』 符號,表示對上一行的繼續。

也可以創建DIRS文件,DIRS文件用於指定在當前目錄下必須創建的子目錄。
DIRS文件格式:
DIRS文件的內容由一系列用空格分開的目錄名組成
DIRS = /
subdir1 /
subdir2 /
subdir3
DIRS文件是可選的。

有的時候,會提示找不到依賴的文件(.h,.lib 之類),其實設置好 source 文件的
INCLUDES和TARGETLIBS就可以,我第一次編譯時就碰到這個問題,和VC環境區別較大,但習慣就好。

Ⅵ linux下編譯驅動一定要本機有的設備才能編譯么

r8101_n.c: In function `rtl8101_init_one':
r8101_n.c:2832: `RTL_NET_POLL_CONTROLLER' undeclared (first use in this function)
r8101_n.c:2832: (Each undeclared identifier is reported only once
r8101_n.c:2832: for each function it appears in.)

r8101_n.c 的 2832 行:`RTL_NET_POLL_CONTROLLER' 未定義(在這個函數里第一次使用)

驅動必須和內核版本對應,而且必須安裝目標內核的開發部分數據。
Linux 經常變更內核代碼介面,有的時候就會取消某些變數,或者增加新的變數。所以必須對應版本。

Ⅶ 如何編譯一個linux下的驅動模塊

linux下編譯運行驅動
嵌入式linux下設備驅動的運行和linux x86 pc下運行設備驅動是類似的,由於手頭沒有嵌入式linux設備,先在vmware上的linux上學習驅動開發。
按照如下方法就可以成功編譯出hello world模塊驅動。
1、首先確定本機linux版本
怎麼查看Linux的內核kernel版本?
'uname'是Linux/unix系統中用來查看系統信息的命令,適用於所有Linux發行版。配合使用'uname'參數可以查看當前伺服器內核運行的各個狀態。
#uname -a
Linux whh 3.5.0-19-generic #30-Ubuntu SMPTue Nov 13 17:49:53 UTC 2012 i686 i686 i686 GNU/Linux

只列印內核版本,以及主要和次要版本:
#uname -r
3.5.0-19-generic

要列印系統的體系架構類型,即的機器是32位還是64位,使用:
#uname -p
i686

/proc/version 文件也包含系統內核信息:
# cat /proc/version
Linux version 3.5.0-19-generic(buildd@aatxe) (gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) ) #30-UbuntuSMP Tue Nov 13 17:49:53 UTC 2012

發現自己的機器linux版本是:3.5.0-19-generic
2、下載機器內核對應linux源碼
到下面網站可以下載各個版本linux源碼https://www.kernel.org/
如我的機器3.5.0版本源碼下載地址為:https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2
下載完後,找一個路徑解壓,如我解壓到/linux-3.5/
然後很重要的一步是:執行命令uname -r,可以看到Ubuntu的版本信息是3.5.0-19-generic
。進入linux源碼目錄,編輯Makefile,將EXTRAVERSION = 修改為EXTRAVERSION= -19-generic。
這些都是要配置源碼的版本號與系統版本號,如果源碼版本號和系統版本號不一致,在載入模塊的時候會出現如下錯誤:insmod: error inserting 'hello.ko': -1 Invalid mole format。
原因很明確:編譯時用的hello.ko的kenerl 不是我的pc的kenerl版本。

執行命令cp /boot/config-3.5.0-19-generic ./config,覆蓋原有配置文件。
進入linux源碼目錄,執行make menuconfig配置內核,執行make編譯內核。

3、寫一個最簡單的linux驅動代碼hello.c

/*======================================================================
Asimple kernel mole: "hello world"
======================================================================*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("zeroboundaryBSD/GPL");
static int hello_init(void)
{
printk(KERN_INFO"Hello World enter\n");
return0;
}

static void hello_exit(void)
{
printk(KERN_INFO"Hello World exit\n ");
}

mole_init(hello_init);
mole_exit(hello_exit);

MODULE_AUTHOR("zeroboundary");
MODULE_DESCRIPTION("A simple HelloWorld Mole");
MODULE_ALIAS("a simplestmole");

4、寫一個Makefile對源碼進行編譯
KERN_DIR = /linux-3.5
all:
make-C $(KERN_DIR) M=`pwd` moles
clean:
make-C $(KERN_DIR) M=`pwd` clean

obj-m += hello.o

5、模塊載入卸載測試
insmod hello.ko
rmmod hello.ko

然後dmesg|tail就可以看見結果了

最後,再次編譯驅動程序hello.c得到hello.ko。執行insmod ./hello.ko,即可正確insert模塊。

使用insmod hello.ko 將該Mole加入內核中。在這里需要注意的是要用 su 命令切換到root用戶,否則會顯示如下的錯誤:insmod: error inserting 'hello.ko': -1 Operation not permitted

內核模塊版本信息的命令為modinfo hello.ko
通過lsmod命令可以查看驅動是否成功載入到內核中
通過insmod命令載入剛編譯成功的time.ko模塊後,似乎系統沒有反應,也沒看到列印信息。而事實上,內核模塊的列印信息一般不會列印在終端上。驅動的列印都在內核日誌中,我們可以使用dmesg命令查看內核日誌信息。dmesg|tail

可能還會遇到這種問題insmod: error inserting 'hello.ko': -1 Invalid mole format
用dmesg|tail查看內核日誌詳細錯誤
disagrees about version of symbolmole_layout,詳細看這里。
http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmoles/index.html
在X86上我的辦法是:
make -C/usr/src/linux-headers-3.5.0-19-generic SUBDIRS=$PWD moles

Ⅷ linux下怎麼編譯安裝驅動

linux 編譯安裝驅動有兩種,動態載入與靜態載入 動態載入 一,編譯,在指點內核樹下編譯,生成o文件或ko文件 二,將生成的o或ko文件拷到相應目錄,一般是/lib/mole/kernel下面 三,用insmod命令載入,用rmmod命令卸載 靜態載入 靜態載入主linux下怎麼編譯安裝驅動?

Ⅸ 怎麼編譯目標機linux設備驅動

在宿主機上安裝開發工具和下載linux源碼(要求版本號和目標機上的linux內核版本一致)。開發工具主要有gcc、gdb、make等,這些工具在redhat或fc中默認就安裝了,在debian或Ubuntu中可以通過下面這個命令安裝:
apt-get install build-essential
linux源碼可以通過以下幾種途徑獲得:
將源碼解壓到/usr/src/目錄後,進入linux-source-(版本號)目錄中執行下面幾個命令:
make oldconfig
make prepare
make scripts
直接去www.kernel.org下載
通過包管理工具下載源碼,在debian和Ubuntu中可以通過下面這個命令下載,
apt-get install linux-source-(版本號) ,下載後的文件在/usr/src目錄中,解壓到該目錄即可
編寫Linux驅動程序,以一個最簡單的hello.c為例,hello.c的內容如下:

#include "linux/init.h"
#include "linux/mole.h"

static int hello_init(void)
{
printk(KERN_ALERT "Hello World linux_driver_mole\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbey linux_driver_mole\n");
}

mole_init(hello_init);
mole_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lpj");

寫Makefile文件,一個示例如下,裡面各項參數根據實際情況更改:

#sample driver mole
obj-m := hello.o
KDIR = /usr/src/linux-source-2.6.24/

all:
$(MAKE) -C $(KDIR) M=$(PWD)

.PHONY:clean
clean:
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions

編譯,在hello.c和Makefile所在目錄下執行 make 即可,編譯後在當前目錄生成hello.ko文件
載入並測試:載入使用insmod或modprobe命令來實現,如在當前路徑執行如下代碼:
insmod hello.ko 或 modprobe hello
注意,如果在虛擬終端載入內核的話,將看不到內核列印信息,因為內核列印信息不會輸出到虛擬終端,而是輸出到/proc/kmsg文件中,所以可以通過以下方式查看內核信息:
cat /proc/kmsg 會一直列印,需要Ctrl-C手動終止
dmesg 或 dmesg | tail -N ,N為一數字,表示顯示最後N行
卸載:使用rmmod命令卸載驅動模塊,如 rmmod hello

Ⅹ 如何把自己的驅動編譯進內核或模塊

我們知道若要給Linux內核添加模塊(驅動)有如下兩種方式:
(1)動態方式:採用insmod命令來給運行中的linux載入模塊。
(2)靜態方式:修改linux的配置菜單,添加模塊相關文件到源碼對應目錄,然後把模塊直接編譯進內核。
對於動態方式,比較簡單,下面我們介紹如何採用靜態的方式把模塊添加到內核。
最終到達的效果是:在內核的配置菜單中可以配置我們添加的模塊,並可以對我們添加的模塊進行編譯。
一. 內核的配置系統組成
首先我們要了解Linux 2.6內核的配置系統的原理,比如我們在源碼下運行「make menuconfig 」為神馬會出現一個圖形配置菜單,配置了這個菜單後又是如何改變了內核的編譯策略滴。
內核的配置系統一般由以下幾部分組成:
(1)Makefile:分布在Linux內核源代碼中的Makefile,定義Linux內核的編譯規則。
(2)配置文件(Kconfig):給用戶提供配置選項,修改該文件來改變配置菜單選項。
(3)配置工具:包括配置命令解釋器(對配置腳本中使用的配置命令進行解釋),配置用戶界面(提供字元界面和圖形界面)。這些配置工具都是使用腳本語言編寫的,如Tcl/TK、Perl等。
其原理可以簡述如下:這里有兩條主線,一條為配置線索,一條為編譯線索。配置工具根據kconfig配置腳本產生配置菜單,然後根據配置菜單的配置情況生成頂層目錄下的.config,在.config里定義了配置選擇的配置宏定義,如下所示:

如上所示,這里定義的這些配置宏變數會在Makefile里出現,如下所示:

然後make 工具根據Makefile里這些宏的賦值情況來指導編譯。所以理論上,我們可以直接修改.config和Makefile來添加模塊,但這樣很麻煩,也容易出錯,下面我們將會看到,實際上我們有兩種方法來很容易的實現。

二. 如何添加模塊到內核
實際上,我們需要做的工作可簡述如下:
(1)將編寫的模塊或驅動源代碼(比如是XXOO)復制到Linux內核源代碼的相應目錄。
(2)在該目錄下的Kconfig文件中依葫蘆畫瓢的添加XXOO配置選項。
(3)在該目錄的Makefile文件中依葫蘆畫瓢的添加XXOO編譯選項。
可以看到,我們奉行的原則是「依葫蘆畫瓢」,主要是添加。
一般的按照上面方式又可出現兩種情況,一種為給XXOO驅動添加我們自己的目錄,一種是不添加目錄。兩種情況的處理方式有點兒不一樣哦。

三. 不加自己目錄的情況
(1)把我們的驅動源文件(xxoo.c)放到對應目錄下,具體放到哪裡需要根據驅動的類型和特點。這里假設我們放到./driver/char下。
(2)然後我們修改./driver/char下的Kconfig文件,依葫蘆添加即可,如下所示:

注意這里的LT_XXOO這個名字可以隨便寫,但需要保持這個格式,他並不需要跟驅動源文件保持一致,但最好保持一致,等下我們在修改Makefile時會用到這個名字,他將會變成CONFIG_LT_XXOO,那個名字必須與這個名字對應。如上所示,tristate定義了這個配置選項的可選項有幾個,help定義了這個配置選項的幫助信息,具體更多的規則這里不講了。

(3)然後我們修改./driver/char下的Makefile文件,如下所示:

這里我們可以看到,前面Kconfig里出現的LT_XXOO,在這里我們就需要使用到CONFIG_XXOO,實際上邏輯是醬汁滴:在Kconfig里定義了LT_XXOO,然後配置完成後,在頂層的.config里會產生CONFIG_XXOO,然後這里我們使用這個變數。
到這里第一種情況下的添加方式就完成了。
四. 添加自己目錄的情況
(1)在源碼的對應目錄下建立自己的目錄(xxoo),這里假設為/drivers/char/xxoo 。
(2) 把驅動源碼放到新建的xxoo目錄下,並在此目錄下新建Kconfig和Makefile文件。然後給新建的Kconfig和Makefile添加內容。
Kconfig下添加的內容如下:

這個格式跟之前在Kconfig里添加選項類似。
Makefile里寫入的內容就更少了:

添加這一句就可以了。
(3)第三也不復雜,還是依葫蘆畫瓢就可以了。
我們在/drivers/char目錄下添加了xxoo目錄,我們總得在這個配置系統里進行登記吧,哈哈,不然配置系統怎麼找到們呢。由於整個配置系統是遞歸調用滴,所以我們需要在xxoo的父目錄也即char目錄的Kconfig和Makefile文件里進行登記。具體如下:
a). 在drivers/char/Kconfig中加入:source 「drivers/char/xxoo/Kconfig」
b). 在drivers/char/Makefile中加入:obj-$(CONFIG_LT_XXOO) += xxoo/
添加過程依葫蘆畫瓢就可以了,灰常滴簡單。

閱讀全文

與本機編譯驅動相關的資料

熱點內容
微信怎麼發應用app 瀏覽:776
花生殼dns伺服器地址 瀏覽:648
squad伺服器一般什麼時候人多 瀏覽:479
程序員戰門課 瀏覽:474
config保存伺服器地址 瀏覽:317
預訂網吧座位的app叫什麼 瀏覽:416
香港伺服器主機地址 瀏覽:640
網店美工pdf 瀏覽:447
一堆文件夾怎麼弄出來 瀏覽:743
博途如何編譯硬體 瀏覽:418
fortran程序pdf 瀏覽:504
電池消耗演算法 瀏覽:394
伺服器中斷連接怎麼處理 瀏覽:222
上世紀互聯網不發達程序員很難 瀏覽:841
語音識別android開源 瀏覽:762
地埋式垃圾壓縮中轉站 瀏覽:902
apachehttpdlinux 瀏覽:944
快遞員中通app預付款是什麼 瀏覽:843
java路徑轉義 瀏覽:857
keytool加密演算法 瀏覽:131