编译环境:rfod6---vmware server 1.0---rfod6-sp1(2.6.23.6)
1:编写Makefile 文件 ,内容如下
obj-m := globalmem.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
    make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules -n
clean:
    rm *.o *.ko
2:编译.c文件    
#gcc   -c   golbalmem.c  //生成globalmem.o文件
编译出错(主要是找不到头文件)
#gcc找到头文件的路径
C_INCLUDE_PATH=/usr/include/libxml2:/MyLib
export C_INCLUDE_PATH

globalmem.c:7:26: error: linux/module.h: 没有那个文件或目录
globalmem.c:11:22: error: linux/mm.h: 没有那个文件或目录
globalmem.c:13:24: error: linux/init.h: 没有那个文件或目录
globalmem.c:14:24: error: linux/cdev.h: 没有那个文件或目录
globalmem.c:15:20: error: asm/io.h: 没有那个文件或目录
globalmem.c:16:24: error: asm/system.h: 没有那个文件或目录
globalmem.c:17:25: error: asm/uaccess.h: 没有那个文件或目录
/usr/include/linux/module.h:9:24: error: linux/list.h: 没有那个文件或目录
/usr/include/linux/module.h:11:28: error: linux/compiler.h: 没有那个文件或目录
/usr/include/linux/module.h:12:25: error: linux/cache.h: 没有那个文件或目录
/usr/include/linux/module.h:13:24: error: linux/kmod.h: 没有那个文件或目录
/usr/include/linux/module.h:15:29: error: linux/stringify.h: 没有那个文件或目录
/usr/include/linux/module.h:16:27: error: linux/kobject.h: 没有那个文件或目录
/usr/include/linux/module.h:17:31: error: linux/moduleparam.h: 没有那个文件或目 录
/usr/include/linux/module.h:18:23: error: asm/local.h: 没有那个文件或目录
/usr/include/linux/module.h:20:24: error: asm/module.h: 没有那个文件或目录
/usr/include/asm/io.h:4:26: error: linux/string.h: 没有那个文件或目录
In file included from globalmem.c:16:
/usr/include/asm/system.h:5:25: error: asm/segment.h: 没有那个文件或目录
/usr/include/asm/system.h:6:28: error: asm/cpufeature.h: 没有那个文件或目录
/usr/include/asm/system.h:7:25: error: asm/cmpxchg.h: 没有那个文件或目录
/usr/include/asm/system.h:296:28: error: linux/irqflags.h: 没有那个文件或目录
In file included from globalmem.c:17:
/usr/include/asm/uaccess.h:8:31: error: linux/thread_info.h: 没有那个文件或目录
/usr/include/asm/uaccess.h:9:28: error: linux/prefetch.h: 没有那个文件或目录
/usr/include/asm/io.h:4:26: error: linux/string.h: 没有那个文件或目录
/usr/include/linux/thread_info.h:21:29: error: asm/thread_info.h: 没有那个文件或
/usr/include/linux/prefetch.h:14:27: error: asm/processor.h: 没有那个文件或目录
/usr/include/asm/local.h:6:24: error: asm/atomic.h: 没有那个文件或目录
/usr/include/linux/cache.h:5:23: error: asm/cache.h: 没有那个文件或目录
/usr/include/asm/local.h:4:26: error: linux/percpu.h: 没有那个文件或目录
/usr/include/asm/cpufeature.h:11:26: error: linux/bitops.h: 没有那个文件或目录
/usr/include/asm/cpufeature.h:13:35: error: asm/required-features.h: 没有那个文
件或目录
/usr/include/asm/bitops.h:408:38: error: asm-generic/bitops/fls64.h: 没有那个文
件或目录
/usr/include/asm/atomic.h:265:32: error: asm-generic/atomic.h: 没有那个文件或目

失败总结:直接将内核源代码的头文件加入到usr/include目录中,并不能正常编译通过,这个问题也不内核开发包没装的问题







可行编译方法:
编译选项的话,应该是在相应目录下面的Kconfig里面添加。
以下文字摘自《Linux设备驱动开发详解》
在Linux内核中增加程序需要完成以下3项工作。
1 将编写的源文件复制到Linux内核源代码的相应目录中
2 在目录的Kconfig文件中增加新源代码对应的编译选项
3 在目录的Makefile里面增加对新代码的编译条目
举例:
1 假设有一个新的驱动globalmem.c,将该文件放到了linux-2.6.26/drivers/char目录下面
2 然后修改该目录下的Kconfig
增加
config GLOBALMEM
tristate "globalmem  y/m/n"
default    m  //编译时的默认选项
help
xxxxxxx
3 然后修改目录下的Makefile
增加
obj-$(CONFIG_GLOBALMEM) += globalmem.o       //这一行必须严格如此,大写部分不能用小写,否则无法生存目标.o文件

单独编译一个模块的话,首先要把内核的模块支持功能开启,才能独立编译成模块
编译流程如下
#cd    /usr/src/linux
#make  clean
#make  allnoconfig
#make  menuconfig
打开模块支持项,选上要独立编译的模块,保存配置文件
#cp   .config   ../oldconfig    //保存配置文件   这一步可选
#make  oldconfig
#make      //会生成模块对应的中间文件  (源文件为globalmem.c,其中间文件为globalmem.o)
#make  modules    //生成模块文件    globalmem.ko   //这一步也可以不用做,我们只需要globalmem.o文件

编译完成后,insmod    globalmem.ko会有出错提示
insmod: error inserting 'globalmem.ko': -1 Invalid module format
一般出现这种错误,是因为makefile文件不对的问题   //makefile文件格式例子如下

# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are lis    ted here.
mymodule-objs := globalmem.o
obj-m := globalmem.o
else
PWD := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
     make  -C $(KDIR) M=$(PWD)
clean:
     rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions     //这一行不能顶头,前面需要一个tab位,否则在make的时候,会出现遗漏分隔符的出错提示!
endif

将globalmem.o和Makefile文件copy到同一个目录下,执行make命令,能正常生成globalmem.ko文件
#insmod   globalmem.ko  
XXXXXXXXXXXXXdevice or resource busy!
问题:因为系统上有个主设备号为254的设备,而globalmem.c源代码中定义的主设备号也为254,两者冲突!
解决办法:将globalmem.c中#define GLOBALMEM_MAJOR 254   改成#define GLOBALMEM_MAJOR 259
               保证主设备号在系统中是唯一的!然后整个过程重作一遍!




另外一种可行的编译方法:  //此方法不推荐
   将整个内核源代码加入globalmem.c(修改过主设备号的)后,按照 boot目录下自带的config文件,在内核配置的时候再选上globalmem,然后重新编译,也是可以的 !!!