‘壹’ 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驱动开发 前 为什么要配置编译内核
编译前当然要配置一下内核啊,make menuconfig ,生成默认配置文件 .config ,这个文件是设置 cpu 体系,及很多驱动选项的,你没配置这个,怎么可能编译那
前面那个说 linux 和微软竞争是扯淡的那位,我只能说你根本不懂内核编译,无知
新下载的内核都是没有默认 .config 需要你找到一个默认的config 文件,比如 arch/arm/configs/ 下是 arm cpu 很多的默认配置,当然你也可以把你以前的 config 文件拷贝到新内核目录下。
.config 是配置编译内核的最初步骤,你要编译驱动程序,就必须要了解这个,多上网查下资料
然后重新编译
‘叁’ 如何使用ubuntu来编译驱动
工具/原料
Ubuntu12.04操作系统和测试驱动程序(beep_arv.c)
方法/步骤
在介绍2种方法前,必须知道的知识点:
1.关联文件Makefile:
Makefile:分布在Linux内核源代码中的Makefile用于定义Linux内核的编译规则;
2.管理文件Kconfig:
给用户提供配置选择的功能;
配置工具:
1)包括配置命令解析器;
2)配置用户界面;menuconfig || xconfig;
3)通过脚本语言编写的;
3.
---tristate 代表三种状态:1.[ ]不选择,2.[*]选择直接编译进内核,加载驱动到内核里,3.[m]动态加载驱动;
---bool 代表两种状态,1.[ ]不选择,2.[*]选择;
---"Mini2440 mole sample"这个是在make menuconfig时刷出的提示字符;
---depends on MACH_MINI2440 这个配置选项出现在make menuconfig菜单栏下,在内核配置中必须选中、MACH_MINI2440;
---default m if MACH_MINI2440 这个如果选中了MACH_MINI2440,默认是手
动加载这个驱动;
help:提示帮助信息;
在了解了基本的知识点,便开始进行第一种添加驱动的方法,本次交流是以beep_arv.c蜂鸣驱动程序为基础的
方法一:
1)进入内核的驱动目录;
#cp beep_arv.c /XXX/.../linux-XXXl/drivers/char
2)进入Kconfig添加驱动信息;
#cd /XXX/linux-XXX/.../drivers/char
#vim Kconfig
添加基本信息:
config BEEP_MINI2440
tristate "---HAH--- BEEP"
default
help
this is test makefile!
3)进入Makefile添加驱动编译信息;
#vim Makefile
添加基本信息:
obj-$(CONFIG-BEEP_MINI2440) +=beep_drv.o
方法一结果:
在--Character devices下就能看到配置信息了;
方法二:
1)进入驱动目录,创建BEED目录;
#cd /XXX/.../linux-XXX/drivers/char
#mkdir beep
2)将beep_arv.c驱动程序复制到新建目录下;
#cp beep_arv.c /XXX/.../linux-XXXl/drivers/char/beep
3)创建Makefile和Kconfig文件
#cd char/beep
#mkdir Makefile Kconfig
#chmod 755 Makefile
#chmod 755 Kconfig
4)进入Kconfig添加驱动信息;
#vim Kconfig
添加基本信息:
config BEEP_MINI2440
tristate "---HAH--- BEEP"
default
help
this is test makefile!
5)进入Makefile添加驱动编译信息;
#vim Makefile
添加基本信息:
obj-$(CONFIG_BEEP_MINI2440) +=beep_drv.o
6)并且要到上一级目录的Makefile和Kconfig添加驱动信息;
#cd ../
#vim Makefile
#vim Kconfig
‘肆’ 如何编译驱动程序
驱动的编译和上层应用程序的编译完全不同,作为初学者应该先了解一下,即使你还不懂得怎么写驱动程序。
首先安装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环境区别较大,但习惯就好。
‘伍’ 如何编写驱动程序
代码:
#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");
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的内容。
‘陆’ 在Linux上编译驱动程序问题,怎么解决
可以用ln -s命令创建一个软连接,也就是快捷方式
它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln -s 源文件 目标文件。
当 我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的 目录下用ln命令链接(link)它就可以,不必重复的占用磁盘空间。
例如:ln -s /bin/less /usr/local/bin/less
-s 是代号(symbolic)的意思。
这 里有两点要注意:第一,ln命令会保持每一处链接文件的同步性,也就是说,不论你改动了哪一处,其它的文件都会发生相同的变化;第二,ln的链接又软链接 和硬链接两种,软链接就是ln -s ** **,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间,硬链接ln ** **,没有参数-s, 它会在你选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。
‘柒’ 为什么linux 需要把驱动编译到内核
因为linux的驱动是内核的一部分,内核启动时会检测硬件需要按需加载相应的驱动,如果在编译内核时没有为你的选择的硬件编译相应的模块,内核是无法加载相应的驱动的,这时候就需要你自己动手编译驱动模块了。
‘捌’ 如何编译驱动(sys)程序。悬赏100分!
我看了,他是dev project!
你下个dev C++就可以编译了
不过少了一个文件
can't open font file `../sys/binary/agony.sys': No such file or directory
我去目录看了下没有。。
是个资源文件
‘玖’ linux下怎么编译安装驱动
linux 编译安装驱动有两种,动态加载与静态加载
动态加载
一,编译,在指点内核树下编译,生成.o文件或.ko文件
二,将生成的.o或.ko文件拷到相应目录,一般是/lib/mole/kernel下面
三,用insmod命令加载,用rmmod命令卸载
静态加载
静态加载主要就是编译内核。就是将编写好的驱动放进内核相应的目录下面。然后编译内核。然后运行编译好的内核。
‘拾’ 如何编译一个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